setproctitle.c revision 281763
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 281763 2015-04-20 09:07:12Z kib $"); 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. 45281763Skib * We only support a kernel providing #3 style ps_strings. 46281763Skib * 47281763Skib * For historical purposes, a definition of the old ps_strings structure 48281763Skib * and location is preserved below: 4913039Speterstruct old_ps_strings { 5013039Speter char *old_ps_argvstr; 5113039Speter int old_ps_nargvstr; 5213039Speter char *old_ps_envstr; 5313039Speter int old_ps_nenvstr; 5413039Speter}; 5513039Speter#define OLD_PS_STRINGS ((struct old_ps_strings *) \ 5613039Speter (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings))) 57281763Skib */ 5813039Speter 5913039Speter#include <stdarg.h> 6013039Speter 6135491Sdg#define SPT_BUFSIZE 2048 /* from other parts of sendmail */ 6213039Speter 6313039Spetervoid 6413039Spetersetproctitle(const char *fmt, ...) 6513039Speter{ 6653297Sbrian static struct ps_strings *ps_strings; 67108163Sbbraun static char *buf = NULL; 68108163Sbbraun static char *obuf = NULL; 6953297Sbrian static char **oargv, *kbuf; 7053297Sbrian static int oargc = -1; 71108163Sbbraun static char *nargv[2] = { NULL, NULL }; 7253297Sbrian char **nargvp; 7353297Sbrian int nargc; 7469558Sjdp int i; 7513039Speter va_list ap; 7614236Speter size_t len; 7741875Sbde unsigned long ul_ps_strings; 7853239Sphk int oid[4]; 7913039Speter 80108163Sbbraun if (buf == NULL) { 81108163Sbbraun buf = malloc(SPT_BUFSIZE); 82108163Sbbraun if (buf == NULL) 83108163Sbbraun return; 84108163Sbbraun nargv[0] = buf; 85108163Sbbraun } 86108163Sbbraun 87108163Sbbraun if (obuf == NULL ) { 88108163Sbbraun obuf = malloc(SPT_BUFSIZE); 89108163Sbbraun if (obuf == NULL) 90108163Sbbraun return; 91117102Salfred *obuf = '\0'; 92108163Sbbraun } 93108163Sbbraun 9413039Speter va_start(ap, fmt); 9513039Speter 9613039Speter if (fmt) { 97108163Sbbraun buf[SPT_BUFSIZE - 1] = '\0'; 9813039Speter 9964094Sps if (fmt[0] == '-') { 10064094Sps /* skip program name prefix */ 10164094Sps fmt++; 10264094Sps len = 0; 10364094Sps } else { 10464094Sps /* print program name heading for grep */ 105108163Sbbraun (void)snprintf(buf, SPT_BUFSIZE, "%s: ", _getprogname()); 10664094Sps len = strlen(buf); 10764094Sps } 10813039Speter 10913039Speter /* print the argument string */ 110108163Sbbraun (void) vsnprintf(buf + len, SPT_BUFSIZE - len, fmt, ap); 11113039Speter 11253297Sbrian nargvp = nargv; 11353297Sbrian nargc = 1; 11453297Sbrian kbuf = buf; 11553297Sbrian } else if (*obuf != '\0') { 11653297Sbrian /* Idea from NetBSD - reset the title on fmt == NULL */ 11753297Sbrian nargvp = oargv; 11853297Sbrian nargc = oargc; 11953297Sbrian kbuf = obuf; 12053297Sbrian } else 12153297Sbrian /* Nothing to restore */ 12253297Sbrian return; 12353297Sbrian 12413039Speter va_end(ap); 12513039Speter 12653239Sphk /* Set the title into the kernel cached command line */ 12753239Sphk oid[0] = CTL_KERN; 12853239Sphk oid[1] = KERN_PROC; 12953239Sphk oid[2] = KERN_PROC_ARGS; 13053239Sphk oid[3] = getpid(); 13153297Sbrian sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1); 13253239Sphk 13335490Sdg if (ps_strings == NULL) { 13441875Sbde len = sizeof(ul_ps_strings); 13541875Sbde if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL, 13641875Sbde 0) == -1) 13741875Sbde ul_ps_strings = PS_STRINGS; 13841875Sbde ps_strings = (struct ps_strings *)ul_ps_strings; 13935490Sdg } 14014236Speter 141281763Skib /* 142281763Skib * PS_STRINGS points to zeroed memory on a style #2 kernel. 143281763Skib * Should not happen. 144281763Skib */ 145281763Skib if (ps_strings->ps_argvstr == NULL) 146281763Skib return; 147281763Skib 148281763Skib /* style #3 */ 149281763Skib if (oargc == -1) { 150281763Skib /* Record our original args */ 151281763Skib oargc = ps_strings->ps_nargvstr; 152281763Skib oargv = ps_strings->ps_argvstr; 153281763Skib for (i = len = 0; i < oargc; i++) { 154281763Skib /* 155281763Skib * The program may have scribbled into its 156281763Skib * argv array, e.g., to remove some arguments. 157281763Skib * If that has happened, break out before 158281763Skib * trying to call strlen on a NULL pointer. 159281763Skib */ 160281763Skib if (oargv[i] == NULL) { 161281763Skib oargc = i; 162281763Skib break; 16353297Sbrian } 164281763Skib snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s", 165281763Skib len != 0 ? " " : "", oargv[i]); 166281763Skib if (len != 0) 167281763Skib len++; 168281763Skib len += strlen(oargv[i]); 169281763Skib if (len >= SPT_BUFSIZE) 170281763Skib break; 17153297Sbrian } 17213039Speter } 173281763Skib ps_strings->ps_nargvstr = nargc; 174281763Skib ps_strings->ps_argvstr = nargvp; 17513039Speter} 176