Skip to content

Commit 704dcdc

Browse files
N-R-Knavi-desu
authored andcommitted
openrc-user: use rc_exec
1 parent 73c342c commit 704dcdc

2 files changed

Lines changed: 14 additions & 24 deletions

File tree

src/openrc-user/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
executable('openrc-user', ['openrc-user.c'],
1+
executable('openrc-user', ['openrc-user.c', rc_exec_c],
22
c_args: [cc_pam_flags],
33
dependencies: pam_dep,
44
include_directories: [incdir, einfo_incdir, rc_incdir],

src/openrc-user/openrc-user.c

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,23 @@
1919

2020
#include "helpers.h"
2121
#include "rc.h"
22+
#include "rc_exec.h"
2223

2324
static bool spawn_openrc(const struct passwd *user, bool start) {
24-
int status = 0;
25-
pid_t pid;
26-
27-
switch ((pid = fork())) {
28-
case 0:
29-
if (setgid(user->pw_gid) == -1 || setuid(user->pw_uid) == -1) {
30-
elog(LOG_ERR, "Failed to set user ids: %s", strerror(errno));
31-
exit(-1);
32-
}
33-
34-
execl(user->pw_shell, "-", "-c", start ? USER_SH "start" : USER_SH "stop", NULL);
35-
elog(LOG_ERR, "execl: %s", strerror(errno));
36-
exit(-1);
37-
break;
38-
39-
case -1:
40-
elog(LOG_ERR, "fork: %s", strerror(errno));
25+
const char *argv[] = {
26+
user->pw_shell, "-", "-c",
27+
start ? USER_SH "start" : USER_SH "stop", NULL
28+
};
29+
struct exec_result res;
30+
struct exec_args args = exec_init(argv);
31+
args.uid = user->pw_uid;
32+
args.gid = user->pw_gid;
33+
res = do_exec(&args);
34+
if (res.pid < 0) {
35+
elog(LOG_ERR, "do_exec: %s", strerror(errno));
4136
return false;
42-
43-
default:
44-
waitpid(pid, &status, 0);
45-
break;
4637
}
47-
48-
return status == 0;
38+
return rc_waitpid(res.pid) == 0;
4939
}
5040

5141
int main(int argc, char **argv) {

0 commit comments

Comments
 (0)