From c3ccaeeddc4d92bfe34cedaa6b71cb6e830fe280 Mon Sep 17 00:00:00 2001 From: "Anna (navi) Figueiredo Gomes" Date: Thu, 18 Jul 2024 17:29:21 +0200 Subject: librc, openrc-pam: instantiate user. service automatically it's created in /run/openrc/dynamic and linked to the service in /etc at login. Signed-off-by: Anna (navi) Figueiredo Gomes --- src/librc/librc-depend.c | 1 + src/librc/librc.c | 29 +++++++++++++++++++++++++++++ src/librc/rc.h.in | 10 ++++++++++ src/openrc-pam/openrc-pam.c | 14 ++++++++++++-- src/shared/helpers.h | 16 ++++++++++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/librc/librc-depend.c b/src/librc/librc-depend.c index a03e1a61..9f592cfe 100644 --- a/src/librc/librc-depend.c +++ b/src/librc/librc-depend.c @@ -700,6 +700,7 @@ static const char *const depdirs[] = "options", "exclusive", "scheduled", + "dynamic", "tmp", NULL }; diff --git a/src/librc/librc.c b/src/librc/librc.c index d218c8be..ee728b30 100644 --- a/src/librc/librc.c +++ b/src/librc/librc.c @@ -1317,6 +1317,35 @@ rc_service_delete(const char *runlevel, const char *service) return (r == 0); } +char * +rc_service_base(const char *service) +{ + char *dot = strchr(service, '.'); + if (!dot) + return NULL; + return xstrndup(service, dot - service); +} + +char * +rc_service_dylink(const char *service, const char *target) +{ + char *file = rc_service_resolve(service); + char *target_file = NULL; + if (!exists(file)) + goto out; + + xasprintf(&target_file, "%s/dynamic/%s", rc_service_dir(), target); + if (!exists(target_file) && symlink(file, target_file) == -1) { + free(target_file); + target_file = NULL; + goto out; + } + +out: + free(file); + return target_file; +} + RC_STRINGLIST * rc_services_scheduled_by(const char *service) { diff --git a/src/librc/rc.h.in b/src/librc/rc.h.in index 59fdfda3..794b4353 100644 --- a/src/librc/rc.h.in +++ b/src/librc/rc.h.in @@ -275,6 +275,16 @@ RC_STRINGLIST *rc_service_extra_commands(const char *); * @return pointer to full path of service */ char *rc_service_resolve(const char *); +/*! Gets the base service for a multiplexed service + * @param multiplexed service to check + * @return pointer to base service name or NULL if not multiplexed */ +char *rc_service_base(const char *); + +/*! Dynamically multiplexes a service into the runtime directory. + * @param service to link + * @param target service name */ +char *rc_service_dylink(const char *, const char *); + /*! Schedule a service to be started when another service starts * @param service that starts the scheduled service when started * @param service_to_start service that will be started */ diff --git a/src/openrc-pam/openrc-pam.c b/src/openrc-pam/openrc-pam.c index 241b123d..f0556ada 100644 --- a/src/openrc-pam/openrc-pam.c +++ b/src/openrc-pam/openrc-pam.c @@ -18,6 +18,7 @@ exec_openrc(pam_handle_t *pamh, bool opening) char *svc_name = NULL; char *pam_lock = NULL; char *logins, *rundir; + char *file; const char *username; struct passwd *pw; int count = 0, fd = -1; @@ -48,17 +49,26 @@ exec_openrc(pam_handle_t *pamh, bool opening) goto out; } + file = rc_service_resolve(svc_name); + if (!file) { + file = rc_service_dylink("user", svc_name); + if (!file) { + svc_unlock(pam_lock, fd); + goto out; + } + } + logins = rc_service_value_get(svc_name, "logins"); if (logins) sscanf(logins, "%d", &count); free(logins); if (opening && count == 0) { - pid = service_start(svc_name); + pid = service_start(file); rc_service_mark(svc_name, RC_SERVICE_HOTPLUGGED); count++; } else if (count > 0) { - pid = service_stop(svc_name); + pid = service_stop(file); count--; } diff --git a/src/shared/helpers.h b/src/shared/helpers.h index dee41b75..36a25a2d 100644 --- a/src/shared/helpers.h +++ b/src/shared/helpers.h @@ -91,6 +91,22 @@ RC_UNUSED static char *xstrdup(const char *str) /* NOTREACHED */ } +RC_UNUSED static char *xstrndup(const char *str, size_t n) +{ + char *value; + + if (!str) + return (NULL); + + value = strndup(str, n); + + if (value) + return (value); + + ERRX; + /* NOTREACHED */ +} + #undef ERRX /* -- cgit v1.2.3