setproctitle.c revision 13039
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. This work was done expressly for inclusion into FreeBSD.  Other use
15 *    is permitted provided this notation is included.
16 * 4. Absolutely no warranty of function or purpose is made by the author
17 *    Peter Wemm.
18 * 5. Modifications may be freely made to this file providing the above
19 *    conditions are met.
20 *
21 * $Id$
22 */
23
24#include <sys/types.h>
25#include <sys/param.h>
26#include <sys/exec.h>
27
28#include <vm/vm.h>
29#include <vm/vm_param.h>
30#include <vm/pmap.h>
31
32#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35
36/*
37 * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and
38 * in different locations.
39 * 1: old_ps_strings at the very top of the stack.
40 * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack.
41 * 3: ps_strings at the very top of the stack.
42 * This attempts to support a kernel built in the #2 and #3 era.
43 */
44
45struct old_ps_strings {
46	char	*old_ps_argvstr;
47	int	old_ps_nargvstr;
48	char	*old_ps_envstr;
49	int	old_ps_nenvstr;
50};
51#define	OLD_PS_STRINGS ((struct old_ps_strings *) \
52	(USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
53
54#if defined(__STDC__)		/* from other parts of sendmail */
55#include <stdarg.h>
56#else
57#include <varargs.h>
58#endif
59
60
61#define SPT_BUFSIZE 2048	/* from other parts of sendmail */
62extern char * __progname;	/* is this defined in a .h anywhere? */
63
64void
65#if defined(__STDC__)
66setproctitle(const char *fmt, ...)
67#else
68setproctitle(fmt, va_alist)
69	const char *fmt;
70	va_dcl
71#endif
72{
73	char *p;
74	int len;
75	static char buf[SPT_BUFSIZE];
76	static char *ps_argv[2];
77	va_list ap;
78
79#if defined(__STDC__)
80	va_start(ap, fmt);
81#else
82	va_start(ap);
83#endif
84
85	buf[sizeof(buf) - 1] = '\0';
86	if (fmt) {
87
88		/* print program name heading for grep */
89		(void) snprintf(buf, sizeof(buf) - 1, "%s: ", __progname);
90
91		/*
92		 * can't use return from sprintf, as that is the count of how
93		 * much it wanted to write, not how much it actually did.
94		 */
95
96		len = strlen(buf);
97
98		/* print the argument string */
99		(void) vsnprintf(buf + len, sizeof(buf) - 1 - len, fmt, ap);
100	} else {
101		/* Idea from NetBSD - reset the title on fmt == NULL */
102		strncpy(buf, __progname, sizeof(buf) - 1);
103	}
104
105	va_end(ap);
106
107	/* PS_STRINGS points to zeroed memory on a style #2 kernel */
108	if (PS_STRINGS->ps_argvstr) {
109		/* style #3 */
110		ps_argv[0] = buf;
111		ps_argv[1] = NULL;
112		PS_STRINGS->ps_nargvstr = 1;
113		PS_STRINGS->ps_argvstr = ps_argv;
114	} else {
115		/* style #2 */
116		OLD_PS_STRINGS->old_ps_nargvstr = 1;
117		OLD_PS_STRINGS->old_ps_argvstr = buf;
118	}
119}
120