2
* setproctitle implementation for linux.
3
* Stolen from sendmail 8.7.4 and bashed around by David A. Holland
7
* Copyright (c) 1983, 1995 Eric P. Allman
8
* Copyright (c) 1988, 1993
9
* The Regents of the University of California. All rights reserved.
11
* Redistribution and use in source and binary forms, with or without
12
* modification, are permitted provided that the following conditions
14
* 1. Redistributions of source code must retain the above copyright
15
* notice, this list of conditions and the following disclaimer.
16
* 2. Redistributions in binary form must reproduce the above copyright
17
* notice, this list of conditions and the following disclaimer in the
18
* documentation and/or other materials provided with the distribution.
19
* 3. All advertising materials mentioning features or use of this software
20
* must display the following acknowledgement:
21
* This product includes software developed by the University of
22
* California, Berkeley and its contributors.
23
* 4. Neither the name of the University nor the names of its contributors
24
* may be used to endorse or promote products derived from this software
25
* without specific prior written permission.
27
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39
* From: @(#)conf.c 8.243 (Berkeley) 11/20/95
41
char setproctitle_rcsid[] =
42
"$Id: setproctitle.c,v 1.3 1997/05/19 12:58:15 dholland Exp $";
50
#include "setproctitle.h"
52
** SETPROCTITLE -- set process title for ps
55
** fmt -- a printf style format string.
56
** a, b, c -- possible parameters to fmt.
62
** Clobbers argv of our main procedure so ps(1) will
68
** Pointers for setproctitle.
69
** This allows "ps" listings to give more useful information.
72
static char **Argv = NULL; /* pointer to argument vector */
73
static char *LastArgv = NULL; /* end of argv */
74
static char Argv0[128]; /* program name */
77
initsetproctitle(int argc, char **argv, char **envp)
83
** Move the environment so setproctitle can use the space at
87
for (i = 0; envp[i] != NULL; i++)
89
__environ = (char **) malloc(sizeof (char *) * (i + 1));
90
for (i = 0; envp[i] != NULL; i++)
91
__environ[i] = strdup(envp[i]);
95
** Save start and extent of argv for setproctitle.
100
LastArgv = envp[i - 1] + strlen(envp[i - 1]);
102
LastArgv = argv[argc - 1] + strlen(argv[argc - 1]);
104
tmp = strrchr(argv[0], '/');
105
if (!tmp) tmp = argv[0];
107
strncpy(Argv0, tmp, sizeof(Argv0));
108
/* remember to take away one or we go outside the array space */
109
Argv0[sizeof(Argv0) - 1] = 0;
113
setproctitle(const char *fmt, ...)
117
static char buf[2048];
122
/* print progname: heading for grep */
123
/* This can't overflow buf due to the relative size of Argv0. */
124
(void) strcpy(p, Argv0);
125
(void) strcat(p, ": ");
128
/* print the argument string */
130
(void) vsnprintf(p, sizeof(buf) - (p - buf), fmt, ap);
135
if (i > LastArgv - Argv[0] - 2)
137
i = LastArgv - Argv[0] - 2;
140
(void) strcpy(Argv[0], buf);