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. |
Damien Miller | 4636d93 | 2003-06-03 11:23:32 +1000 | [diff] [blame] | 17 | * 3. Neither the name of the University nor the names of its contributors |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
Kevin Steves | 8848b24 | 2000-10-18 13:11:44 +0000 | [diff] [blame] | 20 | * |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
Kevin Steves | 8848b24 | 2000-10-18 13:11:44 +0000 | [diff] [blame] | 32 | */ |
| 33 | |
Kevin Steves | 8848b24 | 2000-10-18 13:11:44 +0000 | [diff] [blame] | 34 | #include "includes.h" |
| 35 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 36 | #ifndef HAVE_SETPROCTITLE |
| 37 | |
Darren Tucker | 767e413 | 2006-07-12 22:43:28 +1000 | [diff] [blame] | 38 | #include <stdarg.h> |
Darren Tucker | e6b641a | 2006-08-17 18:55:27 +1000 | [diff] [blame] | 39 | #include <stdlib.h> |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 40 | #include <unistd.h> |
| 41 | #ifdef HAVE_SYS_PSTAT_H |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 42 | #include <sys/pstat.h> |
Kevin Steves | 8848b24 | 2000-10-18 13:11:44 +0000 | [diff] [blame] | 43 | #endif |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 44 | #include <string.h> |
Kevin Steves | 8848b24 | 2000-10-18 13:11:44 +0000 | [diff] [blame] | 45 | |
Damien Miller | c575022 | 2008-05-16 10:01:54 +1000 | [diff] [blame] | 46 | #include <vis.h> |
| 47 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 48 | #define SPT_NONE 0 /* don't use it at all */ |
Damien Miller | 2e45cb0 | 2004-02-20 20:37:44 +1100 | [diff] [blame] | 49 | #define SPT_PSTAT 1 /* use pstat(PSTAT_SETCMD, ...) */ |
| 50 | #define SPT_REUSEARGV 2 /* cover argv with title information */ |
Damien Miller | ec20196 | 2003-01-13 10:04:58 +1100 | [diff] [blame] | 51 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 52 | #ifndef SPT_TYPE |
| 53 | # define SPT_TYPE SPT_NONE |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 54 | #endif |
| 55 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 56 | #ifndef SPT_PADCHAR |
| 57 | # define SPT_PADCHAR '\0' |
Damien Miller | ec20196 | 2003-01-13 10:04:58 +1100 | [diff] [blame] | 58 | #endif |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 59 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 60 | #if SPT_TYPE == SPT_REUSEARGV |
| 61 | static char *argv_start = NULL; |
| 62 | static size_t argv_env_len = 0; |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 63 | #endif |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 64 | |
Kevin Steves | 8848b24 | 2000-10-18 13:11:44 +0000 | [diff] [blame] | 65 | #endif /* HAVE_SETPROCTITLE */ |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 66 | |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 67 | void |
| 68 | compat_init_setproctitle(int argc, char *argv[]) |
| 69 | { |
Damien Miller | ee87838 | 2014-01-22 16:30:15 +1100 | [diff] [blame] | 70 | #if !defined(HAVE_SETPROCTITLE) && \ |
| 71 | defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 72 | extern char **environ; |
| 73 | char *lastargv = NULL; |
| 74 | char **envp = environ; |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 75 | int i; |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 76 | |
| 77 | /* |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 78 | * NB: This assumes that argv has already been copied out of the |
Darren Tucker | c20dccb | 2016-08-02 09:44:25 +1000 | [diff] [blame] | 79 | * way. This is true for sshd, but may not be true for other |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 80 | * programs. Beware. |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 81 | */ |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 82 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 83 | if (argc == 0 || argv[0] == NULL) |
| 84 | return; |
| 85 | |
| 86 | /* Fail if we can't allocate room for the new environment */ |
| 87 | for (i = 0; envp[i] != NULL; i++) |
| 88 | ; |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 89 | if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) { |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 90 | environ = envp; /* put it back */ |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
| 94 | /* |
Darren Tucker | c20dccb | 2016-08-02 09:44:25 +1000 | [diff] [blame] | 95 | * Find the last argv string or environment variable within |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 96 | * our process memory area. |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 97 | */ |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 98 | for (i = 0; i < argc; i++) { |
| 99 | if (lastargv == NULL || lastargv + 1 == argv[i]) |
| 100 | lastargv = argv[i] + strlen(argv[i]); |
| 101 | } |
| 102 | for (i = 0; envp[i] != NULL; i++) { |
| 103 | if (lastargv + 1 == envp[i]) |
| 104 | lastargv = envp[i] + strlen(envp[i]); |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 105 | } |
| 106 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 107 | argv[1] = NULL; |
| 108 | argv_start = argv[0]; |
| 109 | argv_env_len = lastargv - argv[0] - 1; |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 110 | |
Darren Tucker | c20dccb | 2016-08-02 09:44:25 +1000 | [diff] [blame] | 111 | /* |
| 112 | * Copy environment |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 113 | * XXX - will truncate env on strdup fail |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 114 | */ |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 115 | for (i = 0; envp[i] != NULL; i++) |
| 116 | environ[i] = strdup(envp[i]); |
| 117 | environ[i] = NULL; |
| 118 | #endif /* SPT_REUSEARGV */ |
Damien Miller | a8ed44b | 2003-01-10 09:53:12 +1100 | [diff] [blame] | 119 | } |
| 120 | |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 121 | #ifndef HAVE_SETPROCTITLE |
| 122 | void |
| 123 | setproctitle(const char *fmt, ...) |
| 124 | { |
| 125 | #if SPT_TYPE != SPT_NONE |
| 126 | va_list ap; |
Damien Miller | c575022 | 2008-05-16 10:01:54 +1000 | [diff] [blame] | 127 | char buf[1024], ptitle[1024]; |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 128 | size_t len; |
Darren Tucker | 710f374 | 2013-11-03 17:20:34 +1100 | [diff] [blame] | 129 | int r; |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 130 | extern char *__progname; |
| 131 | #if SPT_TYPE == SPT_PSTAT |
| 132 | union pstun pst; |
| 133 | #endif |
| 134 | |
| 135 | #if SPT_TYPE == SPT_REUSEARGV |
| 136 | if (argv_env_len <= 0) |
| 137 | return; |
| 138 | #endif |
| 139 | |
| 140 | strlcpy(buf, __progname, sizeof(buf)); |
| 141 | |
Darren Tucker | 710f374 | 2013-11-03 17:20:34 +1100 | [diff] [blame] | 142 | r = -1; |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 143 | va_start(ap, fmt); |
| 144 | if (fmt != NULL) { |
| 145 | len = strlcat(buf, ": ", sizeof(buf)); |
| 146 | if (len < sizeof(buf)) |
Darren Tucker | 710f374 | 2013-11-03 17:20:34 +1100 | [diff] [blame] | 147 | r = vsnprintf(buf + len, sizeof(buf) - len , fmt, ap); |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 148 | } |
| 149 | va_end(ap); |
Darren Tucker | 710f374 | 2013-11-03 17:20:34 +1100 | [diff] [blame] | 150 | if (r == -1 || (size_t)r >= sizeof(buf) - len) |
| 151 | return; |
Damien Miller | c575022 | 2008-05-16 10:01:54 +1000 | [diff] [blame] | 152 | strnvis(ptitle, buf, sizeof(ptitle), |
| 153 | VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL); |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 154 | |
| 155 | #if SPT_TYPE == SPT_PSTAT |
Damien Miller | c575022 | 2008-05-16 10:01:54 +1000 | [diff] [blame] | 156 | pst.pst_command = ptitle; |
| 157 | pstat(PSTAT_SETCMD, pst, strlen(ptitle), 0, 0); |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 158 | #elif SPT_TYPE == SPT_REUSEARGV |
Darren Tucker | c20dccb | 2016-08-02 09:44:25 +1000 | [diff] [blame] | 159 | /* debug("setproctitle: copy \"%s\" into len %d", |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 160 | buf, argv_env_len); */ |
Damien Miller | c575022 | 2008-05-16 10:01:54 +1000 | [diff] [blame] | 161 | len = strlcpy(argv_start, ptitle, argv_env_len); |
Damien Miller | 3527625 | 2003-06-03 10:14:28 +1000 | [diff] [blame] | 162 | for(; len < argv_env_len; len++) |
| 163 | argv_start[len] = SPT_PADCHAR; |
| 164 | #endif |
| 165 | |
| 166 | #endif /* SPT_NONE */ |
| 167 | } |
| 168 | |
| 169 | #endif /* HAVE_SETPROCTITLE */ |