| 1330 | } |
| 1331 | |
| 1332 | static int |
| 1333 | crm_lock_pidfile(const char *filename) |
| 1334 | { |
| 1335 | long mypid = 0; |
| 1336 | int fd = 0, rc = 0; |
| 1337 | char buf[LOCKSTRLEN + 1]; |
| 1338 | |
| 1339 | mypid = (unsigned long)getpid(); |
| 1340 | |
| 1341 | rc = crm_pidfile_inuse(filename, 0); |
| 1342 | if(rc != pcmk_ok && rc != -ENOENT) { |
| 1343 | /* locked by existing process - give up */ |
| 1344 | return -1; |
| 1345 | } |
| 1346 | |
| 1347 | if ((fd = open(filename, O_CREAT | O_WRONLY | O_EXCL, 0644)) < 0) { |
| 1348 | /* Hmmh, why did we fail? Anyway, nothing we can do about it */ |
| 1349 | return -errno; |
| 1350 | } |
| 1351 | |
| 1352 | snprintf(buf, sizeof(buf), "%*lu\n", LOCKSTRLEN - 1, mypid); |
| 1353 | rc = write(fd, buf, LOCKSTRLEN); |
| 1354 | close(fd); |
| 1355 | |
| 1356 | if(rc != LOCKSTRLEN) { |
| 1357 | crm_perror(LOG_ERR, "Incomplete write to %s", filename); |
| 1358 | return -errno; |
| 1359 | |
| 1360 | } |
| 1361 | |
| 1362 | return crm_pidfile_inuse(filename, mypid); |
| 1363 | } |
| 1364 | |
| 1365 | void |
| 1366 | crm_make_daemon(const char *name, gboolean daemonize, const char *pidfile) |
no test coverage detected