From: Arseny Maslennikov <arseny@altlinux.org> To: devel@lists.altlinux.org, ldv@altlinux.org Cc: Arseny Maslennikov <arseny@altlinux.org>, Alexey Gladkov <legion@altlinux.org> Subject: [devel] [PATCH v2 6/6] Add preliminary cgroup support Date: Thu, 22 Oct 2020 14:43:43 +0300 Message-ID: <20201022114343.1810141-7-arseny@altlinux.org> (raw) In-Reply-To: <20201022114343.1810141-1-arseny@altlinux.org> From: Alexey Gladkov <legion@altlinux.org> A new configuration option allows to put all request handlers to a particular cgroup path, which is built according to a specified template. Note that cgroup1 API is not supported in favour of cgroup2. Signed-off-by: Alexey Gladkov <legion@altlinux.org> Signed-off-by: Arseny Maslennikov <arseny@altlinux.org> --- hasher-priv/Makefile | 2 +- hasher-priv/caller_task.c | 3 + hasher-priv/cgroup.c | 119 ++++++++++++++++++++++++++++++++++++++ hasher-priv/config.c | 5 ++ hasher-priv/priv.h | 2 + hasher-priv/server.conf | 9 +++ 6 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 hasher-priv/cgroup.c diff --git a/hasher-priv/Makefile b/hasher-priv/Makefile index 6e6b1e5..be25fb7 100644 --- a/hasher-priv/Makefile +++ b/hasher-priv/Makefile @@ -52,7 +52,7 @@ server_SRC = hasher-privd.c \ chdir.c chdiruid.c chid.c child.c chrootuid.c cmdline.c \ config.c fds.c getconf.c getugid.c ipc.c killuid.c io_log.c io_x11.c \ makedev.c mount.c net.c parent.c pass.c pty.c signal.c tty.c \ - unshare.c xmalloc.c x11.c + unshare.c xmalloc.c x11.c cgroup.c server_OBJ = $(server_SRC:.c=.o) DEP = $(SRC:.c=.d) $(server_SRC:.c=.d) diff --git a/hasher-priv/caller_task.c b/hasher-priv/caller_task.c index 46745a2..48d8fbd 100644 --- a/hasher-priv/caller_task.c +++ b/hasher-priv/caller_task.c @@ -100,6 +100,9 @@ caller_task(struct task *task) setproctitle("%s for [%u:%u]: %s", "task handler", caller_uid, caller_num, task2str(task->type)); + if (join_cgroup() < 0) + exit(rc); + if ((rc = reopen_iostreams(task->stdin, task->stdout, task->stderr)) < 0) exit(rc); diff --git a/hasher-priv/cgroup.c b/hasher-priv/cgroup.c new file mode 100644 index 0000000..ac14938 --- /dev/null +++ b/hasher-priv/cgroup.c @@ -0,0 +1,119 @@ + +/* + Copyright (C) 2019 Alexey Gladkov <legion@altlinux.org> + + The cgroup helper for hasher-privd program. + + SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <sys/param.h> +#include <sys/types.h> +#include <sys/stat.h> + +#include <unistd.h> +#include <stdio.h> +#include <stdarg.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> + +#include "logging.h" +#include "priv.h" + +int +join_cgroup(void) +{ + int ret = 0; + + if (!server_cgroup_template) + return ret; + + char cgroup_path[MAXPATHLEN]; + + size_t i, j, escape; + size_t len = strlen(server_cgroup_template); + int fd = -1; + + i = j = escape = 0; + + for (; i < len; i++) { + if (j > sizeof(cgroup_path)) { + err("path too long"); + ret = -1; + goto fail; + } + + if (escape) { + ssize_t n = 0; + char *p = cgroup_path + j; + size_t sz = (size_t) (p - cgroup_path); + + switch (server_cgroup_template[i]) { + case 'u': + n = snprintf(p, sz, "%s", caller_user); + break; + case 'U': + n = snprintf(p, sz, "%u", caller_uid); + break; + case 'G': + n = snprintf(p, sz, "%u", caller_gid); + break; + case 'N': + n = snprintf(p, sz, "%u", caller_num); + break; + case '%': + n = snprintf(p, sz, "%%"); + break; + } + + if (n <= 0) { + err("unable to expand escape sequence: %%%c", + server_cgroup_template[i]); + ret = -1; + goto fail; + } + + j += (size_t) n; + + escape = 0; + continue; + + } else if (server_cgroup_template[i] == '%') { + escape = 1; + continue; + + } else if (server_cgroup_template[i] == '/' && j > 0) { + cgroup_path[j] = '\0'; + + errno = 0; + if (mkdir(cgroup_path, 0755) < 0 && errno != EEXIST) { + err("mkdir: %s: errno=%d: %m", cgroup_path, errno); + ret = -1; + goto fail; + } + } + + cgroup_path[j++] = server_cgroup_template[i]; + } + + cgroup_path[j] = '\0'; + + if ((fd = open(cgroup_path, O_CREAT | O_WRONLY | O_CLOEXEC, 0644)) < 0) { + err("open: %s: %m", cgroup_path); + ret = -1; + goto fail; + } + + if (dprintf(fd, "%d\n", getpid()) < 0) { + err("dprintf: %s: unable to write pid", cgroup_path); + ret = -1; + } +fail: + if (fd >= 0 && close(fd) < 0) { + err("close: %s: %m", cgroup_path); + ret = -1; + } + + return ret; +} diff --git a/hasher-priv/config.c b/hasher-priv/config.c index ce83e15..9729982 100644 --- a/hasher-priv/config.c +++ b/hasher-priv/config.c @@ -30,6 +30,7 @@ const char *const *chroot_prefix_list; const char *chroot_prefix_path; const char *change_user1, *change_user2; char *server_access_group = NULL; +char *server_cgroup_template = NULL; char *server_pidfile = NULL; const char *term; const char *x11_display, *x11_key; @@ -671,6 +672,9 @@ set_server_config(const char *name, const char *value, const char *filename) } else if (!strcasecmp("access_group", name)) { free(server_access_group); server_access_group = xstrdup(value); + } else if (!strcasecmp("cgroup_template", name)) { + free(server_cgroup_template); + server_cgroup_template = xstrdup(value); } else { bad_option_name(name, filename); } @@ -771,4 +775,5 @@ free_server_configuration(void) { free(server_pidfile); free(server_access_group); + free(server_cgroup_template); } diff --git a/hasher-priv/priv.h b/hasher-priv/priv.h index 7d5a5a5..6e8a032 100644 --- a/hasher-priv/priv.h +++ b/hasher-priv/priv.h @@ -120,6 +120,7 @@ int do_chrootuid2(void); int process_caller_task(int, struct task *); pid_t fork_server(int, uid_t, gid_t, unsigned); +int join_cgroup(void); extern const char *chroot_path; extern const char **chroot_argv; @@ -162,6 +163,7 @@ extern work_limit_t wlimit; extern int server_log_priority; extern unsigned long server_session_timeout; extern char *server_access_group; +extern char *server_cgroup_template; extern char *server_pidfile; extern gid_t server_gid; diff --git a/hasher-priv/server.conf b/hasher-priv/server.conf index 2d740a6..57744f9 100644 --- a/hasher-priv/server.conf +++ b/hasher-priv/server.conf @@ -11,3 +11,12 @@ session_timeout=3600 # Allow users of this group to interact with hasher-privd via the control socket. access_group=hashman + +# Template for cgroup path to which task handler should be added. +# +# %u -- Session's user name. +# %U -- Session's user numeric ID. +# %G -- Session's group numeric ID. +# %N -- Session's user number. +# +#cgroup_template=/sys/fs/cgroup2/hasher-priv/%u/cgroup.procs -- 2.25.4
prev parent reply other threads:[~2020-10-22 11:43 UTC|newest] Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-10-22 11:43 [devel] [PATCH hasher-priv v2 0/6] hasher-privd Arseny Maslennikov 2020-10-22 11:43 ` [devel] [PATCH v2 1/6] Turn hasher-priv into a daemon Arseny Maslennikov 2020-10-22 11:43 ` [devel] [PATCH v2 2/6] Link with libsetproctitle by Dmitry V. Levin Arseny Maslennikov 2020-10-22 11:43 ` [devel] [PATCH v2 3/6] daemon: set titles for subprocesses Arseny Maslennikov 2020-10-22 11:43 ` [devel] [PATCH v2 4/6] Add systemd and sysvinit service files Arseny Maslennikov 2020-10-22 11:49 ` [devel] [PATCH v2 4/6] Add systemd and sysvinit service files: missing signoff Arseny Maslennikov 2020-10-22 11:43 ` [devel] [PATCH v2 5/6] Install hasher-priv without set ugids Arseny Maslennikov 2020-10-22 11:43 ` Arseny Maslennikov [this message]
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20201022114343.1810141-7-arseny@altlinux.org \ --to=arseny@altlinux.org \ --cc=devel@lists.altlinux.org \ --cc=ldv@altlinux.org \ --cc=legion@altlinux.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
ALT Linux Team development discussions This inbox may be cloned and mirrored by anyone: git clone --mirror http://lore.altlinux.org/devel/0 devel/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 devel devel/ http://lore.altlinux.org/devel \ devel@altlinux.org devel@altlinux.ru devel@lists.altlinux.org devel@lists.altlinux.ru devel@linux.iplabs.ru mandrake-russian@linuxteam.iplabs.ru sisyphus@linuxteam.iplabs.ru public-inbox-index devel Example config snippet for mirrors. Newsgroup available over NNTP: nntp://lore.altlinux.org/org.altlinux.lists.devel AGPL code for this site: git clone https://public-inbox.org/public-inbox.git