diff --git a/mx_util.c b/mx_util.c index b6f404b3..199bcd5c 100644 --- a/mx_util.c +++ b/mx_util.c @@ -79,6 +79,154 @@ inline int mx_stribeginswithany(char *str, char **starts, char **endptr) return _mx_strbeginswithany(str, starts, endptr, 1); } +inline int mx_strtobytes(char *str, unsigned long long int *bytes) +{ + unsigned long long int s = 0; + unsigned long long int t; + + char *end; + + if (!str || !*str) + return -(errno=EINVAL); + + if (strchr(str, '-')) + return -(errno=ERANGE); + + do { + errno = 0; + t = strtoull(str, &end, 10); + + if (errno) + return -errno; + + if (str == end) + return -(errno=EINVAL); + + for (;*end && *end == ' '; end++) + /* empty */; + + switch (*end) { + + case 'T': /* tebi */ + t *= 1024; + + case 'G': /* gibi */ + t *= 1024; + + case 'M': /* mebi */ + t *= 1024; + + case 'k': /* kibi */ + case 'K': + t *= 1024; + + case 'B': /* bytes */ + end++; + break; + + default: + return -(errno=EINVAL); + } + + if (s+t < s) + return -(errno=ERANGE); + + s += t; + + for (;*end && *end == ' '; end++) + /* empty */; + + str = end; + + } while (*str); + + *bytes = s; + + return 0; +} + +inline int mx_strtoseconds(char *str, unsigned long long int *seconds) +{ + unsigned long long int s = 0; + unsigned long long int t; + + char *end; + + if (!str || !*str) + return -(errno=EINVAL); + + if (strchr(str, '-')) + return -(errno=ERANGE); + + do { + errno = 0; + t = strtoull(str, &end, 10); + + if (errno) + return -errno; + + if (str == end) + return -(errno=EINVAL); + + for (;*end && *end == ' '; end++) + /* empty */; + + //if (mx_strtounit(end, &end)); + + switch (*end) { + + case 'y': /* years */ + t *= 52; + + case 'w': /* weeks */ + t *= 7; + + case 'd': /* days */ + t *= 24; + + case 'h': /* hours */ + t *= 60; + + case 'm': /* minutes */ + t *= 60; + + case 's': /* seconds */ + end++; + break; + + default: + return -(errno=EINVAL); + } + + if (s+t < s) + return -(errno=ERANGE); + + s += t; + + for (;*end && *end == ' '; end++) + /* empty */; + + str = end; + + } while (*str); + + *seconds = s; + + return 0; +} + +inline int mx_strtominutes(char *str, unsigned long long int *minutes) +{ + int res; + + res = mx_strtoseconds(str, minutes); + + if (res >= 0) + *minutes /= 60; + + return res; +} + inline char *mx_strskipwhitespaces(char *str) { char *s; diff --git a/mx_util.h b/mx_util.h index 227007bc..d805aef3 100644 --- a/mx_util.h +++ b/mx_util.h @@ -67,6 +67,11 @@ int mx_strbeginswithany(char *str, char **starts, char **endptr); char *mx_strskipwhitespaces(char *str); +int mx_strtobytes(char *str, unsigned long long int *bytes); + +int mx_strtoseconds(char *str, unsigned long long int *seconds); +int mx_strtominutes(char *str, unsigned long long int *minutes); + int mx_strtoul(char *str, unsigned long int *to); int mx_strtoull(char *str, unsigned long long int *to); diff --git a/mxqd.c b/mxqd.c index b5e6697b..8bd485c9 100644 --- a/mxqd.c +++ b/mxqd.c @@ -290,8 +290,13 @@ int server_init(struct mxq_server *server, int argc, char *argv[]) case 'm': if (mx_strtoul(optctl.optarg, &memory_total) < 0) { - mx_log_err("Invalid argument supplied for option --memory '%s': %m", optctl.optarg); - exit(1); + unsigned long long int bytes; + + if(mx_strtobytes(optctl.optarg, &bytes) < 0) { + mx_log_err("Invalid argument supplied for option --memory '%s': %m", optctl.optarg); + exit(1); + } + memory_total = bytes/1024/1024; } if (!memory_total) memory_total = 2048; @@ -299,8 +304,13 @@ int server_init(struct mxq_server *server, int argc, char *argv[]) case 'x': if (mx_strtoul(optctl.optarg, &memory_max) < 0) { - mx_log_err("Invalid argument supplied for option --max-memory-per-slot '%s': %m", optctl.optarg); - exit(1); + unsigned long long int bytes; + + if(mx_strtobytes(optctl.optarg, &bytes) < 0) { + mx_log_err("Invalid argument supplied for option --max-memory-per-slot '%s': %m", optctl.optarg); + exit(1); + } + memory_max = bytes/1024/1024; } break; diff --git a/mxqsub.c b/mxqsub.c index d4d8eb29..5a48b0cb 100644 --- a/mxqsub.c +++ b/mxqsub.c @@ -775,8 +775,13 @@ int main(int argc, char *argv[]) case 'm': if (mx_strtou64(optctl.optarg, &arg_memory) < 0) { - mx_log_crit("--memory '%s': %m", optctl.optarg); - exit(EX_CONFIG); + unsigned long long int bytes; + + if(mx_strtobytes(optctl.optarg, &bytes) < 0) { + mx_log_crit("--memory '%s': %m", optctl.optarg); + exit(EX_CONFIG); + } + arg_memory = bytes/1024/1024; } break; @@ -784,8 +789,18 @@ int main(int argc, char *argv[]) mx_log_warning("option '--time' is deprecated. please use '--runtime' or '-t' in future calls."); case 't': if (mx_strtou32(optctl.optarg, &arg_time) < 0) { - mx_log_crit("--runtime '%s': %m", optctl.optarg); - exit(EX_CONFIG); + unsigned long long int minutes; + + if(mx_strtominutes(optctl.optarg, &minutes) < 0) { + mx_log_crit("--runtime '%s': %m", optctl.optarg); + exit(EX_CONFIG); + } + if ((unsigned long long int)(uint32_t)minutes != minutes) { + errno = ERANGE; + mx_log_crit("--runtime '%s': %m", optctl.optarg); + exit(EX_CONFIG); + } + arg_time = (uint32_t)minutes; } break; diff --git a/test_mx_util.c b/test_mx_util.c index 5bb570bb..66dd7766 100644 --- a/test_mx_util.c +++ b/test_mx_util.c @@ -44,6 +44,7 @@ static void test_mx_strtoul(void) assert(mx_strtoul("-1", &l) == -ERANGE); assert(mx_strtoul(" -1", &l) == -ERANGE); + assert(mx_strtoul("123s", &l) == -EINVAL); assert(mx_strtoul("0888", &l) == -EINVAL); assert(mx_strtoul("1.2", &l) == -EINVAL); assert(mx_strtoul("1,2", &l) == -EINVAL); @@ -181,6 +182,95 @@ static void test_mx_strbeginswithany(void) assert(end == NULL); } +static void test_mx_strtoseconds(void) +{ + unsigned long long int l; + + assert(mx_strtoseconds("123s", &l) == 0); + assert(l == 123); + + assert(mx_strtoseconds("0123s", &l) == 0); + assert(l == 123); + + assert(mx_strtoseconds("123s0s", &l) == 0); + assert(l == 123); + + assert(mx_strtoseconds("2m3s", &l) == 0); + assert(l == 123); + + assert(mx_strtoseconds(" 2 m 3 s ", &l) == 0); + assert(l == 123); + + assert(mx_strtoseconds("1h 2m 3s", &l) == 0); + assert(l == 60*60 + 2*60 + 3); + + assert(mx_strtoseconds("2m 3s 1h", &l) == 0); + assert(l == 60*60 + 2*60 + 3); + + assert(mx_strtoseconds("2m 3s 1h1y", &l) == 0); + assert(l == 60*60 + 2*60 + 3 + 52*7*24*60*60); + + assert(mx_strtoseconds("2m 3s 1h1y", &l) == 0); + assert(l == 60*60 + 2*60 + 3 + 52*7*24*60*60); + + assert(mx_strtoseconds("-1", &l) == -ERANGE); + assert(mx_strtoseconds(" -1", &l) == -ERANGE); + + assert(mx_strtoseconds("123", &l) == -EINVAL); + assert(mx_strtoseconds("0123", &l) == -EINVAL); + assert(mx_strtoseconds("1.2", &l) == -EINVAL); + assert(mx_strtoseconds("1,2", &l) == -EINVAL); + assert(mx_strtoseconds("test", &l) == -EINVAL); +} + +static void test_mx_strtominutes(void) +{ + unsigned long long int l; + + assert(mx_strtominutes("123s", &l) == 0); + assert(l == 2); + + assert(mx_strtominutes("20d", &l) == 0); + assert(l == 20*24*60); + + + assert(mx_strtominutes("-1", &l) == -ERANGE); + assert(mx_strtominutes(" -1", &l) == -ERANGE); + + assert(mx_strtominutes("123", &l) == -EINVAL); + assert(mx_strtominutes("0123", &l) == -EINVAL); + assert(mx_strtominutes("1.2", &l) == -EINVAL); + assert(mx_strtominutes("1,2", &l) == -EINVAL); + assert(mx_strtominutes("test", &l) == -EINVAL); +} + +static void test_mx_strtobytes(void) +{ + unsigned long long int l; + + assert(mx_strtobytes("123B", &l) == 0); + assert(l == 123); + + assert(mx_strtobytes("2M", &l) == 0); + assert(l == 2*1024*1024); + + assert(mx_strtobytes("1M1024k", &l) == 0); + assert(l == 2*1024*1024); + + assert(mx_strtobytes("1024k1024K", &l) == 0); + assert(l == 2*1024*1024); + + assert(mx_strtobytes("-1", &l) == -ERANGE); + assert(mx_strtobytes(" -1", &l) == -ERANGE); + + assert(mx_strtobytes("2.5M", &l) == -EINVAL); + assert(mx_strtobytes("123", &l) == -EINVAL); + assert(mx_strtobytes("0123", &l) == -EINVAL); + assert(mx_strtobytes("1.2", &l) == -EINVAL); + assert(mx_strtobytes("1,2", &l) == -EINVAL); + assert(mx_strtobytes("test", &l) == -EINVAL); +} + int main(int argc, char *argv[]) { test_mx_strskipwhitespaces(); @@ -191,5 +281,8 @@ int main(int argc, char *argv[]) test_mx_strbeginswith(); test_mx_stribeginswith(); test_mx_strbeginswithany(); + test_mx_strtoseconds(); + test_mx_strtominutes(); + test_mx_strtobytes(); return 0; }