From 917ad20c6a855bfefe405ec5b88f7c7a0ce9a688 Mon Sep 17 00:00:00 2001
From: Marius Tolzmann <tolzmann@molgen.mpg.de>
Date: Tue, 28 Jul 2015 00:28:59 +0200
Subject: [PATCH 1/3] mxqsub: Move getumask() to mxqsub.c

---
 mxq_util.c | 7 -------
 mxqsub.c   | 8 ++++++++
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/mxq_util.c b/mxq_util.c
index 3cb6baaa..b83c64cd 100644
--- a/mxq_util.c
+++ b/mxq_util.c
@@ -20,13 +20,6 @@
 #include "mxq_util.h"
 
 
-mode_t getumask(void)
-{
-    mode_t mask = umask( 0 );
-    umask(mask);
-    return mask;
-}
-
 char *mxq_hostname(void)
 {
     static char hostname[1024] = "";
diff --git a/mxqsub.c b/mxqsub.c
index 8682140c..979f43d8 100644
--- a/mxqsub.c
+++ b/mxqsub.c
@@ -116,6 +116,14 @@ static void print_usage(void)
     );
 }
 
+mode_t getumask(void)
+{
+    mode_t mask = umask( 0 );
+    umask(mask);
+    return mask;
+}
+
+
 static int load_group_id(struct mx_mysql *mysql, struct mxq_group *g)
 {
     struct mx_mysql_stmt *stmt = NULL;

From f9538ed2d2cc8be07d35633b1f9e311c738d31c3 Mon Sep 17 00:00:00 2001
From: Marius Tolzmann <tolzmann@molgen.mpg.de>
Date: Tue, 28 Jul 2015 00:58:28 +0200
Subject: [PATCH 2/3] mx_util: Add mx_hostname() from mxq_util

---
 mx_util.c  | 18 ++++++++++++++++++
 mx_util.h  |  1 +
 mxq_util.c | 19 -------------------
 mxq_util.h |  2 --
 mxqd.c     |  4 ++--
 mxqsub.c   |  2 +-
 6 files changed, 22 insertions(+), 24 deletions(-)

diff --git a/mx_util.c b/mx_util.c
index da611762..a9b55465 100644
--- a/mx_util.c
+++ b/mx_util.c
@@ -441,6 +441,24 @@ int mx_asprintf_forever(char **strp, const char *fmt, ...)
     return len;
 }
 
