setproctitle.c revision 181111
1183873Sraj/* Based on conf.c from UCB sendmail 8.8.8 */ 2183873Sraj 3183873Sraj/* 4183873Sraj * Copyright 2003 Damien Miller 5183873Sraj * Copyright (c) 1983, 1995-1997 Eric P. Allman 6183873Sraj * Copyright (c) 1988, 1993 7183873Sraj * The Regents of the University of California. All rights reserved. 8183873Sraj * 9183873Sraj * Redistribution and use in source and binary forms, with or without 10191954Skuriyama * modification, are permitted provided that the following conditions 11185478Ssam * are met: 12183873Sraj * 1. Redistributions of source code must retain the above copyright 13183873Sraj * notice, this list of conditions and the following disclaimer. 14183873Sraj * 2. Redistributions in binary form must reproduce the above copyright 15183873Sraj * notice, this list of conditions and the following disclaimer in the 16183873Sraj * documentation and/or other materials provided with the distribution. 17183873Sraj * 3. Neither the name of the University nor the names of its contributors 18183873Sraj * may be used to endorse or promote products derived from this software 19183873Sraj * without specific prior written permission. 20183873Sraj * 21191954Skuriyama * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22183873Sraj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23191954Skuriyama * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24191954Skuriyama * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25191954Skuriyama * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26191954Skuriyama * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27183873Sraj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28191954Skuriyama * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29183873Sraj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30183873Sraj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31183873Sraj * SUCH DAMAGE. 32183873Sraj */ 33183873Sraj 34183873Sraj#include "includes.h" 35191954Skuriyama 36183873Sraj#ifndef HAVE_SETPROCTITLE 37183873Sraj 38183873Sraj#include <stdarg.h> 39183873Sraj#include <stdlib.h> 40191954Skuriyama#include <unistd.h> 41191954Skuriyama#ifdef HAVE_SYS_PSTAT_H 42203758Sattilio#include <sys/pstat.h> 43191954Skuriyama#endif 44183873Sraj#include <string.h> 45183873Sraj 46191954Skuriyama#include <vis.h> 47183873Sraj 48183873Sraj#define SPT_NONE 0 /* don't use it at all */ 49191954Skuriyama#define SPT_PSTAT 1 /* use pstat(PSTAT_SETCMD, ...) */ 50183873Sraj#define SPT_REUSEARGV 2 /* cover argv with title information */ 51185090Sraj 52185090Sraj#ifndef SPT_TYPE 53183873Sraj# define SPT_TYPE SPT_NONE 54183873Sraj#endif 55183873Sraj 56183873Sraj#ifndef SPT_PADCHAR 57183873Sraj# define SPT_PADCHAR '\0' 58183873Sraj#endif 59183873Sraj 60183873Sraj#if SPT_TYPE == SPT_REUSEARGV 61183873Srajstatic char *argv_start = NULL; 62183873Srajstatic size_t argv_env_len = 0; 63183873Sraj#endif 64183873Sraj 65183873Sraj#endif /* HAVE_SETPROCTITLE */ 66183873Sraj 67183873Srajvoid 68183873Srajcompat_init_setproctitle(int argc, char *argv[]) 69183873Sraj{ 70207077Sthompsa#if defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV 71183873Sraj extern char **environ; 72183873Sraj char *lastargv = NULL; 73183873Sraj char **envp = environ; 74183873Sraj int i; 75183873Sraj 76183873Sraj /* 77183873Sraj * NB: This assumes that argv has already been copied out of the 78183873Sraj * way. This is true for sshd, but may not be true for other 79183873Sraj * programs. Beware. 80183873Sraj */ 81183873Sraj 82194845Sraj if (argc == 0 || argv[0] == NULL) 83194845Sraj return; 84194845Sraj 85194845Sraj /* Fail if we can't allocate room for the new environment */ 86 for (i = 0; envp[i] != NULL; i++) 87 ; 88 if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) { 89 environ = envp; /* put it back */ 90 return; 91 } 92 93 /* 94 * Find the last argv string or environment variable within 95 * our process memory area. 96 */ 97 for (i = 0; i < argc; i++) { 98 if (lastargv == NULL || lastargv + 1 == argv[i]) 99 lastargv = argv[i] + strlen(argv[i]); 100 } 101 for (i = 0; envp[i] != NULL; i++) { 102 if (lastargv + 1 == envp[i]) 103 lastargv = envp[i] + strlen(envp[i]); 104 } 105 106 argv[1] = NULL; 107 argv_start = argv[0]; 108 argv_env_len = lastargv - argv[0] - 1; 109 110 /* 111 * Copy environment 112 * XXX - will truncate env on strdup fail 113 */ 114 for (i = 0; envp[i] != NULL; i++) 115 environ[i] = strdup(envp[i]); 116 environ[i] = NULL; 117#endif /* SPT_REUSEARGV */ 118} 119 120#ifndef HAVE_SETPROCTITLE 121void 122setproctitle(const char *fmt, ...) 123{ 124#if SPT_TYPE != SPT_NONE 125 va_list ap; 126 char buf[1024], ptitle[1024]; 127 size_t len; 128 extern char *__progname; 129#if SPT_TYPE == SPT_PSTAT 130 union pstun pst; 131#endif 132 133#if SPT_TYPE == SPT_REUSEARGV 134 if (argv_env_len <= 0) 135 return; 136#endif 137 138 strlcpy(buf, __progname, sizeof(buf)); 139 140 va_start(ap, fmt); 141 if (fmt != NULL) { 142 len = strlcat(buf, ": ", sizeof(buf)); 143 if (len < sizeof(buf)) 144 vsnprintf(buf + len, sizeof(buf) - len , fmt, ap); 145 } 146 va_end(ap); 147 strnvis(ptitle, buf, sizeof(ptitle), 148 VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL); 149 150#if SPT_TYPE == SPT_PSTAT 151 pst.pst_command = ptitle; 152 pstat(PSTAT_SETCMD, pst, strlen(ptitle), 0, 0); 153#elif SPT_TYPE == SPT_REUSEARGV 154/* debug("setproctitle: copy \"%s\" into len %d", 155 buf, argv_env_len); */ 156 len = strlcpy(argv_start, ptitle, argv_env_len); 157 for(; len < argv_env_len; len++) 158 argv_start[len] = SPT_PADCHAR; 159#endif 160 161#endif /* SPT_NONE */ 162} 163 164#endif /* HAVE_SETPROCTITLE */ 165