1 /* $Id: setproctitle.c 5943 2002-12-08 02:28:06Z rra $
3 ** Replacement for a missing setproctitle.
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
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.
17 #include "portable/setproctitle.h"
19 #include "inn/messages.h"
23 #include <sys/param.h>
24 #include <sys/pstat.h>
27 setproctitle(const char *format, ...)
34 if (message_program_name != NULL) {
35 delta = snprintf(title, sizeof(title), "%s: ", message_program_name);
39 va_start(args, format);
40 vsnprintf(title + delta, sizeof(title) - delta, format, args);
42 un.pst_command = title;
43 pstat(PSTAT_SETCMD, un, strlen(title), 0, 0);
48 static char *title_start = NULL;
49 static char *title_end = NULL;
52 setproctitle_init(int argc, char *argv[])
54 title_start = argv[0];
55 title_end = argv[argc - 1] + strlen(argv[argc - 1]) - 1;
59 setproctitle(const char *format, ...)
66 if (title_start == NULL || title_end == NULL) {
67 warn("setproctitle called without setproctitle_init");
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. */
79 length = title_end - title_start - 2;
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)
92 va_start(args, format);
93 delta = vsnprintf(title, length, format, args);
95 if (delta < 0 || (size_t) delta > length)
101 for (; length > 1; length--, title++)
106 #endif /* !HAVE_PSTAT */