setproctitle.c revision 93399
113039Speter/* 213039Speter * Copyright (c) 1995 Peter Wemm <peter@freebsd.org> 313039Speter * All rights reserved. 413039Speter * 513039Speter * Redistribution and use in source and binary forms, with or without 613039Speter * modification, is permitted provided that the following conditions 713039Speter * are met: 813039Speter * 1. Redistributions of source code must retain the above copyright 913039Speter * notice immediately at the beginning of the file, without modification, 1013039Speter * this list of conditions, and the following disclaimer. 1113039Speter * 2. Redistributions in binary form must reproduce the above copyright 1213039Speter * notice, this list of conditions and the following disclaimer in the 1313039Speter * documentation and/or other materials provided with the distribution. 1414236Speter * 3. Absolutely no warranty of function or purpose is made by the author 1513039Speter * Peter Wemm. 1613039Speter */ 1713039Speter 1890039Sobrien#include <sys/cdefs.h> 1990039Sobrien__FBSDID("$FreeBSD: head/lib/libc/gen/setproctitle.c 93399 2002-03-29 22:43:43Z markm $"); 2090039Sobrien 2193399Smarkm#include "namespace.h" 2213039Speter#include <sys/types.h> 2313039Speter#include <sys/param.h> 2413039Speter#include <sys/exec.h> 2514236Speter#include <sys/sysctl.h> 2613039Speter 2713039Speter#include <vm/vm.h> 2813039Speter#include <vm/vm_param.h> 2913039Speter#include <vm/pmap.h> 3013039Speter 3113039Speter#include <stdio.h> 3213039Speter#include <string.h> 3313039Speter#include <stdlib.h> 3453239Sphk#include <unistd.h> 3593399Smarkm#include "un-namespace.h" 3613039Speter 3793399Smarkm#include "libc_private.h" 3893399Smarkm 3913039Speter/* 4013039Speter * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and 4113039Speter * in different locations. 4213039Speter * 1: old_ps_strings at the very top of the stack. 4313039Speter * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack. 4413039Speter * 3: ps_strings at the very top of the stack. 4513039Speter * This attempts to support a kernel built in the #2 and #3 era. 4613039Speter */ 4713039Speter 4813039Speterstruct old_ps_strings { 4913039Speter char *old_ps_argvstr; 5013039Speter int old_ps_nargvstr; 5113039Speter char *old_ps_envstr; 5213039Speter int old_ps_nenvstr; 5313039Speter}; 5413039Speter#define OLD_PS_STRINGS ((struct old_ps_strings *) \ 5513039Speter (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings))) 5613039Speter 5713039Speter#include <stdarg.h> 5813039Speter 5935491Sdg#define SPT_BUFSIZE 2048 /* from other parts of sendmail */ 6013039Speter 6113039Spetervoid 6213039Spetersetproctitle(const char *fmt, ...) 6313039Speter{ 6453297Sbrian static struct ps_strings *ps_strings; 6513039Speter static char buf[SPT_BUFSIZE]; 6653297Sbrian static char obuf[SPT_BUFSIZE]; 6753297Sbrian static char **oargv, *kbuf; 6853297Sbrian static int oargc = -1; 6953297Sbrian static char *nargv[2] = { buf, NULL }; 7053297Sbrian char **nargvp; 7153297Sbrian int nargc; 7269558Sjdp int i; 7313039Speter va_list ap; 7414236Speter size_t len; 7541875Sbde unsigned long ul_ps_strings; 7653239Sphk int oid[4]; 7713039Speter 7813039Speter va_start(ap, fmt); 7913039Speter 8013039Speter if (fmt) { 8153297Sbrian buf[sizeof(buf) - 1] = '\0'; 8213039Speter 8364094Sps if (fmt[0] == '-') { 8464094Sps /* skip program name prefix */ 8564094Sps fmt++; 8664094Sps len = 0; 8764094Sps } else { 8864094Sps /* print program name heading for grep */ 8993399Smarkm (void)snprintf(buf, sizeof(buf), "%s: ", _getprogname()); 9064094Sps len = strlen(buf); 9164094Sps } 9213039Speter 9313039Speter /* print the argument string */ 9453297Sbrian (void) vsnprintf(buf + len, sizeof(buf) - len, fmt, ap); 9513039Speter 9653297Sbrian nargvp = nargv; 9753297Sbrian nargc = 1; 9853297Sbrian kbuf = buf; 9953297Sbrian } else if (*obuf != '\0') { 10053297Sbrian /* Idea from NetBSD - reset the title on fmt == NULL */ 10153297Sbrian nargvp = oargv; 10253297Sbrian nargc = oargc; 10353297Sbrian kbuf = obuf; 10453297Sbrian } else 10553297Sbrian /* Nothing to restore */ 10653297Sbrian return; 10753297Sbrian 10813039Speter va_end(ap); 10913039Speter 11053239Sphk /* Set the title into the kernel cached command line */ 11153239Sphk oid[0] = CTL_KERN; 11253239Sphk oid[1] = KERN_PROC; 11353239Sphk oid[2] = KERN_PROC_ARGS; 11453239Sphk oid[3] = getpid(); 11553297Sbrian sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1); 11653239Sphk 11735490Sdg if (ps_strings == NULL) { 11841875Sbde len = sizeof(ul_ps_strings); 11941875Sbde if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL, 12041875Sbde 0) == -1) 12141875Sbde ul_ps_strings = PS_STRINGS; 12241875Sbde ps_strings = (struct ps_strings *)ul_ps_strings; 12335490Sdg } 12414236Speter 12513039Speter /* PS_STRINGS points to zeroed memory on a style #2 kernel */ 12614236Speter if (ps_strings->ps_argvstr) { 12713039Speter /* style #3 */ 12853297Sbrian if (oargc == -1) { 12953297Sbrian /* Record our original args */ 13053297Sbrian oargc = ps_strings->ps_nargvstr; 13153297Sbrian oargv = ps_strings->ps_argvstr; 13269558Sjdp for (i = len = 0; i < oargc; i++) { 13369560Sjdp /* 13469560Sjdp * The program may have scribbled into its 13569560Sjdp * argv array, e.g., to remove some arguments. 13669560Sjdp * If that has happened, break out before 13769560Sjdp * trying to call strlen on a NULL pointer. 13869560Sjdp */ 13969560Sjdp if (oargv[i] == NULL) { 14069560Sjdp oargc = i; 14169560Sjdp break; 14269560Sjdp } 14353297Sbrian snprintf(obuf + len, sizeof(obuf) - len, "%s%s", 14469558Sjdp len ? " " : "", oargv[i]); 14553297Sbrian if (len) 14653297Sbrian len++; 14769558Sjdp len += strlen(oargv[i]); 14853297Sbrian if (len >= sizeof(obuf)) 14953297Sbrian break; 15053297Sbrian } 15153297Sbrian } 15253297Sbrian ps_strings->ps_nargvstr = nargc; 15353297Sbrian ps_strings->ps_argvstr = nargvp; 15413039Speter } else { 15553297Sbrian /* style #2 - we can only restore our first arg :-( */ 15653297Sbrian if (*obuf == '\0') 15753297Sbrian strncpy(obuf, OLD_PS_STRINGS->old_ps_argvstr, 15853297Sbrian sizeof(obuf) - 1); 15913039Speter OLD_PS_STRINGS->old_ps_nargvstr = 1; 16053297Sbrian OLD_PS_STRINGS->old_ps_argvstr = nargvp[0]; 16113039Speter } 16213039Speter} 163