]> jfr.im git - irc/atheme/libmowgli-2.git/commitdiff
helper: allow setting of the process title.
authorElizabeth J. Myers <redacted>
Fri, 6 Apr 2012 03:53:20 +0000 (22:53 -0500)
committerElizabeth J. Myers <redacted>
Sun, 8 Apr 2012 08:32:48 +0000 (03:32 -0500)
Also allow proctitle to discard empty process titles.

src/examples/helpertest/helpertest.c
src/libmowgli/core/process.c
src/libmowgli/core/process.h
src/libmowgli/eventloop/eventloop.h
src/libmowgli/eventloop/helper.c
src/libmowgli/ext/proctitle.c

index 56661b54d276df306003cad66dfdef8f01c54ea4..9106000fab83c702b7b00a3495c67cedfc2e7ab6 100644 (file)
@@ -65,7 +65,7 @@ void helper_spawn(mowgli_eventloop_t *eventloop)
        if (helper_count >= 100)
                return;
 
-       helper = mowgli_helper_create(eventloop, helper_start, NULL);
+       helper = mowgli_helper_create(eventloop, helper_start, "Spawned helper", NULL);
        mowgli_helper_set_read_cb(eventloop, helper, helper_read);
 
        helper_count++;
@@ -96,6 +96,9 @@ int main(int argc, char *argv[])
 {
        mowgli_eventloop_t *base_eventloop;
 
+       /* Bleh this is needed to ensure some systems can set the process title */
+       argv = mowgli_proctitle_init(argc, argv);
+
        base_eventloop = mowgli_eventloop_create();
 
        helper_spawn(base_eventloop);
index 6784a6de59c4fd69e1a01dee6126313c470ca60e..43132a63b6ee29e01349e8b7780b6abf2f939256 100644 (file)
@@ -33,6 +33,8 @@ mowgli_process_cloned_execv(mowgli_process_execv_req_t *execv_req)
        return_if_fail(execv_req->path != NULL);
        return_if_fail(execv_req->argv != NULL);
 
+       /* Do best to set proctitle if below hack don't work */
+       mowgli_proctitle_set("%s", execv_req->argv[0]);
        execv(execv_req->path, execv_req->argv);
 
        mowgli_free(execv_req->argv);
@@ -44,7 +46,7 @@ mowgli_process_cloned_execv(mowgli_process_execv_req_t *execv_req)
 }
 
 mowgli_process_t *
-mowgli_process_clone(mowgli_process_start_fn_t start_fn, void *userdata)
+mowgli_process_clone(mowgli_process_start_fn_t start_fn, const char *procname, void *userdata)
 {
 #ifndef _WIN32
        mowgli_process_t *out;
@@ -61,6 +63,8 @@ mowgli_process_clone(mowgli_process_start_fn_t start_fn, void *userdata)
                break;
 
        case 0:
+               /* Do our best to set this... */
+               mowgli_proctitle_set("%s", procname);
                start_fn(out->userdata);
                _exit(255);
 
@@ -100,7 +104,7 @@ mowgli_process_spawn(const char *path, char *const argv[])
        for (i = 0; argv[i] != NULL; i++)
                req->argv[i] = argv[i];
 
-       return mowgli_process_clone((mowgli_process_start_fn_t) mowgli_process_cloned_execv, req);
+       return mowgli_process_clone((mowgli_process_start_fn_t) mowgli_process_cloned_execv, req->argv[0], req);
 }
 
 void
index 0db32c963fa6228af801c5c0a01a1d223c23ce9c..f3559447d30ed23886dc46df55d612a27c3cbe6e 100644 (file)
@@ -30,7 +30,7 @@ typedef struct {
        void *userdata;
 } mowgli_process_t;
 
