1 /*
2  * Copyright (C) 2013 Nikos Mavrogiannopoulos
3  *
4  * This file is part of ocserv.
5  *
6  * ocserv is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * ocserv is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <config.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include "main.h"
24 
25 #if !defined(HAVE_SETPROCTITLE)
26 
27 # if defined(__linux__)
28 #  include <sys/prctl.h>
29 
30 /* This sets the proccess title as shown in top, but not in ps (*@#%@).
31  * To change the ps name in Linux, one needs to do master black magic
32  * trickery (see util-linux setproctitle).
33  */
setproctitle(const char * fmt,...)34 void setproctitle (const char *fmt, ...)
35 {
36 	char name[16];
37 	va_list args;
38 
39 	va_start(args, fmt);
40 	vsnprintf(name, sizeof(name)-1, fmt, args);
41 	va_end(args);
42 
43 #  ifdef PR_SET_NAME
44 	prctl (PR_SET_NAME, name);
45 #  endif
46 	/* Copied systemd's implementation under LGPL by Lennart Poettering */
47 	if (saved_argc > 0) {
48 		int i;
49 
50 		if (saved_argv[0])
51 			strncpy(saved_argv[0], name, strlen(saved_argv[0]));
52 		for (i = 1; i < saved_argc; i++) {
53 			if (!saved_argv[i])
54 				break;
55 			memset(saved_argv[i], 0, strlen(saved_argv[i]));
56 		}
57 	}
58 }
59 # else /* not linux */
60 
setproctitle(const char * fmt,...)61 void setproctitle (const char *fmt, ...)
62 {
63 	return;
64 }
65 
66 # endif /* __linux__ */
67 
68 #endif /* HAVE_SETPROCTITLE */
69