+char *mx_hostname(void)
+{
+    static char hostname[1024] = "";
+    int res;
+
+    if (*hostname)
+        return hostname;
+
+    res = gethostname(hostname, 1024);
+    if (res == -1) {
+        if (errno != ENAMETOOLONG)
+            assert_perror(errno);
+        hostname[1024-1] = 0;
+    }
+
+    return hostname;
+}
+
 char *mx_dirname(char *path)
 {
     char *tmp;
diff --git a/mx_util.h b/mx_util.h
index 09cc545b..9a48b908 100644
--- a/mx_util.h
+++ b/mx_util.h
@@ -88,6 +88,7 @@ char *mx_strdup_forever(char *str);
 int mx_vasprintf_forever(char **strp, const char *fmt, va_list ap);
 int mx_asprintf_forever(char **strp, const char *fmt, ...)  __attribute__ ((format(printf, 2, 3)));
 
+char *mx_hostname(void);
 char *mx_dirname(char *path);
 char *mx_dirname_forever(char *path);
 
diff --git a/mxq_util.c b/mxq_util.c
index b83c64cd..837537a0 100644
--- a/mxq_util.c
+++ b/mxq_util.c
@@ -19,25 +19,6 @@
 
 #include "mxq_util.h"
 
-
-char *mxq_hostname(void)
-{
-    static char hostname[1024] = "";
-    int res;
-
-    if (*hostname)
-        return hostname;
-
-    res = gethostname(hostname, 1024);
-    if (res == -1) {
-        if (errno != ENAMETOOLONG)
-            assert_perror(errno);
-        hostname[1024-1] = 0;
-    }
-
-    return hostname;
-}
-
 char **strvec_new(void)
 {
     char **strvec;
diff --git a/mxq_util.h b/mxq_util.h
index e2e85603..8ddbfdda 100644
--- a/mxq_util.h
+++ b/mxq_util.h
@@ -6,8 +6,6 @@
 #include <string.h>
 #include <mysql.h>
 
-char *mxq_hostname(void);
-
 char** strvec_new(void);
 size_t strvec_length(char **strvec);
 int    strvec_push_str(char ***strvecp, char *str);
diff --git a/mxqd.c b/mxqd.c
index bbaf2d1b..7c5886f1 100644
--- a/mxqd.c
+++ b/mxqd.c
@@ -332,7 +332,7 @@ int server_init(struct mxq_server *server, int argc, char *argv[])
     mx_mysql_option_set_default_group(server->mysql, arg_mysql_default_group);
     mx_mysql_option_set_reconnect(server->mysql, 1);
 
-    server->hostname = mxq_hostname();
+    server->hostname = mx_hostname();
     server->server_id = arg_server_id;
 
     server->flock = mx_flock(LOCK_EX, "/dev/shm/mxqd.%s.%s.lck", server->hostname, server->server_id);
@@ -768,7 +768,7 @@ static int init_child_process(struct mxq_group_list *group, struct mxq_job *j)
     mx_setenv_forever("PWD",      j->job_workdir);
     mx_setenv_forever("HOME",     passwd->pw_dir);
     mx_setenv_forever("SHELL",    passwd->pw_shell);
-    mx_setenv_forever("HOSTNAME", mxq_hostname());
+    mx_setenv_forever("HOSTNAME", mx_hostname());
     mx_setenvf_forever("JOB_ID",      "%lu",    j->job_id);
     mx_setenvf_forever("MXQ_JOBID",   "%lu",    j->job_id);
     mx_setenvf_forever("MXQ_THREADS", "%d",     g->job_threads);
diff --git a/mxqsub.c b/mxqsub.c
index 979f43d8..d0768fd5 100644
--- a/mxqsub.c
+++ b/mxqsub.c
@@ -736,7 +736,7 @@ int main(int argc, char *argv[])
     /******************************************************************/
 
     group.job_command = arg_program_name;
-    job.host_submit = mxq_hostname();
+    job.host_submit = mx_hostname();
 
     /******************************************************************/
 

From c5382e110dbe2fdeb470aea2c3c92437d4778553 Mon Sep 17 00:00:00 2001
From: Marius Tolzmann <tolzmann@molgen.mpg.de>
Date: Tue, 28 Jul 2015 01:10:49 +0200
Subject: [PATCH 3/3] mx_util: Add mx_strvec_() and remove mxq_util

---
 .gitignore |   1 -
 Makefile   |  21 +-----
 mx_util.c  | 191 +++++++++++++++++++++++++++++++++++++++++++++++
 mx_util.h  |   9 +++
 mxq_util.c | 215 -----------------------------------------------------
 mxq_util.h |  17 -----
 mxqd.c     |   3 +-
 mxqdump.c  |   2 +-
 mxqsub.c   |   4 +-
 test.c     |   2 -
 10 files changed, 205 insertions(+), 260 deletions(-)
 delete mode 100644 mxq_util.c
 delete mode 100644 mxq_util.h

diff --git a/.gitignore b/.gitignore
index d9ac12bb..27915310 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,7 +8,6 @@ mxq_job.o
 mxqd.o
 mxqsub.o
 mxqkill.o
-mxq_util.o
 test_mx_util.o
 test_mx_log.o
 test_mx_mysql.o
diff --git a/Makefile b/Makefile
index 818f82dd..c926ac18 100644
--- a/Makefile
+++ b/Makefile
@@ -184,11 +184,6 @@ mx_mysql.h += $(mx_util.h)
 
 mx_mxq.h += mx_mxq.h
 
-### mxq_util.h ---------------------------------------------------------
-
-mxq_util.h += mxq_util.h
-mxq_util.h += $(mx_log.h)
-
 ### mxq_group.h --------------------------------------------------------
 
 mxq_group.h += mxq_group.h
@@ -250,7 +245,7 @@ clean: CLEAN += mxq_log.o
 ### mxqdump.o ---------------------------------------------------
 
 mxqdump.o: $(mx_log.h)
-mxqdump.o: $(mxq_util.h)
+mxqdump.o: $(mx_util.h)
 mxqdump.o: $(mx_mysql.h)
 mxqdump.o: $(mx_getopt.h)
 mxqdump.o: CFLAGS += $(CFLAGS_MYSQL)
@@ -270,15 +265,6 @@ mxqkill.o: CFLAGS += $(CFLAGS_MYSQL)
 
 clean: CLEAN += mxqkill.o
 
-### mxq_util.o ---------------------------------------------------------
-
-mxq_util.o: $(mx_log.h)
-mxq_util.o: $(mxq_util.h)
-mxq_util.o: CFLAGS += $(CFLAGS_MYSQL)
-mxq_util.o: CFLAGS += -Wno-unused-variable
-
-clean: CLEAN += mxq_util.o
-
 ### mxq_group.o --------------------------------------------------------
 
 mxq_group.o: $(mx_log.h)
@@ -324,7 +310,7 @@ mxqsub.o: $(mx_mysql.h)
 mxqsub.o: $(mxq.h)
 mxqsub.o: $(mxq_group.h)
 mxqsub.o: $(mxq_job.h)
-mxqsub.o: $(mxq_util.h)
+mxqsub.o: $(mx_util.h)
 mxqsub.o: CFLAGS += $(CFLAGS_MYSQL)
 
 clean: CLEAN += mxqsub.o
@@ -340,7 +326,6 @@ mxqd: mxq_log.o
 mxqd: mx_getopt.o
 mxqd: mxq_group.o
 mxqd: mxq_job.o
-mxqd: mxq_util.o
 mxqd: mx_mysql.o
 mxqd: LDLIBS += $(LDLIBS_MYSQL)
 
@@ -354,7 +339,6 @@ install:: mxqd
 ### mxqsub ------------------------------------------------------------
 
 mxqsub: mx_getopt.o
-mxqsub: mxq_util.o
 mxqsub: mx_util.o
 mxqsub: mx_log.o
 mxqsub: mx_mysql.o
@@ -373,7 +357,6 @@ mxqdump: mx_log.o
 mxqdump: mx_mysql.o
 mxqdump: mxq_group.o
 mxqdump: mxq_job.o
-mxqdump: mxq_util.o
 mxqdump: mx_util.o
 mxqdump: mx_getopt.o
 mxqdump: LDLIBS += $(LDLIBS_MYSQL)
diff --git a/mx_util.c b/mx_util.c
index a9b55465..b6f404b3 100644
--- a/mx_util.c
+++ b/mx_util.c
@@ -633,3 +633,194 @@ void *mx_calloc_forever_sec(size_t nmemb, size_t size, unsigned int time)
 
     return ptr;
 }
+
+char **mx_strvec_new(void)
+{
+    char **strvec;
+
+    strvec = calloc(sizeof(*strvec), 1);
+
+    return strvec;
+}
+
+static inline size_t mx_strvec_length_cache(char **strvec, int32_t len)
+{
+    static char ** sv = NULL;
+    static size_t l = 0;
+
+    if (likely(len == -1)) {
+        if (likely(sv == strvec)) {
+            return l;
+        }
+        return -1;
+    }
+
+    if (likely(sv == strvec)) {
+        l = len;
+    } else {
+        sv = strvec;
+        l = len;
+    }
+    return l;
+}
+
+size_t mx_strvec_length(char ** strvec)
+{
+    char ** sv;
+    size_t len;
+
+    assert(strvec);
+
+    sv = strvec;
+
+    len = mx_strvec_length_cache(sv, -1);
+    if (len != -1)
+        return len;
+
+    for (; *sv; sv++);
+
+    len = sv-strvec;
+    mx_strvec_length_cache(sv, len);
+    return len;
+}
+
+int mx_strvec_push_str(char *** strvecp, char * str)
+{
+    char ** sv;
+
+    size_t len;
+
+    assert(strvecp);
+    assert(*strvecp);
+    assert(str);
+
+    len = mx_strvec_length(*strvecp);
+
+    sv = realloc(*strvecp, sizeof(**strvecp) * (len + 2));
+    if (!sv) {
+       return 0;
+    }
+
+    sv[len++] = str;
+    sv[len] = NULL;
+
+    mx_strvec_length_cache(sv, len);
+
+    *strvecp = sv;
+
+    return 1;
+}
+
+int mx_strvec_push_strvec(char ***strvecp, char **strvec)
+{
+    char **sv;
+
+    size_t len1;
+    size_t len2;
+
+    assert(strvecp);
+    assert(*strvecp);
+    assert(strvec);
+
+    len1 = mx_strvec_length(*strvecp);
+    len2 = mx_strvec_length(strvec);
+
+    sv = realloc(*strvecp, sizeof(**strvecp) * (len1 + len2 + 1));
+    if (!sv) {
+       return 0;
+    }
+
+    memcpy(sv+len1, strvec, sizeof(*strvec) * (len2 + 1));
+
+    mx_strvec_length_cache(sv, len1+len2);
+
+    *strvecp = sv;
+
+    return 1;
+}
+
+char *mx_strvec_to_str(char **strvec)
+{
+    char **sv;
+    char*  buf;
+    char*  s;
+    size_t totallen;
+    char*  str;
+
+    assert(strvec);
+
+    totallen = 0;
+    for (sv = strvec; *sv; sv++) {
+        totallen += strlen(*sv);
+    }
+
+    buf = malloc(sizeof(*buf) * (totallen * 2 + 2) + 1);
+    if (!buf)
+        return NULL;
+
+    str  = buf;
+    *str = 0;
+
+    for (sv = strvec; *sv; sv++) {
+        for (s=*sv; *s; s++) {
+            switch (*s) {
+                case '\\':
+                    *(str++) = '\\';
+                    *(str++) = '\\';
+                    break;
+
+                default:
+                    *(str++) = *s;
+            }
+        }
+        *(str++) = '\\';
+        *(str++) = '0';
+    }
+
+    *str = '\0';
+
+    return buf;
+}
+
+void mx_strvec_free(char **strvec)
+{
+    char **sv;
+
+    if (!strvec)
+        return;
+
+    for (sv = strvec; *sv; sv++) {
+        free(sv);
+    }
+    free(strvec);
+}
+
+char **mx_strvec_from_str(char *str)
+{
+    int res;
+    char* s;
+    char* p;
+    char** strvec;
+
+    strvec = mx_strvec_new();
+    if (!strvec)
+        return NULL;
+
+    for (s=str; *s; s=p+2) {
+       p = strstr(s, "\\0");
+       if (!p) {
+           free(strvec);
+           errno = EINVAL;
+           return NULL;
+       }
+       *p = 0;
+
+       res = mx_strvec_push_str(&strvec, s);
+       if (!res) {
+          free(strvec);
+          return NULL;
+       }
+    }
+
+    return strvec;
+}
diff --git a/mx_util.h b/mx_util.h
index 9a48b908..227007bc 100644
--- a/mx_util.h
+++ b/mx_util.h
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <stdarg.h>
+#include <string.h>
 
 #include "mx_log.h"
 
@@ -111,4 +112,12 @@ int mx_sleep_nofail(unsigned int seconds);
 
 void *mx_calloc_forever_sec(size_t nmemb, size_t size, unsigned int time);
 
+char** mx_strvec_new(void);
+size_t mx_strvec_length(char **strvec);
+int    mx_strvec_push_str(char ***strvecp, char *str);
+int    mx_strvec_push_strvec(char*** strvecp, char **strvec);
+char*  mx_strvec_to_str(char **strvec);
+char** mx_strvec_from_str(char *str);
+void   mx_strvec_free(char **strvec);
+
 #endif
diff --git a/mxq_util.c b/mxq_util.c
deleted file mode 100644
index 837537a0..00000000
--- a/mxq_util.c
+++ /dev/null
@@ -1,215 +0,0 @@
-
-#define _GNU_SOURCE
-
-#include <unistd.h>
-#include <string.h>
-#include <stdio.h>
-
-#include <errno.h>
-#include <assert.h>
-
-#include <stdarg.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-
-#include <time.h>
-#include "mx_log.h"
-#include "mx_util.h"
-
-#include "mxq_util.h"
-
-char **strvec_new(void)
-{
-    char **strvec;
-
-    strvec = calloc(sizeof(*strvec), 1);
-
-    return strvec;
-}
-
-static inline size_t strvec_length_cache(char **strvec, int32_t len)
-{
-    static char ** sv = NULL;
-    static size_t l = 0;
-
-    if (likely(len == -1)) {
-        if (likely(sv == strvec)) {
-            return l;
-        }
-        return -1;
-    }
-
-    if (likely(sv == strvec)) {
-        l = len;
-    } else {
-        sv = strvec;
-        l = len;
-    }
-    return l;
-}
-
-size_t strvec_length(char ** strvec)
-{
-    char ** sv;
-    size_t len;
-
-    assert(strvec);
-
-    sv = strvec;
-
-    len = strvec_length_cache(sv, -1);
-    if (len != -1)
-        return len;
-
-    for (; *sv; sv++);
-
-    len = sv-strvec;
-    strvec_length_cache(sv, len);
-    return len;
-}
-
-int strvec_push_str(char *** strvecp, char * str)
-{
-    char ** sv;
-
-    size_t len;
-
-    assert(strvecp);
-    assert(*strvecp);
-    assert(str);
-
-    len = strvec_length(*strvecp);
-
-    sv = realloc(*strvecp, sizeof(**strvecp) * (len + 2));
-    if (!sv) {
-       return 0;
-    }
-
-    sv[len++] = str;
-    sv[len] = NULL;
-
-    strvec_length_cache(sv, len);
-
-    *strvecp = sv;
-
-    return 1;
-}
-
-int strvec_push_strvec(char ***strvecp, char **strvec)
-{
-    char **sv;
-
-    size_t len1;
-    size_t len2;
-
-    assert(strvecp);
-    assert(*strvecp);
-    assert(strvec);
-
-    len1 = strvec_length(*strvecp);
-    len2 = strvec_length(strvec);
-
-    sv = realloc(*strvecp, sizeof(**strvecp) * (len1 + len2 + 1));
-    if (!sv) {
-       return 0;
-    }
-
-    memcpy(sv+len1, strvec, sizeof(*strvec) * (len2 + 1));
-
-    strvec_length_cache(sv, len1+len2);
-
-    *strvecp = sv;
-
-    return 1;
-}
-
-char *strvec_to_str(char **strvec)
-{
-    char **sv;
-    char*  buf;
-    char*  s;
-    size_t totallen;
-    char*  str;
-
-    assert(strvec);
-
-    totallen = 0;
-    for (sv = strvec; *sv; sv++) {
-        totallen += strlen(*sv);
-    }
-
-    buf = malloc(sizeof(*buf) * (totallen * 2 + 2) + 1);
-    if (!buf)
-        return NULL;
-
-    str  = buf;
-    *str = 0;
-
-    for (sv = strvec; *sv; sv++) {
-        for (s=*sv; *s; s++) {
-            switch (*s) {
-                case '\\':
-                    *(str++) = '\\';
-                    *(str++) = '\\';
-                    break;
-
-                default:
-                    *(str++) = *s;
-            }
-        }
-        *(str++) = '\\';
-        *(str++) = '0';
-    }
-
-    *str = '\0';
-
-    return buf;
-}
-
-void strvec_free(char **strvec)
-{
-    char **sv;
-    char*  buf;
-    char*  s;
-    size_t totallen;
-    char*  str;
-
-    if (!strvec)
-        return;
-
-    for (sv = strvec; *sv; sv++) {
-        free(sv);
-    }
-    free(strvec);
-}
-
-char **strvec_from_str(char *str)
-{
-    int res;
-    char* s;
-    char* p;
-    char** strvec;
-
-    strvec = strvec_new();
-    if (!strvec)
-        return NULL;
-
-    for (s=str; *s; s=p+2) {
-       p = strstr(s, "\\0");
-       if (!p) {
-           free(strvec);
-           errno = EINVAL;
-           return NULL;
-       }
-       *p = 0;
-
-       res = strvec_push_str(&strvec, s);
-       if (!res) {
-          free(strvec);
-          return NULL;
-       }
-    }
-
-    return strvec;
-}
diff --git a/mxq_util.h b/mxq_util.h
deleted file mode 100644
index 8ddbfdda..00000000
--- a/mxq_util.h
+++ /dev/null
@@ -1,17 +0,0 @@
-
-#ifndef __MXQ_UTIL_H__
-#define __MXQ_UTIL_H__ 1
-
-#include <stdlib.h>
-#include <string.h>
-#include <mysql.h>
-
-char** strvec_new(void);
-size_t strvec_length(char **strvec);
-int    strvec_push_str(char ***strvecp, char *str);
-int    strvec_push_strvec(char*** strvecp, char **strvec);
-char*  strvec_to_str(char **strvec);
-char** strvec_from_str(char *str);
-void   strvec_free(char **strvec);
-
-#endif
diff --git a/mxqd.c b/mxqd.c
index 7c5886f1..034e2c17 100644
--- a/mxqd.c
+++ b/mxqd.c
@@ -33,7 +33,6 @@
 #include "mxq_group.h"
 #include "mxq_job.h"
 #include "mx_mysql.h"
-#include "mxq_util.h"
 #include "mxqd.h"
 
 #ifndef MXQ_VERSION
@@ -1014,7 +1013,7 @@ unsigned long start_job(struct mxq_group_list *group)
         }
 
 
