xref: /netbsd-src/external/bsd/tmux/dist/compat/setproctitle.c (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
14e179ddaSchristos /*
24e179ddaSchristos  * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
34e179ddaSchristos  *
44e179ddaSchristos  * Permission to use, copy, modify, and distribute this software for any
54e179ddaSchristos  * purpose with or without fee is hereby granted, provided that the above
64e179ddaSchristos  * copyright notice and this permission notice appear in all copies.
74e179ddaSchristos  *
84e179ddaSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
94e179ddaSchristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
104e179ddaSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
114e179ddaSchristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
124e179ddaSchristos  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
134e179ddaSchristos  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
144e179ddaSchristos  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
154e179ddaSchristos  */
164e179ddaSchristos 
174e179ddaSchristos #include <sys/types.h>
184e179ddaSchristos 
194e179ddaSchristos #include <stdarg.h>
20*f8cf1a91Swiz #include <stdlib.h>
214e179ddaSchristos #include <string.h>
224e179ddaSchristos 
234e179ddaSchristos #include "compat.h"
244e179ddaSchristos 
254e179ddaSchristos #if defined(HAVE_PRCTL) && defined(HAVE_PR_SET_NAME)
264e179ddaSchristos 
274e179ddaSchristos #include <sys/prctl.h>
284e179ddaSchristos 
294e179ddaSchristos void
304e179ddaSchristos setproctitle(const char *fmt, ...)
314e179ddaSchristos {
324e179ddaSchristos 	char	title[16], name[16], *cp;
334e179ddaSchristos 	va_list	ap;
344e179ddaSchristos 	int	used;
354e179ddaSchristos 
364e179ddaSchristos 	va_start(ap, fmt);
374e179ddaSchristos 	vsnprintf(title, sizeof title, fmt, ap);
384e179ddaSchristos 	va_end(ap);
394e179ddaSchristos 
404e179ddaSchristos 	used = snprintf(name, sizeof name, "%s: %s", getprogname(), title);
414e179ddaSchristos 	if (used >= (int)sizeof name) {
424e179ddaSchristos 		cp = strrchr(name, ' ');
434e179ddaSchristos 		if (cp != NULL)
444e179ddaSchristos 			*cp = '\0';
454e179ddaSchristos 	}
464e179ddaSchristos 	prctl(PR_SET_NAME, name);
474e179ddaSchristos }
484e179ddaSchristos #else
494e179ddaSchristos void
504e179ddaSchristos setproctitle(__unused const char *fmt, ...)
514e179ddaSchristos {
524e179ddaSchristos }
534e179ddaSchristos #endif
54