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$");
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;
65108163Sbbraun	static char *buf = NULL;
66108163Sbbraun	static char *obuf = NULL;
6753297Sbrian	static char **oargv, *kbuf;
6853297Sbrian	static int oargc = -1;
69108163Sbbraun	static char *nargv[2] = { NULL, 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
78108163Sbbraun	if (buf == NULL) {
79108163Sbbraun		buf = malloc(SPT_BUFSIZE);
80108163Sbbraun		if (buf == NULL)
81108163Sbbraun			return;
82108163Sbbraun		nargv[0] = buf;
83108163Sbbraun	}
84108163Sbbraun
85108163Sbbraun	if (obuf == NULL ) {
86108163Sbbraun		obuf = malloc(SPT_BUFSIZE);
87108163Sbbraun		if (obuf == NULL)
88108163Sbbraun			return;
89117102Salfred		*obuf = '\0';
90108163Sbbraun	}
91108163Sbbraun
9213039Speter	va_start(ap, fmt);
9313039Speter
9413039Speter	if (fmt) {
95108163Sbbraun		buf[SPT_BUFSIZE - 1] = '\0';
9613039Speter
9764094Sps		if (fmt[0] == '-') {
9864094Sps			/* skip program name prefix */
9964094Sps			fmt++;
10064094Sps			len = 0;
10164094Sps		} else {
10264094Sps			/* print program name heading for grep */
103108163Sbbraun			(void)snprintf(buf, SPT_BUFSIZE, "%s: ", _getprogname());
10464094Sps			len = strlen(buf);
10564094Sps		}
10613039Speter
10713039Speter		/* print the argument string */
108108163Sbbraun		(void) vsnprintf(buf + len, SPT_BUFSIZE - len, fmt, ap);
10913039Speter
11053297Sbrian		nargvp = nargv;
11153297Sbrian		nargc = 1;
11253297Sbrian		kbuf = buf;
11353297Sbrian	} else if (*obuf != '\0') {
11453297Sbrian  		/* Idea from NetBSD - reset the title on fmt == NULL */
11553297Sbrian		nargvp = oargv;
11653297Sbrian		nargc = oargc;
11753297Sbrian		kbuf = obuf;
11853297Sbrian	} else
11953297Sbrian		/* Nothing to restore */
12053297Sbrian		return;
12153297Sbrian
12213039Speter	va_end(ap);
12313039Speter
12453239Sphk	/* Set the title into the kernel cached command line */
12553239Sphk	oid[0] = CTL_KERN;
12653239Sphk	oid[1] = KERN_PROC;
12753239Sphk	oid[2] = KERN_PROC_ARGS;
12853239Sphk	oid[3] = getpid();
12953297Sbrian	sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1);
13053239Sphk
13135490Sdg	if (ps_strings == NULL) {
13241875Sbde		len = sizeof(ul_ps_strings);
13341875Sbde		if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL,
13441875Sbde		    0) == -1)
13541875Sbde			ul_ps_strings = PS_STRINGS;
13641875Sbde		ps_strings = (struct ps_strings *)ul_ps_strings;
13735490Sdg	}
13814236Speter
13913039Speter	/* PS_STRINGS points to zeroed memory on a style #2 kernel */
14014236Speter	if (ps_strings->ps_argvstr) {
14113039Speter		/* style #3 */
14253297Sbrian		if (oargc == -1) {
14353297Sbrian			/* Record our original args */
14453297Sbrian			oargc = ps_strings->ps_nargvstr;
14553297Sbrian			oargv = ps_strings->ps_argvstr;
14669558Sjdp			for (i = len = 0; i < oargc; i++) {
14769560Sjdp				/*
14869560Sjdp				 * The program may have scribbled into its
14969560Sjdp				 * argv array, e.g., to remove some arguments.
15069560Sjdp				 * If that has happened, break out before
15169560Sjdp				 * trying to call strlen on a NULL pointer.
15269560Sjdp				 */
15369560Sjdp				if (oargv[i] == NULL) {
15469560Sjdp					oargc = i;
15569560Sjdp					break;
15669560Sjdp				}
157108163Sbbraun				snprintf(obuf + len, SPT_BUFSIZE - len, "%s%s",
15869558Sjdp				    len ? " " : "", oargv[i]);
15953297Sbrian				if (len)
16053297Sbrian					len++;
16169558Sjdp				len += strlen(oargv[i]);
162108163Sbbraun				if (len >= SPT_BUFSIZE)
16353297Sbrian					break;
16453297Sbrian			}
16553297Sbrian		}
16653297Sbrian		ps_strings->ps_nargvstr = nargc;
16753297Sbrian		ps_strings->ps_argvstr = nargvp;
16813039Speter	} else {
16953297Sbrian		/* style #2 - we can only restore our first arg :-( */
17053297Sbrian		if (*obuf == '\0')
17153297Sbrian			strncpy(obuf, OLD_PS_STRINGS->old_ps_argvstr,
172108163Sbbraun			    SPT_BUFSIZE - 1);
17313039Speter		OLD_PS_STRINGS->old_ps_nargvstr = 1;
17453297Sbrian		OLD_PS_STRINGS->old_ps_argvstr = nargvp[0];
17513039Speter	}
17613039Speter}
177