MCPcopy Create free account
hub / github.com/F-Stack/f-stack / common_test_pidfile_child

Function common_test_pidfile_child

tools/libutil/tests/pidfile_test.c:127–237  ·  view source on GitHub ↗

* Common code for test_pidfile_{contested,inherited}. */

Source from the content-addressed store, hash-verified

125 * Common code for test_pidfile_{contested,inherited}.
126 */
127static const char *
128common_test_pidfile_child(const char *fn, int parent_open)
129{
130 struct pidfh *pf = NULL;
131 pid_t other = 0, pid = 0;
132 int fd[2], serrno, status;
133 struct kevent event, ke;
134 char ch;
135 int kq;
136
137 unlink(fn);
138 if (pipe(fd) != 0)
139 return (strerror(errno));
140
141 if (parent_open) {
142 pf = pidfile_open(fn, 0600, &other);
143 if (pf == NULL && other != 0)
144 return ("pidfile exists and is locked");
145 if (pf == NULL)
146 return (strerror(errno));
147 }
148
149 pid = fork();
150 if (pid == -1)
151 return (strerror(errno));
152 if (pid == 0) {
153 // child
154 close(fd[0]);
155 signal(SIGINT, signal_handler);
156 if (!parent_open) {
157 pf = pidfile_open(fn, 0600, &other);
158 if (pf == NULL && other != 0)
159 return ("pidfile exists and is locked");
160 if (pf == NULL)
161 return (strerror(errno));
162 }
163 if (pidfile_write(pf) != 0) {
164 serrno = errno;
165 pidfile_close(pf);
166 unlink(fn);
167 return (strerror(serrno));
168 }
169 if (pf == NULL)
170 _exit(1);
171 if (pidfile_write(pf) != 0)
172 _exit(2);
173 kq = kqueue();
174 if (kq == -1)
175 _exit(3);
176 EV_SET(&ke, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
177 /* Attach event to the kqueue. */
178 if (kevent(kq, &ke, 1, NULL, 0, NULL) != 0)
179 _exit(4);
180 /* Inform the parent we are ready to receive SIGINT */
181 if (write(fd[1], "*", 1) != 1)
182 _exit(5);
183 /* Wait for SIGINT received */
184 if (kevent(kq, NULL, 0, &event, 1, NULL) != 1)

Callers 2

test_pidfile_contestedFunction · 0.85
test_pidfile_inheritedFunction · 0.85

Calls 10

pidfile_openFunction · 0.85
pidfile_writeFunction · 0.85
pidfile_closeFunction · 0.85
killFunction · 0.85
keventClass · 0.70
pipeClass · 0.50
closeFunction · 0.50
kqueueClass · 0.50
writeFunction · 0.50
readFunction · 0.50

Tested by

no test coverage detected