landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4 :*/ |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 2 | /* functions.c - reusable stuff. |
| 3 | * |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 4 | * Functions with the x prefix are wrappers for library functions. They either |
| 5 | * succeed or kill the program with an error message, but never return failure. |
| 6 | * They usually have the same arguments and return value as the function they |
| 7 | * wrap. |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 8 | * |
| 9 | * Copyright 2006 Rob Landley <rob@landley.net> |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include "toys.h" |
| 13 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 14 | void verror_msg(char *msg, int err, va_list va) |
| 15 | { |
| 16 | fprintf(stderr, "%s: ", toys.which->name); |
| 17 | vfprintf(stderr, msg, va); |
| 18 | if (err) fprintf(stderr, ": %s", strerror(err)); |
| 19 | putc('\n', stderr); |
| 20 | } |
| 21 | |
| 22 | void error_msg(char *msg, ...) |
| 23 | { |
| 24 | va_list va; |
| 25 | |
| 26 | va_start(va, msg); |
| 27 | verror_msg(msg, 0, va); |
| 28 | va_end(va); |
| 29 | } |
| 30 | |
| 31 | void perror_msg(char *msg, ...) |
| 32 | { |
| 33 | va_list va; |
| 34 | |
| 35 | va_start(va, msg); |
| 36 | verror_msg(msg, errno, va); |
| 37 | va_end(va); |
| 38 | } |
| 39 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 40 | // Die with an error message. |
| 41 | void error_exit(char *msg, ...) |
| 42 | { |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 43 | va_list va; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 44 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 45 | va_start(va, msg); |
| 46 | verror_msg(msg, 0, va); |
| 47 | va_end(va); |
| 48 | |
| 49 | exit(toys.exitval); |
| 50 | } |
| 51 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame^] | 52 | |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 53 | // Die with an error message and strerror(errno) |
| 54 | void perror_exit(char *msg, ...) |
| 55 | { |
| 56 | va_list va; |
| 57 | |
| 58 | va_start(va, msg); |
| 59 | verror_msg(msg, errno, va); |
| 60 | va_end(va); |
| 61 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 62 | exit(toys.exitval); |
| 63 | } |
| 64 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame^] | 65 | // Stub until the online help system goes in. |
| 66 | void usage_exit(void) |
| 67 | { |
| 68 | exit(1); |
| 69 | } |
| 70 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 71 | // Like strncpy but always null terminated. |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 72 | void strlcpy(char *dest, char *src, size_t size) |
| 73 | { |
| 74 | strncpy(dest,src,size); |
| 75 | dest[size-1] = 0; |
| 76 | } |
| 77 | |
| 78 | // Die unless we can allocate memory. |
| 79 | void *xmalloc(size_t size) |
| 80 | { |
| 81 | void *ret = malloc(size); |
| 82 | if (!ret) error_exit("xmalloc"); |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 83 | |
| 84 | return ret; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 85 | } |
| 86 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 87 | // Die unless we can allocate prezeroed memory. |
| 88 | void *xzalloc(size_t size) |
| 89 | { |
| 90 | void *ret = xmalloc(size); |
| 91 | bzero(ret,size); |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | // Die unless we can change the size of an existing allocation, possibly |
| 96 | // moving it. (Notice different arguments from libc function.) |
| 97 | void xrealloc(void **ptr, size_t size) |
| 98 | { |
| 99 | *ptr = realloc(*ptr, size); |
| 100 | if (!*ptr) error_exit("xrealloc"); |
| 101 | } |
| 102 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 103 | // Die unless we can allocate a copy of this many bytes of string. |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 104 | void *xstrndup(char *s, size_t n) |
| 105 | { |
| 106 | void *ret = xmalloc(++n); |
| 107 | strlcpy(ret, s, n); |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 112 | // Die unless we can allocate a copy of this string. |
| 113 | void *xstrdup(char *s) |
| 114 | { |
| 115 | return xstrndup(s,strlen(s)); |
| 116 | } |
| 117 | |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 118 | // Die unless we can allocate enough space to sprintf() into. |
| 119 | char *xmsprintf(char *format, ...) |
| 120 | { |
| 121 | va_list va; |
| 122 | int len; |
| 123 | char *ret; |
| 124 | |
| 125 | // How long is it? |
| 126 | |
| 127 | va_start(va, format); |
| 128 | len = vsnprintf(0, 0, format, va); |
| 129 | len++; |
| 130 | va_end(va); |
| 131 | |
| 132 | // Allocate and do the sprintf() |
| 133 | ret = xmalloc(len); |
| 134 | va_start(va, format); |
| 135 | vsnprintf(ret, len, format, va); |
| 136 | va_end(va); |
| 137 | |
| 138 | return ret; |
| 139 | } |
| 140 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 141 | // Die unless we can exec argv[] (or run builtin command). Note that anything |
| 142 | // with a path isn't a builtin, so /bin/sh won't match the builtin sh. |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 143 | void xexec(char **argv) |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 144 | { |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 145 | toy_exec(argv); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 146 | execvp(argv[0], argv); |
| 147 | error_exit("No %s", argv[0]); |
| 148 | } |
| 149 | |
Rob Landley | d3e9d64 | 2007-01-08 03:25:47 -0500 | [diff] [blame] | 150 | void xaccess(char *path, int flags) |
| 151 | { |
| 152 | if (access(path, flags)) error_exit("Can't access '%s'\n", path); |
| 153 | } |
| 154 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 155 | // Die unless we can open/create a file, returning file descriptor. |
Rob Landley | 1322beb | 2007-01-07 22:51:12 -0500 | [diff] [blame] | 156 | int xcreate(char *path, int flags, int mode) |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 157 | { |
| 158 | int fd = open(path, flags, mode); |
| 159 | if (fd == -1) error_exit("No file %s\n", path); |
| 160 | return fd; |
| 161 | } |
| 162 | |
Rob Landley | 1322beb | 2007-01-07 22:51:12 -0500 | [diff] [blame] | 163 | // Die unless we can open a file, returning file descriptor. |
| 164 | int xopen(char *path, int flags) |
| 165 | { |
| 166 | return xcreate(path, flags, 0); |
| 167 | } |
| 168 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 169 | // Die unless we can open/create a file, returning FILE *. |
| 170 | FILE *xfopen(char *path, char *mode) |
| 171 | { |
| 172 | FILE *f = fopen(path, mode); |
| 173 | if (!f) error_exit("No file %s\n", path); |
| 174 | return f; |
| 175 | } |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 176 | |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 177 | // Read from file handle, retrying if interrupted. |
| 178 | ssize_t reread(int fd, void *buf, size_t count) |
| 179 | { |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 180 | for (;;) { |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 181 | ssize_t len = read(fd, buf, count); |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 182 | if (len >= 0 || errno != EINTR) return len; |
| 183 | } |
| 184 | } |
| 185 | |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 186 | // Write to file handle, retrying if interrupted. |
| 187 | ssize_t rewrite(int fd, void *buf, size_t count) |
| 188 | { |
| 189 | for (;;) { |
| 190 | ssize_t len = write(fd, buf, count); |
| 191 | if (len >= 0 || errno != EINTR) return len; |
| 192 | } |
| 193 | } |
| 194 | |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 195 | // Keep reading until full or EOF |
| 196 | ssize_t readall(int fd, void *buf, size_t count) |
| 197 | { |
| 198 | size_t len = 0; |
| 199 | while (len<count) { |
| 200 | int i = reread(fd, buf, count); |
| 201 | if (!i) return len; |
| 202 | if (i<0) return i; |
| 203 | count += i; |
| 204 | } |
| 205 | |
| 206 | return count; |
| 207 | } |
| 208 | |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 209 | // Keep writing until done or EOF |
| 210 | ssize_t writeall(int fd, void *buf, size_t count) |
| 211 | { |
| 212 | size_t len = 0; |
| 213 | while (len<count) { |
| 214 | int i = rewrite(fd, buf, count); |
| 215 | if (!i) return len; |
| 216 | if (i<0) return i; |
| 217 | count += i; |
| 218 | } |
| 219 | |
| 220 | return count; |
| 221 | } |
| 222 | |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame^] | 223 | // Die if there's an error other than EOF. |
| 224 | size_t xread(int fd, void *buf, size_t count) |
landley | 64b2e23 | 2006-10-30 10:01:19 -0500 | [diff] [blame] | 225 | { |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame^] | 226 | count = reread(fd, buf, count); |
| 227 | if (count < 0) perror_exit("xread"); |
| 228 | |
| 229 | return count; |
| 230 | } |
| 231 | |
| 232 | void xreadall(int fd, void *buf, size_t count) |
| 233 | { |
| 234 | if (count != readall(fd, buf, count)) perror_exit("xreadall"); |
| 235 | } |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 236 | |
Rob Landley | 3388f4c | 2007-01-08 04:26:01 -0500 | [diff] [blame] | 237 | void xwrite(int fd, void *buf, size_t count) |
Rob Landley | f3e452a | 2007-01-08 02:49:39 -0500 | [diff] [blame] | 238 | { |
| 239 | if (count != writeall(fd, buf, count)) perror_exit("xwrite"); |
| 240 | } |
| 241 | |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 242 | char *xgetcwd(void) |
| 243 | { |
| 244 | char *buf = getcwd(NULL, 0); |
| 245 | if (!buf) error_exit("xgetcwd"); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 246 | |
| 247 | return buf; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 250 | // Cannonicalizes path by removing ".", "..", and "//" elements. This is not |
Rob Landley | c6f481c | 2006-12-30 22:01:47 -0500 | [diff] [blame] | 251 | // the same as realpath(), where "dir/.." could wind up somewhere else by |
| 252 | // following symlinks. |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 253 | char *xabspath(char *path) |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 254 | { |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 255 | char *from, *to; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 256 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 257 | // If this isn't an absolute path, make it one with cwd. |
| 258 | if (path[0]!='/') { |
| 259 | char *cwd=xgetcwd(); |
| 260 | path = xmsprintf("%s/%s",cwd,path); |
| 261 | free(cwd); |
| 262 | } else path = xstrdup(path); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 263 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 264 | // Loop through path elements |
| 265 | from = to = path; |
| 266 | while (*from) { |
| 267 | |
| 268 | // Continue any current path component. |
| 269 | if (*from!='/') { |
| 270 | *(to++) = *(from++); |
| 271 | continue; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 272 | } |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 273 | |
| 274 | // Skip duplicate slashes. |
| 275 | while (*from=='/') from++; |
| 276 | |
| 277 | // Start of a new filename. Handle . and .. |
| 278 | while (*from=='.') { |
| 279 | // Skip . |
| 280 | if (from[1]=='/') from += 2; |
| 281 | else if (!from[1]) from++; |
| 282 | // Back up for .. |
| 283 | else if (from[1]=='.') { |
| 284 | if (from[2]=='/') from +=3; |
| 285 | else if(!from[2]) from+=2; |
| 286 | else break; |
| 287 | while (to>path && *(--to)!='/'); |
| 288 | } else break; |
| 289 | } |
| 290 | // Add directory separator slash. |
| 291 | *(to++) = '/'; |
| 292 | } |
| 293 | *to = 0; |
| 294 | |
| 295 | return path; |
| 296 | } |
| 297 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 298 | // Find all file in a colon-separated path with access type "type" (generally |
| 299 | // X_OK or R_OK). Returns a list of absolute paths to each file found, in |
| 300 | // order. |
| 301 | |
| 302 | struct string_list *find_in_path(char *path, char *filename) |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 303 | { |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 304 | struct string_list *rlist = NULL; |
| 305 | char *cwd = xgetcwd(); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 306 | |
| 307 | for (;;) { |
| 308 | char *next = path ? index(path, ':') : NULL; |
| 309 | int len = next ? next-path : strlen(path); |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 310 | struct string_list *rnext; |
| 311 | struct stat st; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 312 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 313 | rnext = xmalloc(sizeof(void *) + strlen(filename) |
| 314 | + (len ? len : strlen(cwd)) + 2); |
| 315 | if (!len) sprintf(rnext->str, "%s/%s", cwd, filename); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 316 | else { |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 317 | char *res = rnext->str; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 318 | strncpy(res, path, len); |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 319 | res += len; |
| 320 | *(res++) = '/'; |
| 321 | strcpy(res, filename); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 322 | } |
| 323 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 324 | // Confirm it's not a directory. |
| 325 | if (!stat(rnext->str, &st) && S_ISREG(st.st_mode)) { |
| 326 | rnext->next = rlist; |
| 327 | rlist = rnext; |
| 328 | } else free(rnext); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 329 | |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 330 | if (!next) break; |
| 331 | path += len; |
| 332 | path++; |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 333 | } |
| 334 | free(cwd); |
| 335 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 336 | return rlist; |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 337 | |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 338 | } |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 339 | |
| 340 | // Convert unsigned int to ascii, writing into supplied buffer. A truncated |
| 341 | // result contains the first few digits of the result ala strncpy, and is |
| 342 | // always null terminated (unless buflen is 0). |
| 343 | void utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
| 344 | { |
| 345 | int i, out = 0; |
| 346 | |
| 347 | if (buflen) { |
| 348 | for (i=1000000000; i; i/=10) { |
| 349 | int res = n/i; |
| 350 | |
| 351 | if ((res || out || i == 1) && --buflen>0) { |
| 352 | out++; |
| 353 | n -= res*i; |
| 354 | *buf++ = '0' + res; |
| 355 | } |
| 356 | } |
| 357 | *buf = 0; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // Convert signed integer to ascii, using utoa_to_buf() |
| 362 | void itoa_to_buf(int n, char *buf, unsigned buflen) |
| 363 | { |
| 364 | if (buflen && n<0) { |
| 365 | n = -n; |
| 366 | *buf++ = '-'; |
| 367 | buflen--; |
| 368 | } |
| 369 | utoa_to_buf((unsigned)n, buf, buflen); |
| 370 | } |
| 371 | |
| 372 | // This static buffer is used by both utoa() and itoa(), calling either one a |
| 373 | // second time will overwrite the previous results. |
| 374 | // |
| 375 | // The longest 32 bit integer is -2 billion plus a null terminator: 12 bytes. |
| 376 | // Note that int is always 32 bits on any remotely unix-like system, see |
| 377 | // http://www.unix.org/whitepapers/64bit.html for details. |
| 378 | |
| 379 | static char itoa_buf[12]; |
| 380 | |
| 381 | // Convert unsigned integer to ascii, returning a static buffer. |
| 382 | char *utoa(unsigned n) |
| 383 | { |
| 384 | utoa_to_buf(n, itoa_buf, sizeof(itoa_buf)); |
| 385 | |
| 386 | return itoa_buf; |
| 387 | } |
| 388 | |
| 389 | char *itoa(int n) |
| 390 | { |
| 391 | itoa_to_buf(n, itoa_buf, sizeof(itoa_buf)); |
| 392 | |
| 393 | return itoa_buf; |
| 394 | } |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame^] | 395 | |
| 396 | /* |
| 397 | This might be of use or might not. Unknown yet... |
| 398 | |
| 399 | |
| 400 | // Return how long the file at fd is, if there's any way to determine it. |
| 401 | off_t fdlength(int fd) |
| 402 | { |
| 403 | off_t bottom = 0, top = 0, pos; |
| 404 | long size; |
| 405 | |
| 406 | // If the ioctl works for this, return it. |
| 407 | |
| 408 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512; |
| 409 | |
| 410 | // If not, do a binary search for the last location we can read. (Some |
| 411 | // block devices don't do BLKGETSIZE right.) This should probably have |
| 412 | // a CONFIG option... |
| 413 | |
| 414 | do { |
| 415 | char temp; |
| 416 | |
| 417 | pos = bottom + (top - bottom) / 2; |
| 418 | |
| 419 | // If we can read from the current location, it's bigger. |
| 420 | |
| 421 | if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) { |
| 422 | if (bottom == top) bottom = top = (top+1) * 2; |
| 423 | else bottom = pos; |
| 424 | |
| 425 | // If we can't, it's smaller. |
| 426 | |
| 427 | } else { |
| 428 | if (bottom == top) { |
| 429 | if (!top) return 0; |
| 430 | bottom = top/2; |
| 431 | } else top = pos; |
| 432 | } |
| 433 | } while (bottom + 1 != top); |
| 434 | |
| 435 | return pos + 1; |
| 436 | } |
| 437 | |
| 438 | // Read contents of file as a single freshly allocated nul-terminated string. |
| 439 | char *readfile(char *name) |
| 440 | { |
| 441 | off_t len; |
| 442 | int fd; |
| 443 | char *buf; |
| 444 | |
| 445 | fd = open(pidfile, O_RDONLY); |
| 446 | if (fd == -1) return 0; |
| 447 | len = fdlength(fd); |
| 448 | buf = xmalloc(len+1); |
| 449 | buf[xread(fd, buf, len)] = 0; |
| 450 | |
| 451 | return buf; |
| 452 | } |
| 453 | |
| 454 | char *xreadfile(char *name) |
| 455 | { |
| 456 | char *buf = readfile(name); |
| 457 | if (!buf) error_exit("xreadfile %s", name); |
| 458 | return buf; |
| 459 | } |
| 460 | |
| 461 | */ |
| 462 | |
| 463 | // Open a /var/run/NAME.pid file, dying if we can't write it or if it currently |
| 464 | // exists and is this executable. |
| 465 | void xpidfile(char *name) |
| 466 | { |
| 467 | char pidfile[256], spid[32]; |
| 468 | int i, fd; |
| 469 | pid_t pid; |
| 470 | |
| 471 | sprintf(pidfile, "/var/run/%s.pid", name); |
| 472 | // Try three times to open the sucker. |
| 473 | for (i=0; i<3; i++) { |
| 474 | fd = open(pidfile, O_CREAT|O_EXCL, 0644); |
| 475 | if (fd != -1) break; |
| 476 | |
| 477 | // If it already existed, read it. Loop for race condition. |
| 478 | fd = open(pidfile, O_RDONLY); |
| 479 | if (fd == -1) continue; |
| 480 | |
| 481 | // Is the old program still there? |
| 482 | spid[xread(fd, spid, sizeof(spid)-1)] = 0; |
| 483 | close(fd); |
| 484 | pid = atoi(spid); |
| 485 | if (fd < 1 || kill(pid, 0) == ESRCH) unlink(pidfile); |
| 486 | |
| 487 | // An else with more sanity checking might be nice here. |
| 488 | } |
| 489 | |
| 490 | if (i == 3) error_exit("xpidfile %s", name); |
| 491 | |
| 492 | xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid())); |
| 493 | close(fd); |
| 494 | } |