File: setproctitle.c

package info (click to toggle)
pmacct 1.7.6-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,548 kB
  • sloc: ansic: 106,538; sh: 4,876; cpp: 4,340; python: 3,631; makefile: 502
file content (232 lines) | stat: -rw-r--r-- 5,473 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * setproctitle()-related routines in this file are derived from Sendmail
 * 8.13.5 which is:
 *        
 * Copyright (c) 1998-2005 Sendmail, Inc. and its suppliers.
 *      All rights reserved.
 * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
 * Copyright (c) 1988, 1993
 *      The Regents of the University of California.  All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 *
 */

#include "pmacct.h"

/*
 *  SETPROCTITLE -- set process title for ps
*/

#define SPT_NONE	0	/* don't use it at all */
#define SPT_REUSEARGV	1	/* cover argv with title information */
#define SPT_BUILTIN	2	/* use libc builtin */
#define SPT_PSTAT	3	/* use pstat(PSTAT_SETCMD, ...) */

#include "setproctitle.h"

#define MAXLINE (2*LONGLONGSRVBUFLEN)
#define SPACELEFT(x) (sizeof(x)-strlen(x))

#if defined PROGNAME && SPT_TYPE == SPT_REUSEARGV
extern char *__progname;
#endif

/*
 * NEWSTR -- Create a copy of a C string
 */

char *spt_newstr(s)
const char *s;
{
  size_t l;
  char *n;
  
  l = strlen(s);
  n = malloc(l + 1);
  strlcpy(n, s, l + 1);

  return n;
}

#ifndef SPT_TYPE
#  define SPT_TYPE	SPT_NONE
#endif /* ! SPT_TYPE */


#if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN

#if SPT_TYPE == SPT_PSTAT
#  include <sys/pstat.h>
#endif /* SPT_TYPE == SPT_PSTAT */

#ifndef SPT_PADCHAR
#  define SPT_PADCHAR	' '
#endif /* ! SPT_PADCHAR */

#endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */

#ifndef SPT_BUFSIZE
#  define SPT_BUFSIZE	MAXLINE
#endif /* ! SPT_BUFSIZE */

/*
 *  Pointers for setproctitle.
 *	This allows "ps" listings to give more useful information.
 */

static char	**Argv = NULL;		/* pointer to argument vector */
static char	*LastArgv = NULL;	/* end of argv */

void
initsetproctitle(argc, argv, envp)
	int argc;
	char **argv;
	char **envp;
{
	register int i;
	extern char **environ;

	/*
	**  Move the environment so setproctitle can use the space at
	**  the top of memory.
	*/

	if (envp != NULL)
	{
		for (i = 0; envp[i] != NULL; i++)
			continue;
		environ = (char **) malloc(sizeof (char *) * (i + 1));
		for (i = 0; envp[i] != NULL; i++)
			environ[i] = spt_newstr(envp[i]);
		environ[i] = NULL;
	}

	/*
	**  Save start and extent of argv for setproctitle.
	*/

	Argv = argv;

	/*
	**  Determine how much space we can use for setproctitle.
	**  Use all contiguous argv and envp pointers starting at argv[0]
	*/

	for (i = 0; i < argc; i++)
	{
		if (i == 0 || LastArgv + 1 == argv[i])
			LastArgv = argv[i] + strlen(argv[i]);
	}
	for (i = 0; LastArgv != NULL && envp != NULL && envp[i] != NULL; i++)
	{
		if (LastArgv + 1 == envp[i])
			LastArgv = envp[i] + strlen(envp[i]);
	}

#if defined PROGNAME && SPT_TYPE == SPT_REUSEARGV
	if (config.uacctd_group) __progname = uacctd_globstr; /* XXX: hack */
	else if (config.acct_type == ACCT_PM) __progname = pmacctd_globstr;
	else if (config.acct_type == ACCT_NF) __progname = nfacctd_globstr;
	else if (config.acct_type == ACCT_SF) __progname = sfacctd_globstr;
	else if (config.acct_type == ACCT_PMTELE) __progname = pmtele_globstr;
	else if (config.acct_type == ACCT_PMBGP) __progname = pmbgpd_globstr;
	else if (config.acct_type == ACCT_PMBMP) __progname = pmbmpd_globstr;
#endif
}

#if SPT_TYPE != SPT_BUILTIN

/*VARARGS1*/
static void
# ifdef __STDC__
setproctitle(const char *fmt, ...)
# else /* __STDC__ */
setproctitle(fmt, va_alist)
	const char *fmt;
	va_dcl
# endif /* __STDC__ */
{
# if SPT_TYPE != SPT_NONE
	register int i;
	register char *p;
	char buf[SPT_BUFSIZE];
	va_list ap;
#  if SPT_TYPE == SPT_PSTAT
	union pstun pst;
#  endif /* SPT_TYPE == SPT_PSTAT */

	memset(buf, 0, SPT_BUFSIZE);
	p = buf;
	va_start(ap, fmt);
	vsnprintf(p, SPACELEFT(buf), fmt, ap);
	va_end(ap);

	i = (int) strlen(buf);
	if (i < 0) return;

#  if SPT_TYPE == SPT_PSTAT
	pst.pst_command = buf;
	pstat(PSTAT_SETCMD, pst, i, 0, 0);
#  endif /* SPT_TYPE == SPT_PSTAT */
#  if SPT_TYPE == SPT_REUSEARGV
	if (LastArgv == NULL)
		return;

	if (i > (LastArgv - Argv[0]) - 2)
	{
		i = (LastArgv - Argv[0]) - 2;
		buf[i] = '\0';
	}
	(void) strlcpy(Argv[0], buf, i + 1);
	p = &Argv[0][i];
	while (p < LastArgv)
		*p++ = SPT_PADCHAR;
	Argv[1] = NULL;
#  endif /* SPT_TYPE == SPT_REUSEARGV */
# endif /* SPT_TYPE != SPT_NONE */
}

#endif /* SPT_TYPE != SPT_BUILTIN */
/*
 *  PM_SETPROCTITLE -- set process task and set process title for ps
 */

/*VARARGS2*/
void
#ifdef __STDC__
pm_setproctitle(const char *fmt, ...)
#else /* __STDC__ */
pm_setproctitle(fmt, va_alist)
  const char *fmt;
  va_dcl
#endif /* __STDC__ */
{
  char buf[SPT_BUFSIZE];
  char prefix[16];
  va_list ap;

  memset(prefix, 0, sizeof(prefix));
  memset(buf, 0, sizeof(buf));

  if (config.uacctd_group) strcpy(prefix, uacctd_globstr); /* XXX: hack */
  else if (config.acct_type == ACCT_PM) strcpy(prefix, pmacctd_globstr);
  else if (config.acct_type == ACCT_NF) strcpy(prefix, nfacctd_globstr);
  else if (config.acct_type == ACCT_SF) strcpy(prefix, sfacctd_globstr);
  else if (config.acct_type == ACCT_PMTELE) strcpy(prefix, pmtele_globstr);
  else if (config.acct_type == ACCT_PMBGP) strcpy(prefix, pmbgpd_globstr);
  else if (config.acct_type == ACCT_PMBMP) strcpy(prefix, pmbmpd_globstr);

  va_start(ap, fmt);
  vsnprintf(buf, sizeof(buf), fmt, ap);
  va_end(ap);

#if SPT_TYPE != SPT_BUILTIN
  setproctitle("%s: %s", prefix, buf);
#else
  setproctitle("%s", buf);
#endif
}