unistr/u{8,16,32}-uctomb: Avoid possible trouble with huge strings.
[gnulib.git] / tests / test-posix_spawn4.c
blob91a5497ede2678480f2ddc7e6320fbe5120f49d0
1 /* Test of posix_spawn() function with 'chdir' action.
2 Copyright (C) 2008-2020 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2018. */
19 #include <config.h>
21 #include <spawn.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
34 #include "findprog.h"
36 static int
37 fd_safer (int fd)
39 if (0 <= fd && fd <= 2)
41 int f = fd_safer (dup (fd));
42 int e = errno;
43 close (fd);
44 errno = e;
45 fd = f;
48 return fd;
51 static void
52 test (const char *pwd_prog)
54 char *argv[2] = { (char *) "pwd", NULL };
55 int ifd[2];
56 sigset_t blocked_signals;
57 sigset_t fatal_signal_set;
58 posix_spawn_file_actions_t actions;
59 bool actions_allocated;
60 posix_spawnattr_t attrs;
61 bool attrs_allocated;
62 int err;
63 pid_t child;
64 int fd;
65 FILE *fp;
66 char line[80];
67 int status;
68 int exitstatus;
70 if (pipe (ifd) < 0 || (ifd[0] = fd_safer (ifd[0])) < 0)
72 perror ("cannot create pipe");
73 exit (1);
75 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
76 sigemptyset (&fatal_signal_set);
77 sigaddset (&fatal_signal_set, SIGINT);
78 sigaddset (&fatal_signal_set, SIGTERM);
79 sigaddset (&fatal_signal_set, SIGHUP);
80 sigaddset (&fatal_signal_set, SIGPIPE);
81 sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
82 actions_allocated = false;
83 attrs_allocated = false;
84 if ((err = posix_spawn_file_actions_init (&actions)) != 0
85 || (actions_allocated = true,
86 (err = posix_spawn_file_actions_adddup2 (&actions, ifd[1], STDOUT_FILENO)) != 0
87 || (err = posix_spawn_file_actions_addclose (&actions, ifd[1])) != 0
88 || (err = posix_spawn_file_actions_addclose (&actions, ifd[0])) != 0
89 || (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0)) != 0
90 || (err = posix_spawn_file_actions_addchdir (&actions, "/")) != 0
91 || (err = posix_spawnattr_init (&attrs)) != 0
92 || (attrs_allocated = true,
93 (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
94 || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
95 || (err = posix_spawnp (&child, pwd_prog, &actions, &attrs, argv, environ)) != 0))
97 if (actions_allocated)
98 posix_spawn_file_actions_destroy (&actions);
99 if (attrs_allocated)
100 posix_spawnattr_destroy (&attrs);
101 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
102 errno = err;
103 perror ("subprocess failed");
104 exit (1);
106 posix_spawn_file_actions_destroy (&actions);
107 posix_spawnattr_destroy (&attrs);
108 sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
109 close (ifd[1]);
110 fd = ifd[0];
111 fp = fdopen (fd, "r");
112 if (fp == NULL)
114 fprintf (stderr, "fdopen() failed\n");
115 exit (1);
117 if (fread (line, 1, 80, fp) < 2)
119 fprintf (stderr, "could not read expected output\n");
120 exit (1);
122 if (memcmp (line, "/\n", 2) != 0)
124 fprintf (stderr, "read output is not the expected output");
125 exit (1);
127 fclose (fp);
128 status = 0;
129 while (waitpid (child, &status, 0) != child)
131 if (!WIFEXITED (status))
133 fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
134 exit (1);
136 exitstatus = WEXITSTATUS (status);
137 if (exitstatus != 0)
139 fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
140 exit (1);
145 main ()
147 test ("pwd");
149 /* Verify that if a program is given as a relative file name with at least one
150 slash, it is interpreted w.r.t. the current directory after chdir has been
151 executed. */
153 const char *abs_pwd_prog = find_in_path ("pwd");
155 if (abs_pwd_prog != NULL
156 && abs_pwd_prog[0] == '/'
157 && abs_pwd_prog[1] != '0' && abs_pwd_prog[1] != '/')
158 test (&abs_pwd_prog[1]);
161 return 0;