-extern mowgli_process_t *mowgli_process_clone(mowgli_process_start_fn_t start_fn, void *userdata);
+extern mowgli_process_t *mowgli_process_clone(mowgli_process_start_fn_t start_fn, const char *proctitle, void *userdata);
 extern mowgli_process_t *mowgli_process_spawn(const char *path, char *const argv[]);
 extern void mowgli_process_kill(mowgli_process_t *process);
 
index 1d721a17cb16b2b344d6ed18c0f374da9a220913..d330d31da85e1e879612e4c359b9b0cbae6a8f4d 100644 (file)
@@ -212,7 +212,7 @@ struct _mowgli_helper {
 };
 
 /* helper.c */
-extern mowgli_eventloop_helper_proc_t *mowgli_helper_create(mowgli_eventloop_t *eventloop, mowgli_eventloop_helper_start_fn_t *start_fn, void *userdata);
+extern mowgli_eventloop_helper_proc_t *mowgli_helper_create(mowgli_eventloop_t *eventloop, mowgli_eventloop_helper_start_fn_t *start_fn, const char *helpername, void *userdata);
 
 /* creation of helpers inside other executable images */
 extern mowgli_eventloop_helper_proc_t *mowgli_helper_spawn(mowgli_eventloop_t *eventloop, const char *path, char *const argv[]);
index 40c14a106bb56f1603810d3486eaa3abd7c41368..b89e447ba6558f61eec7cfc617738b358a35390a 100644 (file)
@@ -70,7 +70,7 @@ mowgli_helper_trampoline(mowgli_helper_create_req_t *req)
 }
 
 mowgli_eventloop_helper_proc_t *
