* Test that pidfile_open() locks against self. */
| 84 | * Test that pidfile_open() locks against self. |
| 85 | */ |
| 86 | static const char * |
| 87 | test_pidfile_self(void) |
| 88 | { |
| 89 | const char *fn = "test_pidfile_self"; |
| 90 | struct pidfh *pf1, *pf2; |
| 91 | pid_t other = 0; |
| 92 | int serrno; |
| 93 | |
| 94 | unlink(fn); |
| 95 | pf1 = pidfile_open(fn, 0600, &other); |
| 96 | if (pf1 == NULL && other != 0) |
| 97 | return ("pidfile exists and is locked"); |
| 98 | if (pf1 == NULL) |
| 99 | return (strerror(errno)); |
| 100 | if (pidfile_write(pf1) != 0) { |
| 101 | serrno = errno; |
| 102 | pidfile_close(pf1); |
| 103 | unlink(fn); |
| 104 | return (strerror(serrno)); |
| 105 | } |
| 106 | // second open should fail |
| 107 | pf2 = pidfile_open(fn, 0600, &other); |
| 108 | if (pf2 != NULL) { |
| 109 | pidfile_close(pf1); |
| 110 | pidfile_close(pf2); |
| 111 | unlink(fn); |
| 112 | return ("managed to opened pidfile twice"); |
| 113 | } |
| 114 | if (other != getpid()) { |
| 115 | pidfile_close(pf1); |
| 116 | unlink(fn); |
| 117 | return ("pidfile contained wrong PID"); |
| 118 | } |
| 119 | pidfile_close(pf1); |
| 120 | unlink(fn); |
| 121 | return (NULL); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Common code for test_pidfile_{contested,inherited}. |
nothing calls this directly
no test coverage detected