mirror of
https://gitlab.com/openconnect/ocserv.git
synced 2026-10-12 01:26:48 +08:00
set process title
This commit is contained in:
+2
-1
@@ -17,7 +17,8 @@ ocserv_SOURCES = main.c main-auth.c worker-vpn.c worker-auth.c tlslib.c \
|
||||
http-parser/http_parser.c ipc.h cookies.c worker-tun.c main-misc.c \
|
||||
vpn.h cookies.h tlslib.h http-parser/http_parser.h log.c tun.c tun.h \
|
||||
config.c pam.c pam.h worker-resume.c worker.h main-resume.c main.h \
|
||||
main-user.c cookies-gdbm.c cookies-hash.c worker-misc.c \
|
||||
main-user.c cookies-gdbm.c cookies-hash.c worker-misc.c setproctitle.h \
|
||||
setproctitle.c \
|
||||
$(CCAN_SOURCES)
|
||||
|
||||
ocserv_SOURCES += ocserv-args.def ocserv-args.c ocserv-args.h
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#include <gnutls/x509.h>
|
||||
#include <tlslib.h>
|
||||
#include "ipc.h"
|
||||
#include "setproctitle.h"
|
||||
|
||||
#include <main.h>
|
||||
#include <worker.h>
|
||||
@@ -498,6 +499,9 @@ int main(int argc, char** argv)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
initproctitle(argc, argv);
|
||||
setproctitle(PACKAGE_NAME, "main");
|
||||
|
||||
if (getuid() != 0) {
|
||||
fprintf(stderr, "This server requires root access to operate.\n");
|
||||
exit(1);
|
||||
@@ -613,6 +617,7 @@ int main(int argc, char** argv)
|
||||
/* close any open descriptors before
|
||||
* running the server
|
||||
*/
|
||||
setproctitle(PACKAGE_NAME, "worker");
|
||||
close(cmd_fd[0]);
|
||||
clear_lists(&s);
|
||||
|
||||
@@ -683,6 +688,7 @@ fork_failed:
|
||||
need_maintainance = 0;
|
||||
pid = fork();
|
||||
if (pid == 0) { /* child */
|
||||
setproctitle(PACKAGE_NAME, "maintainance");
|
||||
mslog(&s, NULL, LOG_INFO, "Performing maintainance");
|
||||
clear_lists(&s);
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/* This is taken from util-linux-2.22.2 which is under GPLv2.
|
||||
*/
|
||||
|
||||
/*
|
||||
* set process title for ps (from sendmail)
|
||||
*
|
||||
* Clobbers argv of our main procedure so ps(1) will display the title.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "setproctitle.h"
|
||||
|
||||
#ifndef SPT_BUFSIZE
|
||||
# define SPT_BUFSIZE 2048
|
||||
#endif
|
||||
|
||||
extern char **environ;
|
||||
|
||||
static char **argv0;
|
||||
static int argv_lth;
|
||||
|
||||
void initproctitle (int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
char **envp = environ;
|
||||
|
||||
/*
|
||||
* Move the environment so we can reuse the memory.
|
||||
* (Code borrowed from sendmail.)
|
||||
* WARNING: ugly assumptions on memory layout here;
|
||||
* if this ever causes problems, #undef DO_PS_FIDDLING
|
||||
*/
|
||||
for (i = 0; envp[i] != NULL; i++)
|
||||
continue;
|
||||
|
||||
environ = (char **) malloc(sizeof(char *) * (i + 1));
|
||||
if (environ == NULL)
|
||||
return;
|
||||
|
||||
for (i = 0; envp[i] != NULL; i++)
|
||||
if ((environ[i] = strdup(envp[i])) == NULL)
|
||||
return;
|
||||
environ[i] = NULL;
|
||||
|
||||
argv0 = argv;
|
||||
if (i > 0)
|
||||
argv_lth = envp[i-1] + strlen(envp[i-1]) - argv0[0];
|
||||
else
|
||||
argv_lth = argv0[argc-1] + strlen(argv0[argc-1]) - argv0[0];
|
||||
}
|
||||
|
||||
void setproctitle (const char *prog, const char *txt)
|
||||
{
|
||||
int i;
|
||||
char buf[SPT_BUFSIZE];
|
||||
|
||||
if (!argv0)
|
||||
return;
|
||||
|
||||
if (strlen(prog) + strlen(txt) + 5 > SPT_BUFSIZE)
|
||||
return;
|
||||
|
||||
sprintf(buf, "%s -- %s", prog, txt);
|
||||
|
||||
i = strlen(buf);
|
||||
if (i > argv_lth - 2) {
|
||||
i = argv_lth - 2;
|
||||
buf[i] = '\0';
|
||||
}
|
||||
memset(argv0[0], '\0', argv_lth); /* clear the memory area */
|
||||
strcpy(argv0[0], buf);
|
||||
|
||||
argv0[1] = NULL;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef UTIL_LINUX_SETPROCTITLE_H
|
||||
#define UTIL_LINUX_SETPROCTITLE_H
|
||||
|
||||
extern void initproctitle (int argc, char **argv);
|
||||
extern void setproctitle (const char *prog, const char *txt);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user