~gandelman-a/ubuntu/oneiric/openbsd-inetd/merge

« back to all changes in this revision

Viewing changes to setproctitle.c

  • Committer: Bazaar Package Importer
  • Author(s): Marco d'Itri
  • Date: 2002-08-20 15:51:39 UTC
  • Revision ID: james.westby@ubuntu.com-20020820155139-y1shmgf3ts6hcxrd
Tags: 0.20020802-1
* New package.
* Pre/postinstall scripts borrowed from aj's netkit-inetd package.
* This package fixes many bugs in netkit-inetd, among them:
  #10813, #32579, #55052, #66752, #143539, #143815, #143816, #125181,
  #45907, #82241, #96544, #110673.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * setproctitle implementation for linux.
 
3
 * Stolen from sendmail 8.7.4 and bashed around by David A. Holland
 
4
 */
 
5
 
 
6
/*
 
7
 * Copyright (c) 1983, 1995 Eric P. Allman
 
8
 * Copyright (c) 1988, 1993
 
9
 *      The Regents of the University of California.  All rights reserved.
 
10
 *
 
11
 * Redistribution and use in source and binary forms, with or without
 
12
 * modification, are permitted provided that the following conditions
 
13
 * are met:
 
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.
 
26
 *
 
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
 
37
 * SUCH DAMAGE.
 
38
 *
 
39
 * From: @(#)conf.c     8.243 (Berkeley) 11/20/95
 
40
 */
 
41
char setproctitle_rcsid[] =
 
42
  "$Id: setproctitle.c,v 1.3 1997/05/19 12:58:15 dholland Exp $";
 
43
 
 
44
#include <stdlib.h>
 
45
#include <string.h>
 
46
#include <stdarg.h>
 
47
#include <unistd.h>
 
48
#include <stdio.h>
 
49
 
 
50
#include "setproctitle.h"
 
51
/*
 
52
**  SETPROCTITLE -- set process title for ps
 
53
**
 
54
**      Parameters:
 
55
**              fmt -- a printf style format string.
 
56
**              a, b, c -- possible parameters to fmt.
 
57
**
 
58
**      Returns:
 
59
**              none.
 
60
**
 
61
**      Side Effects:
 
62
**              Clobbers argv of our main procedure so ps(1) will
 
63
**              display the title.
 
64
*/
 
65
 
 
66
 
 
67
/*
 
68
**  Pointers for setproctitle.
 
69
**      This allows "ps" listings to give more useful information.
 
70
*/
 
71
 
 
72
static char **Argv = NULL;              /* pointer to argument vector */
 
73
static char *LastArgv = NULL;           /* end of argv */
 
74
static char Argv0[128];                 /* program name */
 
75
 
 
76
void
 
77
initsetproctitle(int argc, char **argv, char **envp)
 
78
{
 
79
        register int i;
 
80
        char *tmp;
 
81
 
 
82
        /*
 
83
        **  Move the environment so setproctitle can use the space at
 
84
        **  the top of memory.
 
85
        */
 
86
 
 
87
        for (i = 0; envp[i] != NULL; i++)
 
88
                continue;
 
89
        __environ = (char **) malloc(sizeof (char *) * (i + 1));
 
90
        for (i = 0; envp[i] != NULL; i++)
 
91
                __environ[i] = strdup(envp[i]);
 
92
        __environ[i] = NULL;
 
93
 
 
94
        /*
 
95
        **  Save start and extent of argv for setproctitle.
 
96
        */
 
97
 
 
98
        Argv = argv;
 
99
        if (i > 0)
 
100
                LastArgv = envp[i - 1] + strlen(envp[i - 1]);
 
101
        else
 
102
                LastArgv = argv[argc - 1] + strlen(argv[argc - 1]);
 
103
 
 
104
        tmp = strrchr(argv[0], '/');
 
105
        if (!tmp) tmp = argv[0];
 
106
        else tmp++;
 
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;
 
110
}
 
111
 
 
112
void
 
113
setproctitle(const char *fmt, ...)
 
114
{
 
115
        register char *p;
 
116
        register int i;
 
117
        static char buf[2048];
 
118
        va_list ap;
 
119
 
 
120
        p = buf;
 
121
 
 
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, ": ");
 
126
        p += strlen(p);
 
127
 
 
128
        /* print the argument string */
 
129
        va_start(ap, fmt);
 
130
        (void) vsnprintf(p, sizeof(buf) - (p - buf), fmt, ap);
 
131
        va_end(ap);
 
132
 
 
133
        i = strlen(buf);
 
134
 
 
135
        if (i > LastArgv - Argv[0] - 2)
 
136
        {
 
137
                i = LastArgv - Argv[0] - 2;
 
138
                buf[i] = '\0';
 
139
        }
 
140
        (void) strcpy(Argv[0], buf);
 
141
        p = &Argv[0][i];
 
142
        while (p < LastArgv)
 
143
                *p++ = ' ';
 
144
        Argv[1] = NULL;
 
145
}
 
146