setproctitle.c revision 162852
11590Srgrimes/* Based on conf.c from UCB sendmail 8.8.8 */ 21590Srgrimes 31590Srgrimes/* 41590Srgrimes * Copyright 2003 Damien Miller 51590Srgrimes * Copyright (c) 1983, 1995-1997 Eric P. Allman 61590Srgrimes * Copyright (c) 1988, 1993 71590Srgrimes * The Regents of the University of California. All rights reserved. 81590Srgrimes * 91590Srgrimes * Redistribution and use in source and binary forms, with or without 101590Srgrimes * modification, are permitted provided that the following conditions 111590Srgrimes * are met: 121590Srgrimes * 1. Redistributions of source code must retain the above copyright 131590Srgrimes * notice, this list of conditions and the following disclaimer. 141590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 151590Srgrimes * notice, this list of conditions and the following disclaimer in the 161590Srgrimes * documentation and/or other materials provided with the distribution. 171590Srgrimes * 3. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 341590Srgrimes#include "includes.h" 351590Srgrimes 361590Srgrimes#ifndef HAVE_SETPROCTITLE 3762833Swsanchez 3862833Swsanchez#include <stdarg.h> 391590Srgrimes#include <stdlib.h> 401590Srgrimes#include <unistd.h> 4162833Swsanchez#ifdef HAVE_SYS_PSTAT_H 4294587Sobrien#include <sys/pstat.h> 431590Srgrimes#endif 441590Srgrimes#include <string.h> 451590Srgrimes 461590Srgrimes#define SPT_NONE 0 /* don't use it at all */ 471590Srgrimes#define SPT_PSTAT 1 /* use pstat(PSTAT_SETCMD, ...) */ 481590Srgrimes#define SPT_REUSEARGV 2 /* cover argv with title information */ 491590Srgrimes 501590Srgrimes#ifndef SPT_TYPE 511590Srgrimes# define SPT_TYPE SPT_NONE 521590Srgrimes#endif 535814Sjkh 545814Sjkh#ifndef SPT_PADCHAR 551590Srgrimes# define SPT_PADCHAR '\0' 561590Srgrimes#endif 571590Srgrimes 581590Srgrimes#if SPT_TYPE == SPT_REUSEARGV 591590Srgrimesstatic char *argv_start = NULL; 601590Srgrimesstatic size_t argv_env_len = 0; 611590Srgrimes#endif 621590Srgrimes 631590Srgrimes#endif /* HAVE_SETPROCTITLE */ 641590Srgrimes 651590Srgrimesvoid 661590Srgrimescompat_init_setproctitle(int argc, char *argv[]) 671590Srgrimes{ 681590Srgrimes#if defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV 691590Srgrimes extern char **environ; 701590Srgrimes char *lastargv = NULL; 711590Srgrimes char **envp = environ; 721590Srgrimes int i; 731590Srgrimes 741590Srgrimes /* 751590Srgrimes * NB: This assumes that argv has already been copied out of the 761590Srgrimes * way. This is true for sshd, but may not be true for other 771590Srgrimes * programs. Beware. 781590Srgrimes */ 791590Srgrimes 801590Srgrimes if (argc == 0 || argv[0] == NULL) 811590Srgrimes return; 821590Srgrimes 831590Srgrimes /* Fail if we can't allocate room for the new environment */ 841590Srgrimes for (i = 0; envp[i] != NULL; i++) 851590Srgrimes ; 861590Srgrimes if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) { 871590Srgrimes environ = envp; /* put it back */ 881590Srgrimes return; 891590Srgrimes } 901590Srgrimes 911590Srgrimes /* 925814Sjkh * Find the last argv string or environment variable within 931590Srgrimes * our process memory area. 941590Srgrimes */ 95103503Sjmallett for (i = 0; i < argc; i++) { 961590Srgrimes if (lastargv == NULL || lastargv + 1 == argv[i]) 9792921Simp lastargv = argv[i] + strlen(argv[i]); 9892921Simp } 9992921Simp for (i = 0; envp[i] != NULL; i++) { 10092921Simp if (lastargv + 1 == envp[i]) 1015814Sjkh lastargv = envp[i] + strlen(envp[i]); 1021590Srgrimes } 1031590Srgrimes 1041590Srgrimes argv[1] = NULL; 1051590Srgrimes argv_start = argv[0]; 1061590Srgrimes argv_env_len = lastargv - argv[0] - 1; 1071590Srgrimes 1081590Srgrimes /* 1091590Srgrimes * Copy environment 1101590Srgrimes * XXX - will truncate env on strdup fail 1111590Srgrimes */ 1121590Srgrimes for (i = 0; envp[i] != NULL; i++) 1131590Srgrimes environ[i] = strdup(envp[i]); 1141590Srgrimes environ[i] = NULL; 1151590Srgrimes#endif /* SPT_REUSEARGV */ 1161590Srgrimes} 1171590Srgrimes 1181590Srgrimes#ifndef HAVE_SETPROCTITLE 1191590Srgrimesvoid 1201590Srgrimessetproctitle(const char *fmt, ...) 1211590Srgrimes{ 1221590Srgrimes#if SPT_TYPE != SPT_NONE 1235814Sjkh va_list ap; 1245814Sjkh char buf[1024]; 1255814Sjkh size_t len; 1265814Sjkh extern char *__progname; 1275814Sjkh#if SPT_TYPE == SPT_PSTAT 1285814Sjkh union pstun pst; 1295814Sjkh#endif 1305814Sjkh 1315814Sjkh#if SPT_TYPE == SPT_REUSEARGV 1325814Sjkh if (argv_env_len <= 0) 1335814Sjkh return; 1345814Sjkh#endif 1355814Sjkh 1365814Sjkh strlcpy(buf, __progname, sizeof(buf)); 1375814Sjkh 1385814Sjkh va_start(ap, fmt); 1395814Sjkh if (fmt != NULL) { 1405814Sjkh len = strlcat(buf, ": ", sizeof(buf)); 1415814Sjkh if (len < sizeof(buf)) 1425814Sjkh vsnprintf(buf + len, sizeof(buf) - len , fmt, ap); 1435814Sjkh } 1441590Srgrimes va_end(ap); 1451590Srgrimes 1461590Srgrimes#if SPT_TYPE == SPT_PSTAT 1471590Srgrimes pst.pst_command = buf; 1481590Srgrimes pstat(PSTAT_SETCMD, pst, strlen(buf), 0, 0); 1491590Srgrimes#elif SPT_TYPE == SPT_REUSEARGV 1501590Srgrimes/* debug("setproctitle: copy \"%s\" into len %d", 1511590Srgrimes buf, argv_env_len); */ 1525814Sjkh len = strlcpy(argv_start, buf, argv_env_len); 1531590Srgrimes for(; len < argv_env_len; len++) 1541590Srgrimes argv_start[len] = SPT_PADCHAR; 1551590Srgrimes#endif 1561590Srgrimes 1571590Srgrimes#endif /* SPT_NONE */ 1581590Srgrimes} 15994584Sobrien 1601590Srgrimes#endif /* HAVE_SETPROCTITLE */ 1611590Srgrimes