forked from google/xsecurelock
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigured_command.c
More file actions
77 lines (64 loc) · 1.98 KB
/
Copy pathconfigured_command.c
File metadata and controls
77 lines (64 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// SPDX-License-Identifier: Apache-2.0
#include "config.h"
#include "configured_command.h"
#include <stdbool.h>
#include <signal.h> // for SIGPIPE
#include <stdio.h> // for snprintf
#include <stdlib.h> // for EXIT_FAILURE, EXIT_SUCCESS
#include <unistd.h> // for _exit, execl, fork, pid_t, setsid
#include "env_settings.h"
#include "logging.h"
#include "process_util.h"
#include "xsl_signal.h"
static void ExecShellOrExit(const char *label, const char *command) {
execl("/bin/sh", "sh", "-c", command, (char *)NULL);
LogErrno("execl /bin/sh -c for %s", label);
_exit(EXIT_FAILURE);
}
bool FormatKeyCommandEnvName(char *dst, size_t dst_size, const char *keyname) {
int len = snprintf(dst, dst_size, "XSECURELOCK_KEY_%s_COMMAND", keyname);
if (len <= 0 || (size_t)len >= dst_size) {
Log("Wow, pretty long keysym names you got there");
return false;
}
return true;
}
bool RunShellCommandValue(const char *label, const char *command,
bool background) {
if (command == NULL || *command == '\0') {
return true;
}
pid_t childpid = ForkWithoutSigHandlers();
if (childpid == -1) {
LogErrno("fork for %s", label);
return false;
}
if (childpid == 0) {
ResetSignalToDefaultOrExit(SIGPIPE, label);
if (background) {
if (setsid() == (pid_t)-1) {
LogErrno("setsid for %s", label);
_exit(EXIT_FAILURE);
}
pid_t grandchildpid = fork();
if (grandchildpid == -1) {
LogErrno("fork for %s", label);
_exit(EXIT_FAILURE);
}
if (grandchildpid != 0) {
_exit(EXIT_SUCCESS);
}
}
ExecShellOrExit(label, command);
}
int status = 0;
if (!WaitPidNoEintr(childpid, &status)) {
LogErrno("waitpid for %s", label);
return false;
}
return LogWaitStatus(label, status);
}
bool RunShellCommandFromEnv(const char *env_name, bool background) {
const char *command = GetStringSetting(env_name, "");
return RunShellCommandValue(env_name, command, background);
}