31 #include <sys/types.h>
75 char lockfile[PATH_MAX];
77 snprintf(lockfile, PATH_MAX,
"%s/%s", path, name);
81 if (access(lockfile, F_OK) == 0) {
83 if (unlink(lockfile) != 0) {
85 snprintf(errstr, errlen,
86 "Could not remove lockfile: %s\n"
88 lockfile, strerror(errno));
95 else if (errno != ENOENT) {
97 snprintf(errstr, errlen,
98 "Could not access lockfile: %s\n"
100 lockfile, strerror(errno));
137 char lockfile[PATH_MAX];
142 char time_string[32];
145 char lockfile_string[256];
149 time_t lockfile_time;
155 snprintf(lockfile, PATH_MAX,
"%s/%s", path, name);
159 if (gethostname(hostname, 256) == -1) {
161 snprintf(errstr, errlen,
162 "Could not get hostname for lockfile:\n"
165 lockfile, strerror(errno));
178 if (access(lockfile, F_OK) == 0) {
182 lockfile_fp = fopen(lockfile,
"r");
186 snprintf(errstr, errlen,
187 "Lockfile exists but could not be opened:\n"
190 lockfile, strerror(errno));
197 if (!fgets(lockfile_string, 256, lockfile_fp)) {
199 snprintf(errstr, errlen,
200 "Lockfile exists but could not be read:\n"
203 lockfile, strerror(errno));
210 lockfile_host = strdup(lockfile_string);
211 if (!lockfile_host) {
213 snprintf(errstr, errlen,
216 " -> but could not parse identifier string: '%s'\n"
217 " -> memory allocation error",
218 lockfile, lockfile_string);
225 chrp = strchr(lockfile_host,
':');
228 snprintf(errstr, errlen,
231 " -> but has an invalid identifier string: '%s'\n",
232 lockfile, lockfile_string);
239 lockfile_pid = atoi(++chrp);
243 chrp = strchr(chrp,
':');
246 snprintf(errstr, errlen,
249 " -> but has an invalid identifier string: '%s'\n",
250 lockfile, lockfile_string);
256 lockfile_time = (time_t)atol(++chrp);
262 if ((strcmp(lockfile_host, hostname) == 0) &&
263 (lockfile_time == pid_time) ) {
265 snprintf(errstr, errlen,
269 lockfile, lockfile_string);
285 else if (errno != ENOENT) {
287 snprintf(errstr, errlen,
288 "Could not check if lockfile exists:\n"
291 lockfile, strerror(errno));
304 lockfile_fp = fopen(lockfile,
"w");
308 snprintf(errstr, errlen,
309 "Could not create lockfile:\n"
312 lockfile, strerror(errno));
317 nbytes = fprintf(lockfile_fp,
"%s:%d:%ld %s\n",
318 hostname, (
int)pid, (
long)pid_time, time_string);
322 snprintf(errstr, errlen,
323 "Could not write to lockfile:\n"
326 lockfile, strerror(errno));
334 if (fclose(lockfile_fp) != 0) {
336 snprintf(errstr, errlen,
337 "Could not close lockfile:\n"
340 lockfile, strerror(errno));