bmsd/lock.c

changeset 704
62a1d2baec75
parent 0
033898178630
child 745
3addb8cfcc3e
equal deleted inserted replaced
703:faeede125639 704:62a1d2baec75
1 /***************************************************************************** 1 /*****************************************************************************
2 * Copyright (C) 2017 2 * Copyright (C) 2017-2020
3 * 3 *
4 * Michiel Broek <mbroek at mbse dot eu> 4 * Michiel Broek <mbroek at mbse dot eu>
5 * 5 *
6 * This file is part of the bms (Brewery Management System) 6 * This file is part of the bms (Brewery Management System)
7 * 7 *
42 42
43 snprintf(tempfile, PATH_MAX, "%s/run/%s.tmp", Private_Path, name); 43 snprintf(tempfile, PATH_MAX, "%s/run/%s.tmp", Private_Path, name);
44 snprintf(lockfile, PATH_MAX, "%s/run/%s.pid", Private_Path, name); 44 snprintf(lockfile, PATH_MAX, "%s/run/%s.pid", Private_Path, name);
45 45
46 if (mkdirs(tempfile, 0755) == FALSE) { 46 if (mkdirs(tempfile, 0755) == FALSE) {
47 printf("Can't create path \"%s\"\n", tempfile); 47 syslog(LOG_NOTICE, "Can't create path `%s': %s", tempfile, strerror(errno));
48 free(tempfile); 48 free(tempfile);
49 free(lockfile); 49 free(lockfile);
50 return 1; 50 return 1;
51 } 51 }
52 52
53 if ((fp = fopen(tempfile, "w")) == NULL) { 53 if ((fp = fopen(tempfile, "w")) == NULL) {
54 perror(name); 54 syslog(LOG_NOTICE, "Can't create lockfile `%s': %s", tempfile, strerror(errno));
55 printf("Can't create lockfile \"%s\"\n", tempfile);
56 free(tempfile); 55 free(tempfile);
57 free(lockfile); 56 free(lockfile);
58 return 1; 57 return 1;
59 } 58 }
60 fprintf(fp, "%10u\n", getpid()); 59 fprintf(fp, "%10u\n", getpid());
66 free(tempfile); 65 free(tempfile);
67 free(lockfile); 66 free(lockfile);
68 return 0; 67 return 0;
69 } 68 }
70 if ((fp = fopen(lockfile, "r")) == NULL) { 69 if ((fp = fopen(lockfile, "r")) == NULL) {
71 perror(name); 70 syslog(LOG_NOTICE, "Can't open lockfile `%s': %s", tempfile, strerror(errno));
72 printf("Can't open lockfile \"%s\"\n", tempfile);
73 unlink(tempfile); 71 unlink(tempfile);
74 free(tempfile); 72 free(tempfile);
75 free(lockfile); 73 free(lockfile);
76 return 1; 74 return 1;
77 } 75 }
78 if (fscanf(fp, "%u", &oldpid) != 1) { 76 if (fscanf(fp, "%u", &oldpid) != 1) {
79 perror(name); 77 syslog(LOG_NOTICE, "Can't read old pid from `%s'", tempfile);
80 printf("Can't read old pid from \"%s\"\n", tempfile);
81 fclose(fp); 78 fclose(fp);
82 unlink(tempfile); 79 unlink(tempfile);
83 free(tempfile); 80 free(tempfile);
84 free(lockfile); 81 free(lockfile);
85 return 1; 82 return 1;
86 } 83 }
87 fclose(fp); 84 fclose(fp);
88 if (kill(oldpid,0) == -1) { 85 if (kill(oldpid,0) == -1) {
89 if (errno == ESRCH) { 86 if (errno == ESRCH || errno == EPERM) {
90 printf("Stale lock found for pid %u\n", oldpid); 87 syslog(LOG_NOTICE, "Stale lock found for pid %u", oldpid);
91 unlink(lockfile); 88 unlink(lockfile);
92 /* no return, try lock again */ 89 /* no return, try lock again */
93 } else { 90 } else {
94 perror(name); 91 syslog(LOG_NOTICE, "Kill for %u failed: %s",oldpid, strerror(errno));
95 printf("Kill for %u failed\n",oldpid);
96 unlink(tempfile); 92 unlink(tempfile);
97 free(tempfile); 93 free(tempfile);
98 free(lockfile); 94 free(lockfile);
99 return 1; 95 return 1;
100 } 96 }
101 } else { 97 } else {
102 printf("Another %s is already running, pid=%u\n", name, oldpid); 98 syslog(LOG_NOTICE, "Another %s is already running, pid=%u", name, oldpid);
103 unlink(tempfile); 99 unlink(tempfile);
104 free(tempfile); 100 free(tempfile);
105 free(lockfile); 101 free(lockfile);
106 return 1; 102 return 1;
107 } 103 }
118 114
119 lockfile = calloc(PATH_MAX, sizeof(char)); 115 lockfile = calloc(PATH_MAX, sizeof(char));
120 snprintf(lockfile, PATH_MAX, "%s/run/%s.pid", Private_Path, name); 116 snprintf(lockfile, PATH_MAX, "%s/run/%s.pid", Private_Path, name);
121 117
122 if ((fp = fopen(lockfile, "r")) == NULL) { 118 if ((fp = fopen(lockfile, "r")) == NULL) {
123 syslog(LOG_NOTICE, "Can't open lockfile \"%s\"", lockfile); 119 syslog(LOG_NOTICE, "Can't open lockfile `%s': %s", lockfile, strerror(errno));
124 free(lockfile); 120 free(lockfile);
125 return; 121 return;
126 } 122 }
127 123
128 if (fscanf(fp, "%u", &oldpid) != 1) { 124 if (fscanf(fp, "%u", &oldpid) != 1) {
129 syslog(LOG_NOTICE, "Can't read old pid from \"%s\"", lockfile); 125 syslog(LOG_NOTICE, "Can't read old pid from `%s'", lockfile);
130 fclose(fp); 126 fclose(fp);
131 unlink(lockfile); 127 unlink(lockfile);
132 free(lockfile); 128 free(lockfile);
133 return; 129 return;
134 } 130 }

mercurial