xref: /dflybsd-src/lib/libc/upmap/ukp_setproctitle.c (revision b866b1dac9e41035fffd5f4cec3b0a24a95916b6)
187116512SMatthew Dillon /*
287116512SMatthew Dillon  * Copyright (c) 2014 The DragonFly Project.  All rights reserved.
387116512SMatthew Dillon  *
487116512SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
587116512SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
687116512SMatthew Dillon  *
787116512SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
887116512SMatthew Dillon  * modification, are permitted provided that the following conditions
987116512SMatthew Dillon  * are met:
1087116512SMatthew Dillon  *
1187116512SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
1287116512SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
1387116512SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
1487116512SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
1587116512SMatthew Dillon  *    the documentation and/or other materials provided with the
1687116512SMatthew Dillon  *    distribution.
1787116512SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
1887116512SMatthew Dillon  *    contributors may be used to endorse or promote products derived
1987116512SMatthew Dillon  *    from this software without specific, prior written permission.
2087116512SMatthew Dillon  *
2187116512SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2287116512SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2387116512SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2487116512SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
2587116512SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2687116512SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2787116512SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2887116512SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2987116512SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3087116512SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3187116512SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3287116512SMatthew Dillon  * SUCH DAMAGE.
3387116512SMatthew Dillon  */
3487116512SMatthew Dillon 
35806a5ed7SSascha Wildner #include "namespace.h"
3687116512SMatthew Dillon #include <sys/cdefs.h>
3787116512SMatthew Dillon #include <sys/types.h>
3887116512SMatthew Dillon #include <sys/syscall.h>
3987116512SMatthew Dillon #include <sys/upmap.h>
4087116512SMatthew Dillon #include <sys/time.h>
4187116512SMatthew Dillon #include <machine/cpufunc.h>
4287116512SMatthew Dillon #include <errno.h>
4387116512SMatthew Dillon #include <time.h>
4487116512SMatthew Dillon #include <stdio.h>
4587116512SMatthew Dillon #include <stdlib.h>
4687116512SMatthew Dillon #include <unistd.h>
47806a5ed7SSascha Wildner 
4887116512SMatthew Dillon #include "libc_private.h"
4987116512SMatthew Dillon #include "upmap.h"
50806a5ed7SSascha Wildner #include "un-namespace.h"
5187116512SMatthew Dillon 
5287116512SMatthew Dillon static int fast_spt_state;
5387116512SMatthew Dillon static int fast_spt_count;
5487116512SMatthew Dillon static char *sptbuf;
5587116512SMatthew Dillon 
5687116512SMatthew Dillon /*
5787116512SMatthew Dillon  * This is called from gen/setproctitle.c
5887116512SMatthew Dillon  */
5987116512SMatthew Dillon int
__ukp_spt(const char * fmt,va_list ap)6087116512SMatthew Dillon __ukp_spt(const char *fmt, va_list ap)
6187116512SMatthew Dillon {
6287116512SMatthew Dillon 	size_t n;
6387116512SMatthew Dillon 	size_t t;
6487116512SMatthew Dillon 
65*b866b1daSSascha Wildner 	if (fmt == NULL)
66*b866b1daSSascha Wildner 		return -1;
6787116512SMatthew Dillon 	if (fast_spt_state == 0 && fast_spt_count++ >= 10) {
6887116512SMatthew Dillon 		__upmap_map(&sptbuf, &fast_spt_state, UPTYPE_PROC_TITLE);
6987116512SMatthew Dillon 		__upmap_map(NULL, &fast_spt_state, 0);
7087116512SMatthew Dillon 	}
7187116512SMatthew Dillon 	if (fast_spt_state > 0) {
7287116512SMatthew Dillon 		t = UKPLEN_DECODE(UPTYPE_PROC_TITLE);
73*b866b1daSSascha Wildner 		if (fmt[0] == '-') {
74*b866b1daSSascha Wildner 			fmt++;
75*b866b1daSSascha Wildner 			n = 0;
76*b866b1daSSascha Wildner 		} else {
77806a5ed7SSascha Wildner 			n = snprintf(sptbuf, t, "%s: ", _getprogname());
78*b866b1daSSascha Wildner 		}
7987116512SMatthew Dillon 		vsnprintf(sptbuf + n, t - n, fmt, ap);
8087116512SMatthew Dillon 		return 0;
8187116512SMatthew Dillon 	} else {
8287116512SMatthew Dillon 		return -1;
8387116512SMatthew Dillon 	}
8487116512SMatthew Dillon }
85