Blame


1 c4e2e0d0 2022-07-16 thomas /*
2 c4e2e0d0 2022-07-16 thomas * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
3 c4e2e0d0 2022-07-16 thomas *
4 c4e2e0d0 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
5 c4e2e0d0 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
6 c4e2e0d0 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
7 c4e2e0d0 2022-07-16 thomas *
8 c4e2e0d0 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c4e2e0d0 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c4e2e0d0 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c4e2e0d0 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c4e2e0d0 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13 c4e2e0d0 2022-07-16 thomas * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14 c4e2e0d0 2022-07-16 thomas * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 c4e2e0d0 2022-07-16 thomas */
16 c4e2e0d0 2022-07-16 thomas
17 c4e2e0d0 2022-07-16 thomas #include <sys/types.h>
18 c4e2e0d0 2022-07-16 thomas
19 c4e2e0d0 2022-07-16 thomas #include <stdarg.h>
20 c4e2e0d0 2022-07-16 thomas #include <string.h>
21 c4e2e0d0 2022-07-16 thomas
22 c4e2e0d0 2022-07-16 thomas #include "got_compat.h"
23 c4e2e0d0 2022-07-16 thomas
24 c4e2e0d0 2022-07-16 thomas #if defined(HAVE_PRCTL) && defined(HAVE_PR_SET_NAME)
25 c4e2e0d0 2022-07-16 thomas
26 c4e2e0d0 2022-07-16 thomas #include <sys/prctl.h>
27 c4e2e0d0 2022-07-16 thomas
28 c4e2e0d0 2022-07-16 thomas void
29 c4e2e0d0 2022-07-16 thomas setproctitle(const char *fmt, ...)
30 c4e2e0d0 2022-07-16 thomas {
31 c4e2e0d0 2022-07-16 thomas char title[16], name[16], *cp;
32 c4e2e0d0 2022-07-16 thomas va_list ap;
33 c4e2e0d0 2022-07-16 thomas int used;
34 c4e2e0d0 2022-07-16 thomas
35 c4e2e0d0 2022-07-16 thomas va_start(ap, fmt);
36 c4e2e0d0 2022-07-16 thomas vsnprintf(title, sizeof title, fmt, ap);
37 c4e2e0d0 2022-07-16 thomas va_end(ap);
38 c4e2e0d0 2022-07-16 thomas
39 c4e2e0d0 2022-07-16 thomas used = snprintf(name, sizeof name, "%s: %s", getprogname(), title);
40 c4e2e0d0 2022-07-16 thomas if (used >= (int)sizeof name) {
41 c4e2e0d0 2022-07-16 thomas cp = strrchr(name, ' ');
42 c4e2e0d0 2022-07-16 thomas if (cp != NULL)
43 c4e2e0d0 2022-07-16 thomas *cp = '\0';
44 c4e2e0d0 2022-07-16 thomas }
45 c4e2e0d0 2022-07-16 thomas prctl(PR_SET_NAME, name);
46 c4e2e0d0 2022-07-16 thomas }
47 c4e2e0d0 2022-07-16 thomas #else
48 c4e2e0d0 2022-07-16 thomas void
49 c4e2e0d0 2022-07-16 thomas setproctitle(__unused const char *fmt, ...)
50 c4e2e0d0 2022-07-16 thomas {
51 c4e2e0d0 2022-07-16 thomas }
52 c4e2e0d0 2022-07-16 thomas #endif