-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
211 lines (177 loc) · 4 KB
/
util.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright 2014 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "util.h"
static int daemon_pipe[2] = { -1, -1 };
static int openfd(char *path, int flags, int reqfd)
{
int fd = open(path, flags);
if (fd < 0)
return -1;
if (fd == reqfd)
return reqfd;
if (dup2(fd, reqfd) >= 0) {
close(fd);
return reqfd;
}
close(fd);
return -1;
}
static int init_daemon_stdio(void)
{
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
if (openfd("/dev/null", O_RDONLY, STDIN_FILENO) < 0)
return -1;
if (openfd("/dev/kmsg", O_WRONLY, STDOUT_FILENO) < 0)
return -1;
if (openfd("/dev/kmsg", O_WRONLY, STDERR_FILENO) < 0)
return -1;
return 0;
}
void daemonize(bool wait_child)
{
pid_t pid;
if (wait_child)
if (pipe(daemon_pipe) < 0)
exit(EXIT_FAILURE);
pid = fork();
if (pid == -1)
return;
else if (pid != 0) {
if (wait_child) {
char code = EXIT_FAILURE;
close(daemon_pipe[1]);
if (read(daemon_pipe[0], &code, sizeof(code)) < 0) {
int c;
/* Child has died? */
if (errno == EPIPE)
if (waitpid(pid, &c, 0) >= 0)
exit(c); /* Propagate child exit code. */
/* Just report failure. */
exit(EXIT_FAILURE);
}
exit(code);
}
exit(EXIT_SUCCESS);
}
if (wait_child)
close(daemon_pipe[0]);
if (setsid() == -1)
return;
init_daemon_stdio();
}
void daemon_exit_code(char code)
{
if (write(daemon_pipe[1], &code, sizeof(code)) != sizeof(code)) {
LOG(ERROR, "failed to report exit code back to daemon parent");
}
close(daemon_pipe[1]);
}
static int is_valid_fd(int fd)
{
return fcntl(fd, F_GETFL) != -1 || errno != EBADF;
}
void fix_stdio(void)
{
if (!is_valid_fd(STDIN_FILENO)
|| !is_valid_fd(STDOUT_FILENO)
|| !is_valid_fd(STDERR_FILENO))
init_daemon_stdio();
}
void parse_location(char* loc_str, int* x, int* y)
{
int count = 0;
char* savedptr;
char* str;
int* results[] = {x, y};
long tmp;
for (char* token = str = loc_str; token != NULL; str = NULL) {
if (count > 1)
break;
token = strtok_r(str, ",", &savedptr);
if (token) {
tmp = MIN(INT_MAX, strtol(token, NULL, 0));
*(results[count++]) = (int)tmp;
}
}
}
void parse_filespec(char* filespec, char* filename,
int32_t* offset_x, int32_t* offset_y, uint32_t* duration,
uint32_t default_duration,
int32_t default_x, int32_t default_y)
{
char* saved_ptr;
char* token;
// defaults
*offset_x = default_x;
*offset_y = default_y;
*duration = default_duration;
token = filespec;
token = strtok_r(token, ":", &saved_ptr);
if (token)
strcpy(filename, token);
token = strtok_r(NULL, ":", &saved_ptr);
if (token) {
*duration = strtoul(token, NULL, 0);
token = strtok_r(NULL, ",", &saved_ptr);
if (token) {
token = strtok_r(token, ",", &saved_ptr);
if (token) {
*offset_x = strtol(token, NULL, 0);
token = strtok_r(token, ",", &saved_ptr);
if (token)
*offset_y = strtol(token, NULL, 0);
}
}
}
}
void parse_image_option(char* optionstr, char** name, char** val)
{
char** result[2] = { name, val };
int count = 0;
char* str;
char* savedptr;
for (char* token = str = optionstr; token != NULL; str = NULL) {
if (count > 1)
break;
token = strtok_r(str, ":", &savedptr);
if (token) {
*(result[count]) = malloc(strlen(token) + 1);
strcpy(*(result[count]), token);
count++;
}
}
}
bool write_string_to_file(const char *path, const char *s)
{
int fd;
size_t towrite;
ssize_t written;
fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP);
if (!fd)
return false;
towrite = strlen(s);
written = write(fd, s, towrite);
close(fd);
if (written != (ssize_t)towrite) {
LOG(ERROR, "Failed to write string%s to %s", s, path);
unlink(path);
return false;
}
return true;
}