* Lock the group file */
| 95 | * Lock the group file |
| 96 | */ |
| 97 | int |
| 98 | gr_lock(void) |
| 99 | { |
| 100 | if (*group_file == '\0') |
| 101 | return (-1); |
| 102 | |
| 103 | for (;;) { |
| 104 | struct stat st; |
| 105 | |
| 106 | lockfd = flopen(group_file, O_RDONLY|O_NONBLOCK|O_CLOEXEC, 0); |
| 107 | if (lockfd == -1) { |
| 108 | if (errno == EWOULDBLOCK) { |
| 109 | errx(1, "the group file is busy"); |
| 110 | } else { |
| 111 | err(1, "could not lock the group file"); |
| 112 | } |
| 113 | } |
| 114 | if (fstat(lockfd, &st) == -1) |
| 115 | err(1, "fstat() failed"); |
| 116 | if (st.st_nlink != 0) |
| 117 | break; |
| 118 | close(lockfd); |
| 119 | lockfd = -1; |
| 120 | } |
| 121 | return (lockfd); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Create and open a presmuably safe temp file for editing group data |