Refactoring: split out exec_succeeded function.
[oweals/dinit.git] / src / dinit-util.h
1 #ifndef DINIT_UTIL_H_INCLUDED
2 #define DINIT_UTIL_H_INCLUDED 1
3
4 #include <cstddef>
5 #include <cerrno>
6
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 // Signal-safe read. Read and re-try if interrupted by signal (EINTR).
11 // *May* affect errno even on a successful read (when the return is less than n).
12 inline ssize_t ss_read(int fd, void * buf, size_t n)
13 {
14     char * cbuf = static_cast<char *>(buf);
15     ssize_t r = 0;
16     while ((size_t)r < n) {
17         ssize_t res = read(fd, cbuf + r, n - r);
18         if (res == 0) {
19             return r;
20         }
21         if (res < 0) {
22             if (res == EINTR) {
23                 continue;
24             }
25
26             // If any other error, and we have successfully read some, return it:
27             if (r == 0) {
28                 return -1;
29             }
30             else {
31                 return r;
32             }
33         }
34         r += res;
35     }
36     return n;
37 }
38
39 #endif