Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 1 | /* Based on conf.c from UCB sendmail 8.8.8 */ |
Kevin Steves | 8848b24 | 2000-10-18 13:11:44 +0000 | [diff] [blame] | 2 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 3 | /* |
| 4 | * Copyright 2003 Damien Miller |
| 5 | * Copyright (c) 1983, 1995-1997 Eric P. Allman |
| 6 | * Copyright (c) 1988, 1993 |
| 7 | * The Regents of the University of California. All rights reserved. |
Kevin Steves | 8848b24 | 2000-10-18 13:11:44 +0000 | [diff] [blame] | 8 | * |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. All advertising materials mentioning features or use of this software |
| 18 | * must display the following acknowledgement: |
| 19 | * This product includes software developed by the University of |
| 20 | * California, Berkeley and its contributors. |
| 21 | * 4. Neither the name of the University nor the names of its contributors |
| 22 | * may be used to endorse or promote products derived from this software |
| 23 | * without specific prior written permission. |
Kevin Steves | 8848b24 | 2000-10-18 13:11:44 +0000 | [diff] [blame] | 24 | * |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 35 | * SUCH DAMAGE. |
Kevin Steves | 8848b24 | 2000-10-18 13:11:44 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
Kevin Steves | 8848b24 | 2000-10-18 13:11:44 +0000 | [diff] [blame] | 38 | #include "includes.h" |
| 39 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 40 | #ifndef HAVE_SETPROCTITLE |
| 41 | |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 42 | #include <unistd.h> |
| 43 | #ifdef HAVE_SYS_PSTAT_H |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 44 | #include <sys/pstat.h> |
Kevin Steves | 8848b24 | 2000-10-18 13:11:44 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 47 | #define SPT_NONE 0 /* don't use it at all */ |
| 48 | #define SPT_PSTAT 1 /* cover argv with title information */ |
| 49 | #define SPT_REUSEARGV 2 /* use pstat(PSTAT_SETCMD, ...) */ |
Damien Miller | ec20196 | 2003-01-13 10:04:58 +1100 | [diff] [blame] | 50 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 51 | #ifndef SPT_TYPE |
| 52 | # define SPT_TYPE SPT_NONE |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 53 | #endif |
| 54 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 55 | #ifndef SPT_PADCHAR |
| 56 | # define SPT_PADCHAR '\0' |
Damien Miller | ec20196 | 2003-01-13 10:04:58 +1100 | [diff] [blame] | 57 | #endif |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 58 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 59 | #if SPT_TYPE == SPT_REUSEARGV |
| 60 | static char *argv_start = NULL; |
| 61 | static size_t argv_env_len = 0; |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 62 | #endif |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 63 | |
Kevin Steves | 8848b24 | 2000-10-18 13:11:44 +0000 | [diff] [blame] | 64 | #endif /* HAVE_SETPROCTITLE */ |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 65 | |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 66 | void |
| 67 | compat_init_setproctitle(int argc, char *argv[]) |
| 68 | { |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 69 | #if SPT_TYPE == SPT_REUSEARGV |
| 70 | extern char **environ; |
| 71 | char *lastargv = NULL; |
| 72 | char **envp = environ; |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 73 | int i; |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 74 | |
| 75 | /* |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 76 | * NB: This assumes that argv has already been copied out of the |
| 77 | * way. This is true for sshd, but may not be true for other |
| 78 | * programs. Beware. |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 79 | */ |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 80 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 81 | if (argc == 0 || argv[0] == NULL) |
| 82 | return; |
| 83 | |
| 84 | /* Fail if we can't allocate room for the new environment */ |
| 85 | for (i = 0; envp[i] != NULL; i++) |
| 86 | ; |
| 87 | if ((environ = malloc(sizeof(*environ) * (i + 1))) == NULL) { |
| 88 | environ = envp; /* put it back */ |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 89 | return; |
| 90 | } |
| 91 | |
| 92 | /* |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 93 | * Find the last argv string or environment variable within |
| 94 | * our process memory area. |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 95 | */ |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 96 | for (i = 0; i < argc; i++) { |
| 97 | if (lastargv == NULL || lastargv + 1 == argv[i]) |
| 98 | lastargv = argv[i] + strlen(argv[i]); |
| 99 | } |
| 100 | for (i = 0; envp[i] != NULL; i++) { |
| 101 | if (lastargv + 1 == envp[i]) |
| 102 | lastargv = envp[i] + strlen(envp[i]); |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 103 | } |
| 104 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 105 | argv[1] = NULL; |
| 106 | argv_start = argv[0]; |
| 107 | argv_env_len = lastargv - argv[0] - 1; |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 108 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 109 | /* |
| 110 | * Copy environment |
| 111 | * XXX - will truncate env on strdup fail |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 112 | */ |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 113 | for (i = 0; envp[i] != NULL; i++) |
| 114 | environ[i] = strdup(envp[i]); |
| 115 | environ[i] = NULL; |
| 116 | #endif /* SPT_REUSEARGV */ |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 117 | } |
| 118 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame^] | 119 | #ifndef HAVE_SETPROCTITLE |
| 120 | void |
| 121 | setproctitle(const char *fmt, ...) |
| 122 | { |
| 123 | #if SPT_TYPE != SPT_NONE |
| 124 | va_list ap; |
| 125 | char buf[1024]; |
| 126 | size_t len; |
| 127 | extern char *__progname; |
| 128 | #if SPT_TYPE == SPT_PSTAT |
| 129 | union pstun pst; |
| 130 | #endif |
| 131 | |
| 132 | #if SPT_TYPE == SPT_REUSEARGV |
| 133 | if (argv_env_len <= 0) |
| 134 | return; |
| 135 | #endif |
| 136 | |
| 137 | strlcpy(buf, __progname, sizeof(buf)); |
| 138 | |
| 139 | va_start(ap, fmt); |
| 140 | if (fmt != NULL) { |
| 141 | len = strlcat(buf, ": ", sizeof(buf)); |
| 142 | if (len < sizeof(buf)) |
| 143 | vsnprintf(buf + len, sizeof(buf) - len , fmt, ap); |
| 144 | } |
| 145 | va_end(ap); |
| 146 | |
| 147 | #if SPT_TYPE == SPT_PSTAT |
| 148 | pst.pst_command = buf; |
| 149 | pstat(PSTAT_SETCMD, pst, strlen(buf), 0, 0); |
| 150 | #elif SPT_TYPE == SPT_REUSEARGV |
| 151 | /* debug("setproctitle: copy \"%s\" into len %d", |
| 152 | buf, argv_env_len); */ |
| 153 | len = strlcpy(argv_start, buf, argv_env_len); |
| 154 | for(; len < argv_env_len; len++) |
| 155 | argv_start[len] = SPT_PADCHAR; |
| 156 | #endif |
| 157 | |
| 158 | #endif /* SPT_NONE */ |
| 159 | } |
| 160 | |
| 161 | #endif /* HAVE_SETPROCTITLE */ |