-        argv = strvec_from_str(mxqjob.job_argv_str);
+        argv = mx_strvec_from_str(mxqjob.job_argv_str);
         if (!argv) {
             mx_log_err("job=%s(%d):%lu:%lu Can't recaculate commandline. str_to_strvev(%s) failed: %m",
                 group->group.user_name, group->group.user_uid, group->group.group_id, mxqjob.job_id,
diff --git a/mxqdump.c b/mxqdump.c
index 10243725..424a279c 100644
--- a/mxqdump.c
+++ b/mxqdump.c
@@ -19,7 +19,7 @@
 
 #include "mxq.h"
 
-#include "mxq_util.h"
+#include "mx_util.h"
 #include "mx_mysql.h"
 
 #include "mxq_group.h"
diff --git a/mxqsub.c b/mxqsub.c
index d0768fd5..cbd311c9 100644
--- a/mxqsub.c
+++ b/mxqsub.c
@@ -26,8 +26,6 @@
 
 #include <string.h>
 
-#include "mxq_util.h"
-
 #include "mxq_group.h"
 #include "mxq_job.h"
 
@@ -684,7 +682,7 @@ int main(int argc, char *argv[])
         arg_stderr = arg_stderr_absolute;
     }
 
-    arg_args = strvec_to_str(argv);
+    arg_args = mx_strvec_to_str(argv);
     assert(arg_args);
 
     /******************************************************************/
diff --git a/test.c b/test.c
index 9d21e944..6cac737e 100644
--- a/test.c
+++ b/test.c
@@ -4,8 +4,6 @@
 #include <string.h>
 #include <malloc.h>
 
-#include "mxq_util.h"
-
 static void display_mallinfo(void)
 {
     struct mallinfo mi;