]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libutil/setproctitle.c
This commit was generated by cvs2svn to compensate for changes in r56639,
[FreeBSD/FreeBSD.git] / lib / libutil / setproctitle.c
1 /*
2  * Copyright (c) 1995 Peter Wemm <peter@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, is permitted provided that the following conditions
7  * are met:
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
15  *    Peter Wemm.
16  *
17  * $FreeBSD$
18  */
19
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/exec.h>
23 #include <sys/sysctl.h>
24
25 #include <vm/vm.h>
26 #include <vm/vm_param.h>
27 #include <vm/pmap.h>
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 /*
35  * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and
36  * in different locations.
37  * 1: old_ps_strings at the very top of the stack.
38  * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack.
39  * 3: ps_strings at the very top of the stack.
40  * This attempts to support a kernel built in the #2 and #3 era.
41  */
42
43 struct old_ps_strings {
44         char    *old_ps_argvstr;
45         int     old_ps_nargvstr;
46         char    *old_ps_envstr;
47         int     old_ps_nenvstr;
48 };
49 #define OLD_PS_STRINGS ((struct old_ps_strings *) \
50         (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
51
52 #if defined(__STDC__)           /* from other parts of sendmail */
53 #include <stdarg.h>
54 #else
55 #include <varargs.h>
56 #endif
57
58
59 #define SPT_BUFSIZE 2048        /* from other parts of sendmail */
60 extern char * __progname;       /* is this defined in a .h anywhere? */
61
62 void
63 #if defined(__STDC__)
64 setproctitle(const char *fmt, ...)
65 #else
66 setproctitle(fmt, va_alist)
67         const char *fmt;
68         va_dcl
69 #endif
70 {
71         static struct ps_strings *ps_strings;
72         static char buf[SPT_BUFSIZE];
73         static char obuf[SPT_BUFSIZE];
74         static char **oargv, *kbuf;
75         static int oargc = -1;
76         static char *nargv[2] = { buf, NULL };
77         char **nargvp;
78         int nargc;
79         va_list ap;
80         size_t len;
81         unsigned long ul_ps_strings;
82         int oid[4];
83
84 #if defined(__STDC__)
85         va_start(ap, fmt);
86 #else
87         va_start(ap);
88 #endif
89
90         if (fmt) {
91                 buf[sizeof(buf) - 1] = '\0';
92
93                 /* print program name heading for grep */
94                 (void) snprintf(buf, sizeof(buf), "%s: ", __progname);
95
96                 /*
97                  * can't use return from sprintf, as that is the count of how
98                  * much it wanted to write, not how much it actually did.
99                  */
100
101                 len = strlen(buf);
102
103                 /* print the argument string */
104                 (void) vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
105
106                 nargvp = nargv;
107                 nargc = 1;
108                 kbuf = buf;
109         } else if (*obuf != '\0') {
110                 /* Idea from NetBSD - reset the title on fmt == NULL */
111                 nargvp = oargv;
112                 nargc = oargc;
113                 kbuf = obuf;
114         } else
115                 /* Nothing to restore */
116                 return;
117
118         va_end(ap);
119
120         /* Set the title into the kernel cached command line */
121         oid[0] = CTL_KERN;
122         oid[1] = KERN_PROC;
123         oid[2] = KERN_PROC_ARGS;
124         oid[3] = getpid();
125         sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1);
126
127         if (ps_strings == NULL) {
128                 len = sizeof(ul_ps_strings);
129                 if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
130                     0) == -1)
131                         ul_ps_strings = PS_STRINGS;
132                 ps_strings = (struct ps_strings *)ul_ps_strings;
133         }
134
135         /* PS_STRINGS points to zeroed memory on a style #2 kernel */
136         if (ps_strings->ps_argvstr) {
137                 /* style #3 */
138                 if (oargc == -1) {
139                         /* Record our original args */
140                         oargc = ps_strings->ps_nargvstr;
141                         oargv = ps_strings->ps_argvstr;
142                         for (nargc = len = 0; nargc < oargc; nargc++) {
143                                 snprintf(obuf + len, sizeof(obuf) - len, "%s%s",
144                                     len ? " " : "", oargv[nargc]);
145                                 if (len)
146                                         len++;
147                                 len += strlen(oargv[nargc]);
148                                 if (len >= sizeof(obuf))
149                                         break;
150                         }
151                 }
152                 ps_strings->ps_nargvstr = nargc;
153                 ps_strings->ps_argvstr = nargvp;
154         } else {
155                 /* style #2 - we can only restore our first arg :-( */
156                 if (*obuf == '\0')
157                         strncpy(obuf, OLD_PS_STRINGS->old_ps_argvstr,
158                             sizeof(obuf) - 1);
159                 OLD_PS_STRINGS->old_ps_nargvstr = 1;
160                 OLD_PS_STRINGS->old_ps_argvstr = nargvp[0];
161         }
162 }