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