setproctitle.c revision 35491
1/* 2 * Copyright (c) 1995 Peter Wemm <peter@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, is permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice immediately at the beginning of the file, without modification, 10 * this list of conditions, and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Absolutely no warranty of function or purpose is made by the author 15 * Peter Wemm. 16 * 17 * $Id: setproctitle.c,v 1.6 1998/04/28 06:59:14 dg Exp $ 18 */ 19 20#include <sys/types.h> 21#include <sys/param.h> 22#include <sys/exec.h> 23#include <sys/sysctl.h> 24 25#include <vm/vm.h> 26#include <vm/vm_param.h> 27#include <vm/pmap.h> 28 29#include <stdio.h> 30#include <string.h> 31#include <stdlib.h> 32 33/* 34 * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and 35 * in different locations. 36 * 1: old_ps_strings at the very top of the stack. 37 * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack. 38 * 3: ps_strings at the very top of the stack. 39 * This attempts to support a kernel built in the #2 and #3 era. 40 */ 41 42struct old_ps_strings { 43 char *old_ps_argvstr; 44 int old_ps_nargvstr; 45 char *old_ps_envstr; 46 int old_ps_nenvstr; 47}; 48#define OLD_PS_STRINGS ((struct old_ps_strings *) \ 49 (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings))) 50 51#if defined(__STDC__) /* from other parts of sendmail */ 52#include <stdarg.h> 53#else 54#include <varargs.h> 55#endif 56 57 58#define SPT_BUFSIZE 2048 /* from other parts of sendmail */ 59extern char * __progname; /* is this defined in a .h anywhere? */ 60 61static struct ps_strings *ps_strings; 62 63void 64#if defined(__STDC__) 65setproctitle(const char *fmt, ...) 66#else 67setproctitle(fmt, va_alist) 68 const char *fmt; 69 va_dcl 70#endif 71{ 72 static char buf[SPT_BUFSIZE]; 73 static char *ps_argv[2]; 74 va_list ap; 75 int mib[2]; 76 size_t len; 77 78#if defined(__STDC__) 79 va_start(ap, fmt); 80#else 81 va_start(ap); 82#endif 83 84 buf[sizeof(buf) - 1] = '\0'; 85 if (fmt) { 86 87 /* print program name heading for grep */ 88 (void) snprintf(buf, sizeof(buf) - 1, "%s: ", __progname); 89 90 /* 91 * can't use return from sprintf, as that is the count of how 92 * much it wanted to write, not how much it actually did. 93 */ 94 95 len = strlen(buf); 96 97 /* print the argument string */ 98 (void) vsnprintf(buf + len, sizeof(buf) - 1 - len, fmt, ap); 99 } else { 100 /* Idea from NetBSD - reset the title on fmt == NULL */ 101 strncpy(buf, __progname, sizeof(buf) - 1); 102 } 103 104 va_end(ap); 105 106 if (ps_strings == NULL) { 107 mib[0] = CTL_KERN; 108 mib[1] = KERN_PS_STRINGS; 109 len = sizeof(ps_strings); 110 if (sysctl(mib, 2, &ps_strings, &len, NULL, 0) < 0 || 111 ps_strings == NULL) 112 ps_strings = PS_STRINGS; 113 } 114 115 /* PS_STRINGS points to zeroed memory on a style #2 kernel */ 116 if (ps_strings->ps_argvstr) { 117 /* style #3 */ 118 ps_argv[0] = buf; 119 ps_argv[1] = NULL; 120 ps_strings->ps_nargvstr = 1; 121 ps_strings->ps_argvstr = ps_argv; 122 } else { 123 /* style #2 */ 124 OLD_PS_STRINGS->old_ps_nargvstr = 1; 125 OLD_PS_STRINGS->old_ps_argvstr = buf; 126 } 127} 128