1 |
commit: 488d8989c518d9256f183899aac02024c679b93e |
2 |
Author: William Hubbs <w.d.hubbs <AT> gmail <DOT> com> |
3 |
AuthorDate: Thu Feb 15 21:10:24 2018 +0000 |
4 |
Commit: William Hubbs <williamh <AT> gentoo <DOT> org> |
5 |
CommitDate: Thu Feb 15 21:26:40 2018 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=488d8989 |
7 |
|
8 |
openrc-run: clean up string handling |
9 |
|
10 |
- remove references to PATH_MAX |
11 |
- use xasprintf to create strings |
12 |
|
13 |
src/rc/openrc-run.c | 61 ++++++++++++++++++++++++++++++++--------------------- |
14 |
1 file changed, 37 insertions(+), 24 deletions(-) |
15 |
|
16 |
diff --git a/src/rc/openrc-run.c b/src/rc/openrc-run.c |
17 |
index bb79f42f..7e8d2e50 100644 |
18 |
--- a/src/rc/openrc-run.c |
19 |
+++ b/src/rc/openrc-run.c |
20 |
@@ -109,7 +109,7 @@ static void |
21 |
handle_signal(int sig) |
22 |
{ |
23 |
int serrno = errno; |
24 |
- char signame[10] = { '\0' }; |
25 |
+ char *signame = NULL; |
26 |
struct winsize ws; |
27 |
|
28 |
switch (sig) { |
29 |
@@ -134,20 +134,22 @@ handle_signal(int sig) |
30 |
break; |
31 |
|
32 |
case SIGINT: |
33 |
- if (!signame[0]) |
34 |
- snprintf(signame, sizeof(signame), "SIGINT"); |
35 |
+ if (!signame) |
36 |
+ xasprintf(&signame, "SIGINT"); |
37 |
/* FALLTHROUGH */ |
38 |
case SIGTERM: |
39 |
- if (!signame[0]) |
40 |
- snprintf(signame, sizeof(signame), "SIGTERM"); |
41 |
+ if (!signame) |
42 |
+ xasprintf(&signame, "SIGTERM"); |
43 |
/* FALLTHROUGH */ |
44 |
case SIGQUIT: |
45 |
- if (!signame[0]) |
46 |
- snprintf(signame, sizeof(signame), "SIGQUIT"); |
47 |
+ if (!signame) |
48 |
+ xasprintf(&signame, "SIGQUIT"); |
49 |
/* Send the signal to our children too */ |
50 |
if (service_pid > 0) |
51 |
kill(service_pid, sig); |
52 |
- eerrorx("%s: caught %s, aborting", applet, signame); |
53 |
+ eerror("%s: caught %s, aborting", applet, signame); |
54 |
+ free(signame); |
55 |
+ exit(EXIT_FAILURE); |
56 |
/* NOTREACHED */ |
57 |
|
58 |
default: |
59 |
@@ -161,11 +163,12 @@ handle_signal(int sig) |
60 |
static void |
61 |
unhotplug() |
62 |
{ |
63 |
- char file[PATH_MAX]; |
64 |
+ char *file = NULL; |
65 |
|
66 |
- snprintf(file, sizeof(file), RC_SVCDIR "/hotplugged/%s", applet); |
67 |
+ xasprintf(&file, RC_SVCDIR "/hotplugged/%s", applet); |
68 |
if (exists(file) && unlink(file) != 0) |
69 |
eerror("%s: unlink `%s': %s", applet, file, strerror(errno)); |
70 |
+ free(file); |
71 |
} |
72 |
|
73 |
static void |
74 |
@@ -485,7 +488,7 @@ svc_exec(const char *arg1, const char *arg2) |
75 |
static bool |
76 |
svc_wait(const char *svc) |
77 |
{ |
78 |
- char file[PATH_MAX]; |
79 |
+ char *file = NULL; |
80 |
int fd; |
81 |
bool forever = false; |
82 |
RC_STRINGLIST *keywords; |
83 |
@@ -498,8 +501,7 @@ svc_wait(const char *svc) |
84 |
forever = true; |
85 |
rc_stringlist_free(keywords); |
86 |
|
87 |
- snprintf(file, sizeof(file), RC_SVCDIR "/exclusive/%s", |
88 |
- basename_c(svc)); |
89 |
+ xasprintf(&file, RC_SVCDIR "/exclusive/%s", basename_c(svc)); |
90 |
|
91 |
interval.tv_sec = 0; |
92 |
interval.tv_nsec = WAIT_INTERVAL; |
93 |
@@ -512,23 +514,29 @@ svc_wait(const char *svc) |
94 |
if (fd != -1) { |
95 |
if (flock(fd, LOCK_SH | LOCK_NB) == 0) { |
96 |
close(fd); |
97 |
+ free(file); |
98 |
return true; |
99 |
} |
100 |
close(fd); |
101 |
} |
102 |
- if (errno == ENOENT) |
103 |
+ if (errno == ENOENT) { |
104 |
+ free(file); |
105 |
return true; |
106 |
- if (errno != EWOULDBLOCK) |
107 |
- eerrorx("%s: open `%s': %s", applet, file, |
108 |
+ } |
109 |
+ if (errno != EWOULDBLOCK) { |
110 |
+ eerror("%s: open `%s': %s", applet, file, |
111 |
strerror(errno)); |
112 |
+ free(file); |
113 |
+ exit(EXIT_FAILURE); |
114 |
+ } |
115 |
if (nanosleep(&interval, NULL) == -1) { |
116 |
if (errno != EINTR) |
117 |
- return false; |
118 |
+ goto finish; |
119 |
} |
120 |
if (!forever) { |
121 |
timespecsub(&timeout, &interval, &timeout); |
122 |
if (timeout.tv_sec <= 0) |
123 |
- return false; |
124 |
+ goto finish; |
125 |
timespecsub(&warn, &interval, &warn); |
126 |
if (warn.tv_sec <= 0) { |
127 |
ewarn("%s: waiting for %s (%d seconds)", |
128 |
@@ -538,6 +546,8 @@ svc_wait(const char *svc) |
129 |
} |
130 |
} |
131 |
} |
132 |
+finish: |
133 |
+ free(file); |
134 |
return false; |
135 |
} |
136 |
|
137 |
@@ -1105,9 +1115,10 @@ int main(int argc, char **argv) |
138 |
bool runscript = false; |
139 |
int retval, opt, depoptions = RC_DEP_TRACE; |
140 |
RC_STRING *svc; |
141 |
- char path[PATH_MAX], lnk[PATH_MAX]; |
142 |
+ char *path = NULL; |
143 |
+ char *lnk = NULL; |
144 |
char *dir, *save = NULL, *saveLnk = NULL; |
145 |
- char pidstr[10]; |
146 |
+ char *pidstr = NULL; |
147 |
size_t l = 0, ll; |
148 |
const char *file; |
149 |
struct stat stbuf; |
150 |
@@ -1134,10 +1145,12 @@ int main(int argc, char **argv) |
151 |
* This works fine, provided that we ONLY allow multiplexed services |
152 |
* to exist in the same directory as the master link. |
153 |
* Also, the master link as to be a real file in the init dir. */ |
154 |
- if (!realpath(argv[1], path)) { |
155 |
+ path = realpath(argv[1], NULL); |
156 |
+ if (!path) { |
157 |
fprintf(stderr, "realpath: %s\n", strerror(errno)); |
158 |
exit(EXIT_FAILURE); |
159 |
} |
160 |
+ lnk = xmalloc(4096); |
161 |
memset(lnk, 0, sizeof(lnk)); |
162 |
if (readlink(argv[1], lnk, sizeof(lnk)-1)) { |
163 |
dir = dirname(path); |
164 |
@@ -1153,8 +1166,7 @@ int main(int argc, char **argv) |
165 |
} else |
166 |
file = basename_c(argv[1]); |
167 |
ll = strlen(dir) + strlen(file) + 2; |
168 |
- service = xmalloc(ll); |
169 |
- snprintf(service, ll, "%s/%s", dir, file); |
170 |
+ xasprintf(&service, "%s/%s", dir, file); |
171 |
if (stat(service, &stbuf) != 0) { |
172 |
free(service); |
173 |
service = xstrdup(lnk); |
174 |
@@ -1162,6 +1174,7 @@ int main(int argc, char **argv) |
175 |
free(save); |
176 |
free(saveLnk); |
177 |
} |
178 |
+ free(lnk); |
179 |
if (!service) |
180 |
service = xstrdup(path); |
181 |
applet = basename_c(service); |
182 |
@@ -1185,7 +1198,7 @@ int main(int argc, char **argv) |
183 |
/* Set an env var so that we always know our pid regardless of any |
184 |
subshells the init script may create so that our mark_service_* |
185 |
functions can always instruct us of this change */ |
186 |
- snprintf(pidstr, sizeof(pidstr), "%d", (int) getpid()); |
187 |
+ xasprintf(&pidstr, "%d", (int) getpid()); |
188 |
setenv("RC_OPENRC_PID", pidstr, 1); |
189 |
/* |
190 |
* RC_RUNSCRIPT_PID is deprecated, but we will keep it for a while |