2 * Copyright (c) 1995 Peter Wemm <peter@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, is permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Absolutely no warranty of function or purpose is made by the author
17 * $FreeBSD: src/lib/libc/gen/setproctitle.c,v 1.18 2003/07/01 09:45:35 alfred Exp $
18 * $DragonFly: src/lib/libc/gen/setproctitle.c,v 1.5 2005/11/13 00:07:42 swildner Exp $
21 #include "namespace.h"
22 #include <sys/types.h>
23 #include <sys/param.h>
25 #include <sys/sysctl.h>
28 #include <vm/vm_param.h>
35 #include "un-namespace.h"
37 #include "libc_private.h"
40 * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and
41 * in different locations.
42 * 1: old_ps_strings at the very top of the stack.
43 * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack.
44 * 3: ps_strings at the very top of the stack.
45 * This attempts to support a kernel built in the #2 and #3 era.
48 struct old_ps_strings
{
54 #define OLD_PS_STRINGS ((struct old_ps_strings *) \
55 (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
59 #define SPT_BUFSIZE 2048 /* from other parts of sendmail */
62 setproctitle(const char *fmt
, ...)
64 static struct ps_strings
*ps_strings
;
65 static char *buf
= NULL
;
66 static char *obuf
= NULL
;
67 static char **oargv
, *kbuf
;
68 static int oargc
= -1;
69 static char *nargv
[2] = { NULL
, NULL
};
75 unsigned long ul_ps_strings
;
79 buf
= malloc(SPT_BUFSIZE
);
86 obuf
= malloc(SPT_BUFSIZE
);
95 buf
[SPT_BUFSIZE
- 1] = '\0';
98 /* skip program name prefix */
102 /* print program name heading for grep */
103 snprintf(buf
, SPT_BUFSIZE
, "%s: ", _getprogname());
107 /* print the argument string */
108 vsnprintf(buf
+ len
, SPT_BUFSIZE
- len
, fmt
, ap
);
113 } else if (*obuf
!= '\0') {
114 /* Idea from NetBSD - reset the title on fmt == NULL */
119 /* Nothing to restore */
124 /* Set the title into the kernel cached command line */
127 oid
[2] = KERN_PROC_ARGS
;
129 sysctl(oid
, 4, 0, 0, kbuf
, strlen(kbuf
) + 1);
131 if (ps_strings
== NULL
) {
132 len
= sizeof(ul_ps_strings
);
133 if (sysctlbyname("kern.ps_strings", &ul_ps_strings
, &len
, NULL
,
135 ul_ps_strings
= PS_STRINGS
;
136 ps_strings
= (struct ps_strings
*)ul_ps_strings
;
139 /* PS_STRINGS points to zeroed memory on a style #2 kernel */
140 if (ps_strings
->ps_argvstr
) {
143 /* Record our original args */
144 oargc
= ps_strings
->ps_nargvstr
;
145 oargv
= ps_strings
->ps_argvstr
;
146 for (i
= len
= 0; i
< oargc
; i
++) {
148 * The program may have scribbled into its
149 * argv array, e.g., to remove some arguments.
150 * If that has happened, break out before
151 * trying to call strlen on a NULL pointer.
153 if (oargv
[i
] == NULL
) {
157 snprintf(obuf
+ len
, SPT_BUFSIZE
- len
, "%s%s",
158 len
? " " : "", oargv
[i
]);
161 len
+= strlen(oargv
[i
]);
162 if (len
>= SPT_BUFSIZE
)
166 ps_strings
->ps_nargvstr
= nargc
;
167 ps_strings
->ps_argvstr
= nargvp
;
169 /* style #2 - we can only restore our first arg :-( */
171 strncpy(obuf
, OLD_PS_STRINGS
->old_ps_argvstr
,
173 OLD_PS_STRINGS
->old_ps_nargvstr
= 1;
174 OLD_PS_STRINGS
->old_ps_argvstr
= nargvp
[0];