.\" $OpenBSD: mdoc.template,v 1.15 2014/03/31 00:09:54 dlg Exp $ .\" .\" Copyright (c) 2022 Guilherme Janczak .\" .\" Permission to use, copy, modify, and distribute this software for any .\" purpose with or without fee is hereby granted, provided that the above .\" copyright notice and this permission notice appear in all copies. .\" .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" .Dd $Mdocdate: April 20 2022 $ .Dt SETPROCTITLE 3 .Os .Sh NAME .Nm setproctitle_init , .Nm setproctitle .Nd set process title .Sh SYNOPSIS .In stdlib.h .Fd #if defined(setproctitle_init) .Ft void .Fn setproctitle_init "int argc" "char *argv[]" "char *envp[]" .Fd #endif .Ft void .Fn setproctitle "const char *fmt" "..." .Sh DESCRIPTION If .Dv setproctitle_init is defined, call .Fn setproctitle_init "argc" "argv" "environ" before using .Fa argc , .Fa argv , or the environment. .Pp The .Fn setproctitle function sets the process' title; the title is set to the program's name as returned by .Xr getprogname 3 , followed by a colon, a space, and the output of .Xr printf 3 with .Fa fmt and .Fa ... as arguments. The title is limited to 2048 bytes. .Pp If .Fa fmt is .Dv NULL , the title is set to the program's name. .Pp If .Fn setproctitle is called before .Fn setproctitle_init , it is no-op. .Fn setproctitle_init is compatible with libbsd. .Sh EXAMPLES As far as .Fn setproctitle_init , .Fn setproctitle , and their users are concerned, there are 3 kinds of systems: .Bl -bullet .It Systems in which libobsd is not used because it's not needed. .It Systems in which libobsd is in use, and libobsd's .Fn setproctitle does not need initialization. .It Systems in which libobsd is in use, and libobsd's .Fn setproctitle needs initialization. .El .Pp The following is a complete program listing which works unmodified across all 3 kinds of systems, and showcases proper usage of .Fn setproctitle_init and .Fn setproctitle : .Bd -literal -offset indent #include #include #include int main(int argc, char *argv[]) { int ch; char *tflag; #if defined(setproctitle_init) extern char **environ; setproctitle_init(argc, argv, environ); #endif tflag = NULL; while ((ch = getopt(argc, argv, "t:")) != -1) { switch(ch) { case 't': tflag = optarg; break; default: exit(1); } } if (tflag == NULL) setproctitle("libobsd setproctitle"); else setproctitle("%s", tflag); fputs("Press enter to exit: ", stdout); fflush(stdout); getchar(); } .Ed .Sh SEE ALSO .Xr getprogname 3 , .Xr vsnprintf 3 .Sh CAVEATS .Fn setproctitle_init is a necessary libbsd extension and does not exist on .Ox . .Pp The process' title may be truncated to less than 2048 bytes in some cases. .Pp A .Fn setproctitle NULL call doesn't set the title to the original title. It only sets the title to the program name, the original might have contained the program's path and arguments. .Pp Do not pass an user-supplied string as the value of .Fa fmt . Format specifiers in .Fa fmt will be interpreted and can be used to manipulate the stack, this is a security vulnerability. Always use the secure idiom: .Bd -literal -offset indent setproctitle("%s", str); .Ed