setproctitle.c revision 17141
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 * $Id: setproctitle.c,v 1.2 1996/02/24 14:37:29 peter Exp $
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
33/*
34 * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and
35 * in different locations.
36 * 1: old_ps_strings at the very top of the stack.
37 * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack.
38 * 3: ps_strings at the very top of the stack.
39 * This attempts to support a kernel built in the #2 and #3 era.
40 */
41
42struct old_ps_strings {
43	char	*old_ps_argvstr;
44	int	old_ps_nargvstr;
45	char	*old_ps_envstr;
46	int	old_ps_nenvstr;
47};
48#define	OLD_PS_STRINGS ((struct old_ps_strings *) \
49	(USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
50
51#if defined(__STDC__)		/* from other parts of sendmail */
52#include <stdarg.h>
53#else
54#include <varargs.h>
55#endif
56
57
58#define SPT_BUFSIZE 2048	/* from other parts of sendmail */
59extern char * __progname;	/* is this defined in a .h anywhere? */
60
61void
62#if defined(__STDC__)
63setproctitle(const char *fmt, ...)
64#else
65setproctitle(fmt, va_alist)
66	const char *fmt;
67	va_dcl
68#endif
69{
70	static char buf[SPT_BUFSIZE];
71	static char *ps_argv[2];
72	va_list ap;
73	int mib[2];
74	struct ps_strings *ps_strings;
75	size_t len;
76
77#if defined(__STDC__)
78	va_start(ap, fmt);
79#else
80	va_start(ap);
81#endif
82
83	buf[sizeof(buf) - 1] = '\0';
84	if (fmt) {
85
86		/* print program name heading for grep */
87		(void) snprintf(buf, sizeof(buf) - 1, "%s: ", __progname);
88
89		/*
90		 * can't use return from sprintf, as that is the count of how
91		 * much it wanted to write, not how much it actually did.
92		 */
93
94		len = strlen(buf);
95
96		/* print the argument string */
97		(void) vsnprintf(buf + len, sizeof(buf) - 1 - len, fmt, ap);
98	} else {
99		/* Idea from NetBSD - reset the title on fmt == NULL */
100		strncpy(buf, __progname, sizeof(buf) - 1);
101	}
102
103	va_end(ap);
104
105	ps_strings = NULL;
106	mib[0] = CTL_KERN;
107	mib[1] = KERN_PS_STRINGS;
108	len = sizeof(ps_strings);
109	if (sysctl(mib, 2, &ps_strings, &len, NULL, 0) < 0 ||
110	    ps_strings == NULL)
111		ps_strings = PS_STRINGS;
112
113	/* PS_STRINGS points to zeroed memory on a style #2 kernel */
114	if (ps_strings->ps_argvstr) {
115		/* style #3 */
116		ps_argv[0] = buf;
117		ps_argv[1] = NULL;
118		ps_strings->ps_nargvstr = 1;
119		ps_strings->ps_argvstr = ps_argv;
120	} else {
121		/* style #2 */
122		OLD_PS_STRINGS->old_ps_nargvstr = 1;
123		OLD_PS_STRINGS->old_ps_argvstr = buf;
124	}
125}
126