Skip to content

Commit 5c83677

Browse files
rickyesJeffroMF
authored andcommitted
unix,process: add uv__write_errno helper function
No functional changes, but slightly more compact code. PR-URL: libuv#3059 Reviewed-By: Jameson Nash <[email protected]>
1 parent 59329ec commit 5c83677

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

src/unix/process.c

+23-32
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ static void uv__write_int(int fd, int val) {
201201
}
202202

203203

204+
static void uv__write_errno(int error_fd) {
205+
uv__write_int(error_fd, UV__ERR(errno));
206+
_exit(127);
207+
}
208+
209+
204210
#if !(defined(__APPLE__) && (TARGET_OS_TV || TARGET_OS_WATCH))
205211
/* execvp is marked __WATCHOS_PROHIBITED __TVOS_PROHIBITED, so must be
206212
* avoided. Since this isn't called on those targets, the function
@@ -229,10 +235,8 @@ static void uv__process_child_init(const uv_process_options_t* options,
229235
if (use_fd < 0 || use_fd >= fd)
230236
continue;
231237
pipes[fd][1] = fcntl(use_fd, F_DUPFD, stdio_count);
232-
if (pipes[fd][1] == -1) {
233-
uv__write_int(error_fd, UV__ERR(errno));
234-
_exit(127);
235-
}
238+
if (pipes[fd][1] == -1)
239+
uv__write_errno(error_fd);
236240
}
237241

238242
for (fd = 0; fd < stdio_count; fd++) {
@@ -249,10 +253,8 @@ static void uv__process_child_init(const uv_process_options_t* options,
249253
use_fd = open("/dev/null", fd == 0 ? O_RDONLY : O_RDWR);
250254
close_fd = use_fd;
251255

252-
if (use_fd < 0) {
253-
uv__write_int(error_fd, UV__ERR(errno));
254-
_exit(127);
255-
}
256+
if (use_fd < 0)
257+
uv__write_errno(error_fd);
256258
}
257259
}
258260

@@ -261,10 +263,8 @@ static void uv__process_child_init(const uv_process_options_t* options,
261263
else
262264
fd = dup2(use_fd, fd);
263265

264-
if (fd == -1) {
265-
uv__write_int(error_fd, UV__ERR(errno));
266-
_exit(127);
267-
}
266+
if (fd == -1)
267+
uv__write_errno(error_fd);
268268

269269
if (fd <= 2)
270270
uv__nonblock_fcntl(fd, 0);
@@ -280,10 +280,8 @@ static void uv__process_child_init(const uv_process_options_t* options,
280280
uv__close(use_fd);
281281
}
282282

283-
if (options->cwd != NULL && chdir(options->cwd)) {
284-
uv__write_int(error_fd, UV__ERR(errno));
285-
_exit(127);
286-
}
283+
if (options->cwd != NULL && chdir(options->cwd))
284+
uv__write_errno(error_fd);
287285

288286
if (options->flags & (UV_PROCESS_SETUID | UV_PROCESS_SETGID)) {
289287
/* When dropping privileges from root, the `setgroups` call will
@@ -296,15 +294,11 @@ static void uv__process_child_init(const uv_process_options_t* options,
296294
SAVE_ERRNO(setgroups(0, NULL));
297295
}
298296

299-
if ((options->flags & UV_PROCESS_SETGID) && setgid(options->gid)) {
300-
uv__write_int(error_fd, UV__ERR(errno));
301-
_exit(127);
302-
}
297+
if ((options->flags & UV_PROCESS_SETGID) && setgid(options->gid))
298+
uv__write_errno(error_fd);
303299

304-
if ((options->flags & UV_PROCESS_SETUID) && setuid(options->uid)) {
305-
uv__write_int(error_fd, UV__ERR(errno));
306-
_exit(127);
307-
}
300+
if ((options->flags & UV_PROCESS_SETUID) && setuid(options->uid))
301+
uv__write_errno(error_fd);
308302

309303
if (options->env != NULL) {
310304
environ = options->env;
@@ -327,26 +321,23 @@ static void uv__process_child_init(const uv_process_options_t* options,
327321
if (SIG_ERR != signal(n, SIG_DFL))
328322
continue;
329323

330-
uv__write_int(error_fd, UV__ERR(errno));
331-
_exit(127);
324+
uv__write_errno(error_fd);
332325
}
333326

334327
/* Reset signal mask. */
335328
sigemptyset(&set);
336329
err = pthread_sigmask(SIG_SETMASK, &set, NULL);
337330

338-
if (err != 0) {
339-
uv__write_int(error_fd, UV__ERR(err));
340-
_exit(127);
341-
}
331+
if (err != 0)
332+
uv__write_errno(error_fd);
342333

343334
#ifdef __MVS__
344335
execvpe(options->file, options->args, environ);
345336
#else
346337
execvp(options->file, options->args);
347338
#endif
348-
uv__write_int(error_fd, UV__ERR(errno));
349-
_exit(127);
339+
340+
uv__write_errno(error_fd);
350341
}
351342
#endif
352343

0 commit comments

Comments
 (0)