main: introduced main-worker-cmd.c

This commit is contained in:
Nikos Mavrogiannopoulos
2016-01-11 13:09:34 +01:00
parent e1dea8ae71
commit 43d55261e6
3 changed files with 54 additions and 53 deletions

View File

@@ -48,7 +48,7 @@ AUTH_SOURCES=auth/pam.c auth/pam.h auth/plain.c auth/plain.h auth/radius.c auth/
ACCT_SOURCES=acct/radius.c acct/radius.h acct/pam.c acct/pam.h
ocserv_SOURCES = main.c main-auth.c worker-vpn.c worker-auth.c tlslib.c \
cookies.c main-misc.c ip-lease.c ip-lease.h main-proc.c \
cookies.c main-worker-cmd.c ip-lease.c ip-lease.h main-proc.c \
vpn.h cookies.h tlslib.h log.c tun.c tun.h config-kkdcp.c \
config.c worker-resume.c worker.h sec-mod-resume.c main.h \
worker-http-handlers.c html.c html.h worker-http.c \

View File

@@ -46,6 +46,59 @@
#include <ccan/list/list.h>
#include <common.h>
/* Puts the provided PIN into the config's cgroup */
void put_into_cgroup(main_server_st * s, const char *_cgroup, pid_t pid)
{
char *name, *p, *savep;
char cgroup[128];
char file[_POSIX_PATH_MAX];
FILE *fd;
if (_cgroup == NULL)
return;
#ifdef __linux__
/* format: cpu,memory:cgroup-name */
strlcpy(cgroup, _cgroup, sizeof(cgroup));
name = strchr(cgroup, ':');
if (name == NULL) {
mslog(s, NULL, LOG_ERR, "error parsing cgroup name: %s",
cgroup);
return;
}
name[0] = 0;
name++;
p = strtok_r(cgroup, ",", &savep);
while (p != NULL) {
mslog(s, NULL, LOG_DEBUG,
"putting process %u to cgroup '%s:%s'", (unsigned)pid, p,
name);
snprintf(file, sizeof(file), "/sys/fs/cgroup/%s/%s/tasks", p,
name);
fd = fopen(file, "w");
if (fd == NULL) {
mslog(s, NULL, LOG_ERR, "cannot open: %s", file);
return;
}
if (fprintf(fd, "%u", (unsigned)pid) <= 0) {
mslog(s, NULL, LOG_ERR, "could not write to: %s", file);
}
fclose(fd);
p = strtok_r(NULL, ",", &savep);
}
return;
#else
mslog(s, NULL, LOG_DEBUG,
"Ignoring cgroup option as it is not supported on this system");
#endif
}
int send_cookie_auth_reply(main_server_st* s, struct proc_st* proc,
AUTHREP r)
{

View File

@@ -451,55 +451,3 @@ int handle_worker_commands(main_server_st * s, struct proc_st *proc)
return ret;
}
/* Puts the provided PIN into the config's cgroup */
void put_into_cgroup(main_server_st * s, const char *_cgroup, pid_t pid)
{
char *name, *p, *savep;
char cgroup[128];
char file[_POSIX_PATH_MAX];
FILE *fd;
if (_cgroup == NULL)
return;
#ifdef __linux__
/* format: cpu,memory:cgroup-name */
strlcpy(cgroup, _cgroup, sizeof(cgroup));
name = strchr(cgroup, ':');
if (name == NULL) {
mslog(s, NULL, LOG_ERR, "error parsing cgroup name: %s",
cgroup);
return;
}
name[0] = 0;
name++;
p = strtok_r(cgroup, ",", &savep);
while (p != NULL) {
mslog(s, NULL, LOG_DEBUG,
"putting process %u to cgroup '%s:%s'", (unsigned)pid, p,
name);
snprintf(file, sizeof(file), "/sys/fs/cgroup/%s/%s/tasks", p,
name);
fd = fopen(file, "w");
if (fd == NULL) {
mslog(s, NULL, LOG_ERR, "cannot open: %s", file);
return;
}
if (fprintf(fd, "%u", (unsigned)pid) <= 0) {
mslog(s, NULL, LOG_ERR, "could not write to: %s", file);
}
fclose(fd);
p = strtok_r(NULL, ",", &savep);
}
return;
#else
mslog(s, NULL, LOG_DEBUG,
"Ignoring cgroup option as it is not supported on this system");
#endif
}