1*2fe8fb19SBen Gras /* $NetBSD: setproctitle.c,v 1.22 2008/01/03 04:26:27 christos Exp $ */
2*2fe8fb19SBen Gras
3*2fe8fb19SBen Gras /*
4*2fe8fb19SBen Gras * Copyright (c) 1994, 1995 Christopher G. Demetriou
5*2fe8fb19SBen Gras * All rights reserved.
6*2fe8fb19SBen Gras *
7*2fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
8*2fe8fb19SBen Gras * modification, are permitted provided that the following conditions
9*2fe8fb19SBen Gras * are met:
10*2fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
11*2fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
12*2fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
13*2fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
14*2fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
15*2fe8fb19SBen Gras * 3. All advertising materials mentioning features or use of this software
16*2fe8fb19SBen Gras * must display the following acknowledgement:
17*2fe8fb19SBen Gras * This product includes software developed for the
18*2fe8fb19SBen Gras * NetBSD Project. See http://www.NetBSD.org/ for
19*2fe8fb19SBen Gras * information about NetBSD.
20*2fe8fb19SBen Gras * 4. The name of the author may not be used to endorse or promote products
21*2fe8fb19SBen Gras * derived from this software without specific prior written permission.
22*2fe8fb19SBen Gras *
23*2fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24*2fe8fb19SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25*2fe8fb19SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26*2fe8fb19SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27*2fe8fb19SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28*2fe8fb19SBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29*2fe8fb19SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30*2fe8fb19SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31*2fe8fb19SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32*2fe8fb19SBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*2fe8fb19SBen Gras *
34*2fe8fb19SBen Gras * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35*2fe8fb19SBen Gras */
36*2fe8fb19SBen Gras
37*2fe8fb19SBen Gras #include <sys/cdefs.h>
38*2fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
39*2fe8fb19SBen Gras __RCSID("$NetBSD: setproctitle.c,v 1.22 2008/01/03 04:26:27 christos Exp $");
40*2fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
41*2fe8fb19SBen Gras
42*2fe8fb19SBen Gras #include "namespace.h"
43*2fe8fb19SBen Gras #include <sys/param.h>
44*2fe8fb19SBen Gras #include <sys/types.h>
45*2fe8fb19SBen Gras #include <sys/exec.h>
46*2fe8fb19SBen Gras #include <stdarg.h>
47*2fe8fb19SBen Gras #include <stdio.h>
48*2fe8fb19SBen Gras #include <stdlib.h>
49*2fe8fb19SBen Gras #include <string.h>
50*2fe8fb19SBen Gras
51*2fe8fb19SBen Gras #ifdef __weak_alias
52*2fe8fb19SBen Gras __weak_alias(setproctitle,_setproctitle)
53*2fe8fb19SBen Gras #endif
54*2fe8fb19SBen Gras
55*2fe8fb19SBen Gras #define MAX_PROCTITLE 2048
56*2fe8fb19SBen Gras
57*2fe8fb19SBen Gras /*
58*2fe8fb19SBen Gras * For compatibility with old versions of crt0 that didn't define __ps_strings,
59*2fe8fb19SBen Gras * define it as a common here.
60*2fe8fb19SBen Gras */
61*2fe8fb19SBen Gras struct ps_strings *__ps_strings;
62*2fe8fb19SBen Gras
63*2fe8fb19SBen Gras void
setproctitle(const char * fmt,...)64*2fe8fb19SBen Gras setproctitle(const char *fmt, ...)
65*2fe8fb19SBen Gras {
66*2fe8fb19SBen Gras static char buf[MAX_PROCTITLE], *bufp;
67*2fe8fb19SBen Gras const char *pname = getprogname();
68*2fe8fb19SBen Gras
69*2fe8fb19SBen Gras if (fmt != NULL) {
70*2fe8fb19SBen Gras int len = snprintf(buf, sizeof(buf), "%s: ", pname);
71*2fe8fb19SBen Gras if (len >= 0) {
72*2fe8fb19SBen Gras va_list ap;
73*2fe8fb19SBen Gras
74*2fe8fb19SBen Gras va_start(ap, fmt);
75*2fe8fb19SBen Gras (void)vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
76*2fe8fb19SBen Gras va_end(ap);
77*2fe8fb19SBen Gras }
78*2fe8fb19SBen Gras } else
79*2fe8fb19SBen Gras (void)snprintf(buf, sizeof(buf), "%s", pname);
80*2fe8fb19SBen Gras
81*2fe8fb19SBen Gras bufp = buf;
82*2fe8fb19SBen Gras
83*2fe8fb19SBen Gras #ifdef USRSTACK
84*2fe8fb19SBen Gras /*
85*2fe8fb19SBen Gras * For compatibility with old versions of crt0 and old kernels, set
86*2fe8fb19SBen Gras * __ps_strings to a default value if it's null.
87*2fe8fb19SBen Gras * But only if USRSTACK is defined. It might not be defined if
88*2fe8fb19SBen Gras * user-level code can not assume it's a constant (i.e. m68k).
89*2fe8fb19SBen Gras */
90*2fe8fb19SBen Gras if (__ps_strings == 0)
91*2fe8fb19SBen Gras __ps_strings = PS_STRINGS;
92*2fe8fb19SBen Gras #endif /* USRSTACK */
93*2fe8fb19SBen Gras
94*2fe8fb19SBen Gras if (__ps_strings != 0) {
95*2fe8fb19SBen Gras __ps_strings->ps_nargvstr = 1;
96*2fe8fb19SBen Gras __ps_strings->ps_argvstr = &bufp;
97*2fe8fb19SBen Gras }
98*2fe8fb19SBen Gras }
99