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
20 #include <sys/types.h>
21 #include <sys/param.h>
23 #include <sys/sysctl.h>
26 #include <vm/vm_param.h>
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.
43 struct old_ps_strings {
49 #define OLD_PS_STRINGS ((struct old_ps_strings *) \
50 (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
52 #if defined(__STDC__) /* from other parts of sendmail */
59 #define SPT_BUFSIZE 2048 /* from other parts of sendmail */
60 extern char * __progname; /* is this defined in a .h anywhere? */
62 static struct ps_strings *ps_strings;
66 setproctitle(const char *fmt, ...)
68 setproctitle(fmt, va_alist)
73 static char buf[SPT_BUFSIZE];
74 static char *ps_argv[2];
77 unsigned long ul_ps_strings;
86 buf[sizeof(buf) - 1] = '\0';
89 /* print program name heading for grep */
90 (void) snprintf(buf, sizeof(buf) - 1, "%s: ", __progname);
93 * can't use return from sprintf, as that is the count of how
94 * much it wanted to write, not how much it actually did.
99 /* print the argument string */
100 (void) vsnprintf(buf + len, sizeof(buf) - 1 - len, fmt, ap);
102 /* Idea from NetBSD - reset the title on fmt == NULL */
103 strncpy(buf, __progname, sizeof(buf) - 1);
108 /* Set the title into the kernel cached command line */
111 oid[2] = KERN_PROC_ARGS;
113 sysctl(oid, 4, 0, 0, buf, strlen(buf) + 1);
115 if (ps_strings == NULL) {
116 len = sizeof(ul_ps_strings);
117 if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
119 ul_ps_strings = PS_STRINGS;
120 ps_strings = (struct ps_strings *)ul_ps_strings;
123 /* PS_STRINGS points to zeroed memory on a style #2 kernel */
124 if (ps_strings->ps_argvstr) {
128 ps_strings->ps_nargvstr = 1;
129 ps_strings->ps_argvstr = ps_argv;
132 OLD_PS_STRINGS->old_ps_nargvstr = 1;
133 OLD_PS_STRINGS->old_ps_argvstr = buf;