chiark / gitweb /
abolish xk_Malloc
[inn-innduct.git] / lib / setproctitle.c
1 /*  $Id: setproctitle.c 5943 2002-12-08 02:28:06Z rra $
2 **
3 **  Replacement for a missing setproctitle.
4 **
5 **  Provides the same functionality as the BSD function setproctitle on hosts
6 **  where modifying argv will produce those results, or on HP-UX (which has
7 **  its own peculiar way of doing this).  This may be ineffective on some
8 **  platforms.
9 **
10 **  Before calling setproctitle, it is *required* that setproctitle_init be
11 **  called, passing it argc and argv as arguments.  setproctitle_init will be
12 **  stubbed out on those platforms that don't need it.
13 */
14
15 #include "config.h"
16 #include "clibrary.h"
17 #include "portable/setproctitle.h"
18
19 #include "inn/messages.h"
20
21 #if HAVE_PSTAT
22
23 #include <sys/param.h>
24 #include <sys/pstat.h>
25
26 void
27 setproctitle(const char *format, ...)
28 {
29     va_list args;
30     char title[BUFSIZ];
31     union pstun un;
32     ssize_t delta = 0;
33
34     if (message_program_name != NULL) {
35         delta = snprintf(title, sizeof(title), "%s: ", message_program_name);
36         if (delta < 0)
37             delta = 0;
38     }
39     va_start(args, format);
40     vsnprintf(title + delta, sizeof(title) - delta, format, args);
41     va_end(args);
42     un.pst_command = title;
43     pstat(PSTAT_SETCMD, un, strlen(title), 0, 0);
44 }
45
46 #else
47
48 static char *title_start = NULL;
49 static char *title_end = NULL;
50
51 void
52 setproctitle_init(int argc, char *argv[])
53 {
54     title_start = argv[0];
55     title_end = argv[argc - 1] + strlen(argv[argc - 1]) - 1;
56 }
57
58 void
59 setproctitle(const char *format, ...)
60 {
61     va_list args;
62     size_t length;
63     ssize_t delta;
64     char *title;
65
66     if (title_start == NULL || title_end == NULL) {
67         warn("setproctitle called without setproctitle_init");
68         return;
69     }
70
71     /* setproctitle prepends the program name to its arguments.  Our emulation
72        should therefore do the same thing.  However, some operating systems
73        seem to do that automatically even when we completely overwrite argv,
74        so start our title with a - so that they'll instead put (nnrpd) at the
75        end, thinking we're swapped out. */
76     title = title_start;
77     *title++ = '-';
78     *title++ = ' ';
79     length = title_end - title_start - 2;
80
81     /* Now, put in the actual content.  Get the program name from
82        message_program_name if it's set. */
83     if (message_program_name != NULL) {
84         delta = snprintf(title, length, "%s: ", message_program_name);
85         if (delta < 0 || (size_t) delta > length)
86             return;
87         if (delta > 0) {
88             title += delta;
89             length -= delta;
90         }
91     }
92     va_start(args, format);
93     delta = vsnprintf(title, length, format, args);
94     va_end(args);
95     if (delta < 0 || (size_t) delta > length)
96         return;
97     if (delta > 0) {
98         title += delta;
99         length -= delta;
100     }
101     for (; length > 1; length--, title++)
102         *title = ' ';
103     *title = '\0';
104 }
105
106 #endif /* !HAVE_PSTAT */