-mowgli_helper_create(mowgli_eventloop_t *eventloop, mowgli_eventloop_helper_start_fn_t *start_fn, void *userdata)
+mowgli_helper_create(mowgli_eventloop_t *eventloop, mowgli_eventloop_helper_start_fn_t *start_fn, const char *helpername, void *userdata)
 {
        mowgli_eventloop_helper_proc_t *helper;
        mowgli_helper_create_req_t child;
@@ -97,7 +97,7 @@ mowgli_helper_create(mowgli_eventloop_t *eventloop, mowgli_eventloop_helper_star
        mowgli_pollable_set_nonblocking(helper->pfd, true);
 
        /* spawn helper process using mowgli_process_clone() */
-       helper->child = mowgli_process_clone((mowgli_process_start_fn_t) mowgli_helper_trampoline, &child);
+       helper->child = mowgli_process_clone((mowgli_process_start_fn_t) mowgli_helper_trampoline, helpername, &child);
 
        if (helper->child == NULL)
        {
index 91670c09167efd6407889d79d6446dbf07bac66a..2410f9205df689ac6ba145503d5b7e18a5bbb9fa 100644 (file)
@@ -131,48 +131,47 @@ mowgli_proctitle_init(int argc, char **argv)
         * If we're going to overwrite the argv area, count the available space.
         * Also move the environment to make additional room.
         */
+       char *end_of_area = NULL;
+       char **new_environ;
+       int i;
+
+       /*
+        * check for contiguous argv strings
+        */
+       for (i = 0; i < argc; i++)
+       {
+               if (i == 0 || end_of_area + 1 == argv[i])
+                       end_of_area = argv[i] + strlen(argv[i]);
+       }
+
+       if (end_of_area == NULL)        /* probably can't happen? */
        {
-               char *end_of_area = NULL;
-               char **new_environ;
-               int i;
-
-               /*
-                * check for contiguous argv strings
-                */
-               for (i = 0; i < argc; i++)
-               {
-                       if (i == 0 || end_of_area + 1 == argv[i])
-                               end_of_area = argv[i] + strlen(argv[i]);
-               }
-
-               if (end_of_area == NULL)        /* probably can't happen? */
-               {
-                       ps_buffer = NULL;
-                       ps_buffer_size = 0;
-                       return argv;
-               }
-
-               /*
-                * check for contiguous environ strings following argv
-                */
-               for (i = 0; environ[i] != NULL; i++)
-               {
-                       if (end_of_area + 1 == environ[i])
-                               end_of_area = environ[i] + strlen(environ[i]);
-               }
-
-               ps_buffer = argv[0];
-               ps_buffer_size = end_of_area - argv[0];
-
-               /*
-                * move the environment out of the way
-                */
-               new_environ = (char **) malloc((i + 1) * sizeof(char *));
-               for (i = 0; environ[i] != NULL; i++)
-                       new_environ[i] = mowgli_strdup(environ[i]);
-               new_environ[i] = NULL;
-               environ = new_environ;
+               ps_buffer = NULL;
+               ps_buffer_size = 0;
+               return argv;
        }
+
+       /*
+        * check for contiguous environ strings following argv
+        */
+       for (i = 0; environ[i] != NULL; i++)
+       {
+               if (end_of_area + 1 == environ[i])
+                       end_of_area = environ[i] + strlen(environ[i]);
+       }
+
+       ps_buffer = argv[0];
+       ps_buffer_size = end_of_area - argv[0];
+
+       /*
+        * move the environment out of the way
+        */
+       new_environ = (char **) malloc((i + 1) * sizeof(char *));
+       for (i = 0; environ[i] != NULL; i++)
+               new_environ[i] = mowgli_strdup(environ[i]);
+       new_environ[i] = NULL;
+       environ = new_environ;
+
 #endif   /* MOWGLI_SETPROC_USE_CLOBBER_ARGV */
 
 #if defined(MOWGLI_SETPROC_USE_CHANGE_ARGV) || defined(MOWGLI_SETPROC_USE_CLOBBER_ARGV)
@@ -187,25 +186,25 @@ mowgli_proctitle_init(int argc, char **argv)
         * argument string if the argv storage has been clobbered meanwhile. Other
         * platforms have other dependencies on argv[].
         */
-       {
-               char **new_argv;
-               int i;
 
-               new_argv = (char **) malloc((argc + 1) * sizeof(char *));
-               for (i = 0; i < argc; i++)
-                       new_argv[i] = mowgli_strdup(argv[i]);
-               new_argv[argc] = NULL;
+       char **new_argv;
+       int i;
+
+       new_argv = (char **) malloc((argc + 1) * sizeof(char *));
+       for (i = 0; i < argc; i++)
+               new_argv[i] = mowgli_strdup(argv[i]);
+       new_argv[argc] = NULL;
 
 #if defined(__darwin__)
-               /*
-                * Darwin (and perhaps other NeXT-derived platforms?) has a static
-                * copy of the argv pointer, which we may fix like so:
-                */
-               *_NSGetArgv() = new_argv;
+       /*
+        * Darwin (and perhaps other NeXT-derived platforms?) has a static
+        * copy of the argv pointer, which we may fix like so:
+        */
+       *_NSGetArgv() = new_argv;
 #endif
 
-               argv = new_argv;
-       }
+       argv = new_argv;
+
 #endif   /* MOWGLI_SETPROC_USE_CHANGE_ARGV or MOWGLI_SETPROC_USE_CLOBBER_ARGV */
 
        return argv;
@@ -226,6 +225,8 @@ mowgli_proctitle_set(const char *fmt, ...)
        vsnprintf(ps_buffer, ps_buffer_size, fmt, va);
        va_end(va);
 
+       return_if_fail(*ps_buffer == NULL);
+
        ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
 
 #ifdef MOWGLI_SETPROC_USE_CHANGE_ARGV
@@ -248,7 +249,7 @@ mowgli_proctitle_set(const char *fmt, ...)
 
 #ifdef MOWGLI_SETPROC_USE_PRCTL
        /* Limit us to 16 chars to be safe */
-       char procbuf[17];
+       char procbuf[16];
        mowgli_strlcpy(procbuf, ps_buffer, sizeof(procbuf));
        prctl(PR_SET_NAME, procbuf, 0, 0, 0);
        printf("%s\n", procbuf);