Line | Count | Source |
1 | | #include "git-compat-util.h" |
2 | | |
3 | | #ifdef OPEN_RETURNS_EINTR |
4 | | #undef open |
5 | | int git_open_with_retry(const char *path, int flags, ...) |
6 | | { |
7 | | mode_t mode = 0; |
8 | | int ret; |
9 | | |
10 | | /* |
11 | | * Also O_TMPFILE would take a mode, but it isn't defined everywhere. |
12 | | * And anyway, we don't use it in our code base. |
13 | | */ |
14 | | if (flags & O_CREAT) { |
15 | | va_list ap; |
16 | | va_start(ap, flags); |
17 | | mode = va_arg(ap, int); |
18 | | va_end(ap); |
19 | | } |
20 | | |
21 | | do { |
22 | | ret = open(path, flags, mode); |
23 | | } while (ret < 0 && errno == EINTR); |
24 | | |
25 | | return ret; |
26 | | } |
27 | | #endif |
28 | | |
29 | | int git_open_cloexec(const char *name, int flags) |
30 | 0 | { |
31 | 0 | int fd; |
32 | 0 | static int o_cloexec = O_CLOEXEC; |
33 | |
|
34 | 0 | fd = open(name, flags | o_cloexec); |
35 | 0 | if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) { |
36 | | /* Try again w/o O_CLOEXEC: the kernel might not support it */ |
37 | 0 | o_cloexec &= ~O_CLOEXEC; |
38 | 0 | fd = open(name, flags | o_cloexec); |
39 | 0 | } |
40 | |
|
41 | 0 | #if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC) |
42 | 0 | { |
43 | 0 | static int fd_cloexec = FD_CLOEXEC; |
44 | |
|
45 | 0 | if (!o_cloexec && 0 <= fd && fd_cloexec) { |
46 | | /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */ |
47 | 0 | int flags = fcntl(fd, F_GETFD); |
48 | 0 | if (fcntl(fd, F_SETFD, flags | fd_cloexec)) |
49 | 0 | fd_cloexec = 0; |
50 | 0 | } |
51 | 0 | } |
52 | 0 | #endif |
53 | 0 | return fd; |
54 | 0 | } |