1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * Modified for OpenSSH by Kevin Steves
3*7c478bd9Sstevel@tonic-gate * October 2000
4*7c478bd9Sstevel@tonic-gate */
5*7c478bd9Sstevel@tonic-gate
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate * Copyright (c) 1994, 1995 Christopher G. Demetriou
8*7c478bd9Sstevel@tonic-gate * All rights reserved.
9*7c478bd9Sstevel@tonic-gate *
10*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
11*7c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
12*7c478bd9Sstevel@tonic-gate * are met:
13*7c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
14*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
15*7c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
16*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
17*7c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
18*7c478bd9Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
19*7c478bd9Sstevel@tonic-gate * must display the following acknowledgement:
20*7c478bd9Sstevel@tonic-gate * This product includes software developed by Christopher G. Demetriou
21*7c478bd9Sstevel@tonic-gate * for the NetBSD Project.
22*7c478bd9Sstevel@tonic-gate * 4. The name of the author may not be used to endorse or promote products
23*7c478bd9Sstevel@tonic-gate * derived from this software without specific prior written permission
24*7c478bd9Sstevel@tonic-gate *
25*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26*7c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27*7c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28*7c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29*7c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30*7c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31*7c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32*7c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33*7c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35*7c478bd9Sstevel@tonic-gate */
36*7c478bd9Sstevel@tonic-gate
37*7c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
38*7c478bd9Sstevel@tonic-gate static char rcsid[] = "$OpenBSD: setproctitle.c,v 1.8 2001/11/06 19:21:40 art Exp $";
39*7c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
40*7c478bd9Sstevel@tonic-gate
41*7c478bd9Sstevel@tonic-gate #include "includes.h"
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate #ifndef HAVE_SETPROCTITLE
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gate #define SPT_NONE 0
46*7c478bd9Sstevel@tonic-gate #define SPT_PSTAT 1
47*7c478bd9Sstevel@tonic-gate
48*7c478bd9Sstevel@tonic-gate #ifndef SPT_TYPE
49*7c478bd9Sstevel@tonic-gate #define SPT_TYPE SPT_NONE
50*7c478bd9Sstevel@tonic-gate #endif
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate #if SPT_TYPE == SPT_PSTAT
53*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/pstat.h>
55*7c478bd9Sstevel@tonic-gate #endif /* SPT_TYPE == SPT_PSTAT */
56*7c478bd9Sstevel@tonic-gate
57*7c478bd9Sstevel@tonic-gate #define MAX_PROCTITLE 2048
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate extern char *__progname;
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate * Set Process Title (SPT) defines. Modeled after sendmail's
63*7c478bd9Sstevel@tonic-gate * SPT type definition strategy.
64*7c478bd9Sstevel@tonic-gate *
65*7c478bd9Sstevel@tonic-gate * SPT_TYPE:
66*7c478bd9Sstevel@tonic-gate *
67*7c478bd9Sstevel@tonic-gate * SPT_NONE: Don't set the process title. Default.
68*7c478bd9Sstevel@tonic-gate * SPT_PSTAT: Use pstat(PSTAT_SETCMD). HP-UX specific.
69*7c478bd9Sstevel@tonic-gate */
70*7c478bd9Sstevel@tonic-gate
71*7c478bd9Sstevel@tonic-gate void
setproctitle(const char * fmt,...)72*7c478bd9Sstevel@tonic-gate setproctitle(const char *fmt, ...)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate #if SPT_TYPE != SPT_NONE
75*7c478bd9Sstevel@tonic-gate va_list ap;
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gate char buf[MAX_PROCTITLE];
78*7c478bd9Sstevel@tonic-gate size_t used;
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate #if SPT_TYPE == SPT_PSTAT
81*7c478bd9Sstevel@tonic-gate union pstun pst;
82*7c478bd9Sstevel@tonic-gate #endif /* SPT_TYPE == SPT_PSTAT */
83*7c478bd9Sstevel@tonic-gate
84*7c478bd9Sstevel@tonic-gate va_start(ap, fmt);
85*7c478bd9Sstevel@tonic-gate if (fmt != NULL) {
86*7c478bd9Sstevel@tonic-gate used = snprintf(buf, MAX_PROCTITLE, "%s: ", __progname);
87*7c478bd9Sstevel@tonic-gate if (used >= MAX_PROCTITLE)
88*7c478bd9Sstevel@tonic-gate used = MAX_PROCTITLE - 1;
89*7c478bd9Sstevel@tonic-gate (void)vsnprintf(buf + used, MAX_PROCTITLE - used, fmt, ap);
90*7c478bd9Sstevel@tonic-gate } else
91*7c478bd9Sstevel@tonic-gate (void)snprintf(buf, MAX_PROCTITLE, "%s", __progname);
92*7c478bd9Sstevel@tonic-gate va_end(ap);
93*7c478bd9Sstevel@tonic-gate used = strlen(buf);
94*7c478bd9Sstevel@tonic-gate
95*7c478bd9Sstevel@tonic-gate #if SPT_TYPE == SPT_PSTAT
96*7c478bd9Sstevel@tonic-gate pst.pst_command = buf;
97*7c478bd9Sstevel@tonic-gate pstat(PSTAT_SETCMD, pst, used, 0, 0);
98*7c478bd9Sstevel@tonic-gate #endif /* SPT_TYPE == SPT_PSTAT */
99*7c478bd9Sstevel@tonic-gate
100*7c478bd9Sstevel@tonic-gate #endif /* SPT_TYPE != SPT_NONE */
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate #endif /* HAVE_SETPROCTITLE */
103*7c478bd9Sstevel@tonic-gate
104*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
105