Blame


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