setproctitle.c revision 53239
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 * 1750476Speter * $FreeBSD: head/lib/libc/gen/setproctitle.c 53239 1999-11-16 20:31:58Z phk $ 1813039Speter */ 1913039Speter 2013039Speter#include <sys/types.h> 2113039Speter#include <sys/param.h> 2213039Speter#include <sys/exec.h> 2314236Speter#include <sys/sysctl.h> 2413039Speter 2513039Speter#include <vm/vm.h> 2613039Speter#include <vm/vm_param.h> 2713039Speter#include <vm/pmap.h> 2813039Speter 2913039Speter#include <stdio.h> 3013039Speter#include <string.h> 3113039Speter#include <stdlib.h> 3253239Sphk#include <unistd.h> 3313039Speter 3413039Speter/* 3513039Speter * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and 3613039Speter * in different locations. 3713039Speter * 1: old_ps_strings at the very top of the stack. 3813039Speter * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack. 3913039Speter * 3: ps_strings at the very top of the stack. 4013039Speter * This attempts to support a kernel built in the #2 and #3 era. 4113039Speter */ 4213039Speter 4313039Speterstruct old_ps_strings { 4413039Speter char *old_ps_argvstr; 4513039Speter int old_ps_nargvstr; 4613039Speter char *old_ps_envstr; 4713039Speter int old_ps_nenvstr; 4813039Speter}; 4913039Speter#define OLD_PS_STRINGS ((struct old_ps_strings *) \ 5013039Speter (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings))) 5113039Speter 5213039Speter#if defined(__STDC__) /* from other parts of sendmail */ 5313039Speter#include <stdarg.h> 5413039Speter#else 5513039Speter#include <varargs.h> 5613039Speter#endif 5713039Speter 5813039Speter 5935491Sdg#define SPT_BUFSIZE 2048 /* from other parts of sendmail */ 6013039Speterextern char * __progname; /* is this defined in a .h anywhere? */ 6113039Speter 6235490Sdgstatic struct ps_strings *ps_strings; 6335490Sdg 6413039Spetervoid 6513039Speter#if defined(__STDC__) 6613039Spetersetproctitle(const char *fmt, ...) 6713039Speter#else 6813039Spetersetproctitle(fmt, va_alist) 6913039Speter const char *fmt; 7013039Speter va_dcl 7113039Speter#endif 7213039Speter{ 7313039Speter static char buf[SPT_BUFSIZE]; 7413039Speter static char *ps_argv[2]; 7513039Speter va_list ap; 7614236Speter size_t len; 7741875Sbde unsigned long ul_ps_strings; 7853239Sphk int oid[4]; 7913039Speter 8013039Speter#if defined(__STDC__) 8113039Speter va_start(ap, fmt); 8213039Speter#else 8313039Speter va_start(ap); 8413039Speter#endif 8513039Speter 8613039Speter buf[sizeof(buf) - 1] = '\0'; 8713039Speter if (fmt) { 8813039Speter 8913039Speter /* print program name heading for grep */ 9013039Speter (void) snprintf(buf, sizeof(buf) - 1, "%s: ", __progname); 9113039Speter 9213039Speter /* 9313039Speter * can't use return from sprintf, as that is the count of how 9413039Speter * much it wanted to write, not how much it actually did. 9513039Speter */ 9613039Speter 9713039Speter len = strlen(buf); 9813039Speter 9913039Speter /* print the argument string */ 10013039Speter (void) vsnprintf(buf + len, sizeof(buf) - 1 - len, fmt, ap); 10113039Speter } else { 10213039Speter /* Idea from NetBSD - reset the title on fmt == NULL */ 10313039Speter strncpy(buf, __progname, sizeof(buf) - 1); 10413039Speter } 10513039Speter 10613039Speter va_end(ap); 10713039Speter 10853239Sphk /* Set the title into the kernel cached command line */ 10953239Sphk oid[0] = CTL_KERN; 11053239Sphk oid[1] = KERN_PROC; 11153239Sphk oid[2] = KERN_PROC_ARGS; 11253239Sphk oid[3] = getpid(); 11353239Sphk sysctl(oid, 4, 0, 0, buf, strlen(buf) + 1); 11453239Sphk 11535490Sdg if (ps_strings == NULL) { 11641875Sbde len = sizeof(ul_ps_strings); 11741875Sbde if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL, 11841875Sbde 0) == -1) 11941875Sbde ul_ps_strings = PS_STRINGS; 12041875Sbde ps_strings = (struct ps_strings *)ul_ps_strings; 12135490Sdg } 12214236Speter 12313039Speter /* PS_STRINGS points to zeroed memory on a style #2 kernel */ 12414236Speter if (ps_strings->ps_argvstr) { 12513039Speter /* style #3 */ 12613039Speter ps_argv[0] = buf; 12713039Speter ps_argv[1] = NULL; 12814236Speter ps_strings->ps_nargvstr = 1; 12914236Speter ps_strings->ps_argvstr = ps_argv; 13013039Speter } else { 13113039Speter /* style #2 */ 13213039Speter OLD_PS_STRINGS->old_ps_nargvstr = 1; 13313039Speter OLD_PS_STRINGS->old_ps_argvstr = buf; 13413039Speter } 13513039Speter} 136