1995-12-26 22:50:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1995 Peter Wemm <peter@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, is permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice immediately at the beginning of the file, without modification,
|
|
|
|
* this list of conditions, and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1996-02-24 14:37:30 +00:00
|
|
|
* 3. Absolutely no warranty of function or purpose is made by the author
|
1995-12-26 22:50:08 +00:00
|
|
|
* Peter Wemm.
|
|
|
|
*/
|
|
|
|
|
2002-02-01 00:57:29 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-03-29 22:43:43 +00:00
|
|
|
#include "namespace.h"
|
1995-12-26 22:50:08 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/exec.h>
|
1996-02-24 14:37:30 +00:00
|
|
|
#include <sys/sysctl.h>
|
1995-12-26 22:50:08 +00:00
|
|
|
|
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_param.h>
|
|
|
|
#include <vm/pmap.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
1999-11-16 20:31:58 +00:00
|
|
|
#include <unistd.h>
|
2002-03-29 22:43:43 +00:00
|
|
|
#include "un-namespace.h"
|
|
|
|
|
|
|
|
#include "libc_private.h"
|
1995-12-26 22:50:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and
|
|
|
|
* in different locations.
|
|
|
|
* 1: old_ps_strings at the very top of the stack.
|
|
|
|
* 2: old_ps_strings at SPARE_USRSPACE below the top of the stack.
|
|
|
|
* 3: ps_strings at the very top of the stack.
|
|
|
|
* This attempts to support a kernel built in the #2 and #3 era.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct old_ps_strings {
|
|
|
|
char *old_ps_argvstr;
|
|
|
|
int old_ps_nargvstr;
|
|
|
|
char *old_ps_envstr;
|
|
|
|
int old_ps_nenvstr;
|
|
|
|
};
|
|
|
|
#define OLD_PS_STRINGS ((struct old_ps_strings *) \
|
|
|
|
(USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings)))
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
1998-04-28 07:02:33 +00:00
|
|
|
#define SPT_BUFSIZE 2048 /* from other parts of sendmail */
|
1995-12-26 22:50:08 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
setproctitle(const char *fmt, ...)
|
|
|
|
{
|
1999-11-17 21:12:17 +00:00
|
|
|
static struct ps_strings *ps_strings;
|
2002-12-21 22:04:50 +00:00
|
|
|
static char *buf = NULL;
|
|
|
|
static char *obuf = NULL;
|
1999-11-17 21:12:17 +00:00
|
|
|
static char **oargv, *kbuf;
|
|
|
|
static int oargc = -1;
|
2002-12-21 22:04:50 +00:00
|
|
|
static char *nargv[2] = { NULL, NULL };
|
1999-11-17 21:12:17 +00:00
|
|
|
char **nargvp;
|
|
|
|
int nargc;
|
2000-12-04 01:26:29 +00:00
|
|
|
int i;
|
1995-12-26 22:50:08 +00:00
|
|
|
va_list ap;
|
1996-02-24 14:37:30 +00:00
|
|
|
size_t len;
|
1998-12-16 17:34:05 +00:00
|
|
|
unsigned long ul_ps_strings;
|
1999-11-16 20:31:58 +00:00
|
|
|
int oid[4];
|
1995-12-26 22:50:08 +00:00
|
|
|
|
2002-12-21 22:04:50 +00:00
|
|
|
if (buf == NULL) {
|
|
|
|
buf = malloc(SPT_BUFSIZE);
|
|
|
|
if (buf == NULL)
|
|
|
|
return;
|
|
|
|
nargv[0] = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obuf == NULL ) {
|
|
|
|
obuf = malloc(SPT_BUFSIZE);
|
|
|
|
if (obuf == NULL)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1995-12-26 22:50:08 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
if (fmt) {
|
2002-12-21 22:04:50 +00:00
|
|
|
buf[SPT_BUFSIZE - 1] = '\0';
|
1995-12-26 22:50:08 +00:00
|
|
|
|
2000-08-01 06:37:09 +00:00
|
|
|
if (fmt[0] == '-') {
|
|
|
|
/* skip program name prefix */
|
|
|
|
fmt++;
|
|
|
|
len = 0;
|
|
|
|
} else {
|
|
|
|
/* print program name heading for grep */
|
2002-12-21 22:04:50 +00:00
|
|
|
(void)snprintf(buf, SPT_BUFSIZE, "%s: ", _getprogname());
|
2000-08-01 06:37:09 +00:00
|
|
|
len = strlen(buf);
|
|
|
|
}
|
1995-12-26 22:50:08 +00:00
|
|
|
|
|
|
|
/* print the argument string */
|
2002-12-21 22:04:50 +00:00
|
|
|
(void) vsnprintf(buf + len, SPT_BUFSIZE - len, fmt, ap);
|
1999-11-17 21:12:17 +00:00
|
|
|
|
|
|
|
nargvp = nargv;
|
|
|
|
nargc = 1;
|
|
|
|
kbuf = buf;
|
|
|
|
} else if (*obuf != '\0') {
|
|
|
|
/* Idea from NetBSD - reset the title on fmt == NULL */
|
|
|
|
nargvp = oargv;
|
|
|
|
nargc = oargc;
|
|
|
|
kbuf = obuf;
|
|
|
|
} else
|
|
|
|
/* Nothing to restore */
|
|
|
|
return;
|
1995-12-26 22:50:08 +00:00
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
1999-11-16 20:31:58 +00:00
|
|
|
/* Set the title into the kernel cached command line */
|
|
|
|
oid[0] = CTL_KERN;
|
|
|
|
oid[1] = KERN_PROC;
|
|
|
|
oid[2] = KERN_PROC_ARGS;
|
|
|
|
oid[3] = getpid();
|
1999-11-17 21:12:17 +00:00
|
|
|
sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1);
|
1999-11-16 20:31:58 +00:00
|
|
|
|
1998-04-28 06:59:14 +00:00
|
|
|
if (ps_strings == NULL) {
|
1998-12-16 17:34:05 +00:00
|
|
|
len = sizeof(ul_ps_strings);
|
|
|
|
if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
|
|
|
|
0) == -1)
|
|
|
|
ul_ps_strings = PS_STRINGS;
|
|
|
|
ps_strings = (struct ps_strings *)ul_ps_strings;
|
1998-04-28 06:59:14 +00:00
|
|
|
}
|
1996-02-24 14:37:30 +00:00
|
|
|
|
1995-12-26 22:50:08 +00:00
|
|
|
/* PS_STRINGS points to zeroed memory on a style #2 kernel */
|
1996-02-24 14:37:30 +00:00
|
|
|
if (ps_strings->ps_argvstr) {
|
1995-12-26 22:50:08 +00:00
|
|
|
/* style #3 */
|
1999-11-17 21:12:17 +00:00
|
|
|
if (oargc == -1) {
|
|
|
|
/* Record our original args */
|
|
|
|
oargc = ps_strings->ps_nargvstr;
|
|
|
|
oargv = ps_strings->ps_argvstr;
|
2000-12-04 01:26:29 +00:00
|
|
|
for (i = len = 0; i < oargc; i++) {
|
2000-12-04 01:45:57 +00:00
|
|
|
/*
|
|
|
|
* The program may have scribbled into its
|
|
|
|
* argv array, e.g., to remove some arguments.
|
|
|
|
* If that has happened, break out before
|
|
|
|
* trying to call strlen on a NULL pointer.
|
|
|
|
*/
|
|
|
|
if (oargv[i] == NULL) {
|
|
|
|
oargc = i;
|
|
|
|
break;
|
|
|
|
}
|
2002-12-21 22:04:50 +00:00
|
|
|
snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s",
|
2000-12-04 01:26:29 +00:00
|
|
|
len ? " " : "", oargv[i]);
|
1999-11-17 21:12:17 +00:00
|
|
|
if (len)
|
|
|
|
len++;
|
2000-12-04 01:26:29 +00:00
|
|
|
len += strlen(oargv[i]);
|
2002-12-21 22:04:50 +00:00
|
|
|
if (len >= SPT_BUFSIZE)
|
1999-11-17 21:12:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ps_strings->ps_nargvstr = nargc;
|
|
|
|
ps_strings->ps_argvstr = nargvp;
|
1995-12-26 22:50:08 +00:00
|
|
|
} else {
|
1999-11-17 21:12:17 +00:00
|
|
|
/* style #2 - we can only restore our first arg :-( */
|
|
|
|
if (*obuf == '\0')
|
|
|
|
strncpy(obuf, OLD_PS_STRINGS->old_ps_argvstr,
|
2002-12-21 22:04:50 +00:00
|
|
|
SPT_BUFSIZE - 1);
|
1995-12-26 22:50:08 +00:00
|
|
|
OLD_PS_STRINGS->old_ps_nargvstr = 1;
|
1999-11-17 21:12:17 +00:00
|
|
|
OLD_PS_STRINGS->old_ps_argvstr = nargvp[0];
|
1995-12-26 22:50:08 +00:00
|
|
|
}
|
|
|
|
}
|