diff options
Diffstat (limited to 'lib/libutil')
-rw-r--r-- | lib/libutil/Makefile | 3 | ||||
-rw-r--r-- | lib/libutil/setproctitle.3 | 103 | ||||
-rw-r--r-- | lib/libutil/setproctitle.c | 119 |
3 files changed, 224 insertions, 1 deletions
diff --git a/lib/libutil/Makefile b/lib/libutil/Makefile index bbc7aab..47366cc 100644 --- a/lib/libutil/Makefile +++ b/lib/libutil/Makefile @@ -2,6 +2,7 @@ LIB= util CFLAGS+=-DLIBC_SCCS -I/sys -SRCS= login.c login_tty.c logout.c logwtmp.c pty.c +SRCS= login.c login_tty.c logout.c logwtmp.c pty.c setproctitle.c +MAN3+= setproctitle.3 .include <bsd.lib.mk> diff --git a/lib/libutil/setproctitle.3 b/lib/libutil/setproctitle.3 new file mode 100644 index 0000000..dffed30 --- /dev/null +++ b/lib/libutil/setproctitle.3 @@ -0,0 +1,103 @@ +.\" 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. +.\" 3. This work was done expressly for inclusion into FreeBSD. Other use +.\" is permitted provided this notation is included. +.\" 4. Absolutely no warranty of function or purpose is made by the author +.\" Peter Wemm. +.\" 5. Modifications may be freely made to this file providing the above +.\" conditions are met. +.\" +.\" $Id$ +.\" +.\" The following requests are required for all man pages. +.Dd December 16, 1995 +.Os FreeBSD +.Dt SETPROCTITLE 3 +.Sh NAME +.Nm setproctitle +.Nd set the process title for +.Xr ps 1 +.Sh SYNOPSIS +.Ft void +.Fn setproctitle "const char *fmt" "..." +.Sh DESCRIPTION +The +.Nm setproctitle +library routine sets the process title that appears on the +.Xr ps 1 +command. +.Pp +The title is set from the executable's name, followed by the +result of a +.Xr printf 3 +style expansion of the arguments as specified by the +.Va fmt +argument. +.Pp +If +.Va fmt +is NULL, the process title is reset to simply the name of the executable. +.\" The following requests should be uncommented and used where appropriate. +.\" This next request is for sections 2 and 3 function return values only. +.\" .Sh RETURN VALUES +.\" This next request is for sections 1, 6, 7 & 8 only +.\" .Sh ENVIRONMENT +.\" .Sh FILES +.Sh EXAMPLES +To set the title on a daemon to indicate it's activity: +.Bd -literal -offset indent +setproctitle("talking to %s", inet_ntoa(addr)); +.Ed +.\" This next request is for sections 1, 6, 7 & 8 only +.\" (command return values (to shell) and fprintf/stderr type diagnostics) +.\" .Sh DIAGNOSTICS +.\" The next request is for sections 2 and 3 error and signal handling only. +.\" .Sh ERRORS +.Sh SEE ALSO +.Xr ps 1 , +.Xr w 1 , +.Xr kvm 3 , +.Xr kvm_getargv 3 , +.Xr printf 3 +.Sh STANDARDS +.Nm setproctitle +is implicitly non-standard. Other methods of causing the +.Xr ps 1 +command line to change, including copying over the argv[0] string are +also implicitly non-portable. It is preferable to use an operating system +supplied +.Nm setproctitle +if present. +.Pp +Unfortunately, it is possible that there are other calling conventions +to other versions of +.Nm setproctitle , +although none have been found by the author as yet. This is believed to be +the predominant convention. +.Pp +It is thought that the implementation is compatable with other systems, +including +.Nm NetBSD +and +.Nm BSD/OS . +.Sh HISTORY +.Nm setproctitle +first appeared in FreeBSD at version 2.2. Other operating systems have +similar functions. +.Sh AUTHORS +.Nm Peter Wemm <peter@FreeBSD.org> +stole the idea from the +.Nm Sendmail 8.7.3 +source code by +.Nm Eric Allman <eric@sendmail.org> . +.\" .Sh BUGS diff --git a/lib/libutil/setproctitle.c b/lib/libutil/setproctitle.c new file mode 100644 index 0000000..2faf976 --- /dev/null +++ b/lib/libutil/setproctitle.c @@ -0,0 +1,119 @@ +/* + * 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. + * 3. This work was done expressly for inclusion into FreeBSD. Other use + * is permitted provided this notation is included. + * 4. Absolutely no warranty of function or purpose is made by the author + * Peter Wemm. + * 5. Modifications may be freely made to this file providing the above + * conditions are met. + * + * $Id$ + */ + +#include <sys/types.h> +#include <sys/param.h> +#include <sys/exec.h> + +#include <vm/vm.h> +#include <vm/vm_param.h> +#include <vm/pmap.h> + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +/* + * 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))) + +#if defined(__STDC__) /* from other parts of sendmail */ +#include <stdarg.h> +#else +#include <varargs.h> +#endif + + +#define SPT_BUFSIZE 2048 /* from other parts of sendmail */ +extern char * __progname; /* is this defined in a .h anywhere? */ + +void +#if defined(__STDC__) +setproctitle(const char *fmt, ...) +#else +setproctitle(fmt, va_alist) + const char *fmt; + va_dcl +#endif +{ + char *p; + int len; + static char buf[SPT_BUFSIZE]; + static char *ps_argv[2]; + va_list ap; + +#if defined(__STDC__) + va_start(ap, fmt); +#else + va_start(ap); +#endif + + buf[sizeof(buf) - 1] = '\0'; + if (fmt) { + + /* print program name heading for grep */ + (void) snprintf(buf, sizeof(buf) - 1, "%s: ", __progname); + + /* + * can't use return from sprintf, as that is the count of how + * much it wanted to write, not how much it actually did. + */ + + len = strlen(buf); + + /* print the argument string */ + (void) vsnprintf(buf + len, sizeof(buf) - 1 - len, fmt, ap); + } else { + /* Idea from NetBSD - reset the title on fmt == NULL */ + strncpy(buf, __progname, sizeof(buf) - 1); + } + + va_end(ap); + + /* PS_STRINGS points to zeroed memory on a style #2 kernel */ + if (PS_STRINGS->ps_argvstr) { + /* style #3 */ + ps_argv[0] = buf; + ps_argv[1] = NULL; + PS_STRINGS->ps_nargvstr = 1; + PS_STRINGS->ps_argvstr = ps_argv; + } else { + /* style #2 */ + OLD_PS_STRINGS->old_ps_nargvstr = 1; + OLD_PS_STRINGS->old_ps_argvstr = buf; + } +} |