thermferm/lock.c

changeset 607
021338fc4e4f
parent 268
dda91dfa4aa8
equal deleted inserted replaced
606:798dd0c4fd00 607:021338fc4e4f
1 /***************************************************************************** 1 /*****************************************************************************
2 * Copyright (C) 2014 2 * Copyright (C) 2014-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 mbsePi-apps 6 * This file is part of the mbsePi-apps
7 * 7 *
37 37
38 snprintf(tempfile, PATH_MAX, "/var/run/%s.tmp", name); 38 snprintf(tempfile, PATH_MAX, "/var/run/%s.tmp", name);
39 snprintf(lockfile, PATH_MAX, "/var/run/%s.pid", name); 39 snprintf(lockfile, PATH_MAX, "/var/run/%s.pid", name);
40 40
41 if ((fp = fopen(tempfile, "w")) == NULL) { 41 if ((fp = fopen(tempfile, "w")) == NULL) {
42 perror(name); 42 syslog(LOG_NOTICE, "Can't create lockfile `%s': %s", tempfile, strerror(errno));
43 printf("Can't create lockfile \"%s\"\n", tempfile);
44 free(tempfile); 43 free(tempfile);
45 free(lockfile); 44 free(lockfile);
46 return 1; 45 return 1;
47 } 46 }
48 fprintf(fp, "%10u\n", getpid()); 47 fprintf(fp, "%10u\n", getpid());
54 free(tempfile); 53 free(tempfile);
55 free(lockfile); 54 free(lockfile);
56 return 0; 55 return 0;
57 } 56 }
58 if ((fp = fopen(lockfile, "r")) == NULL) { 57 if ((fp = fopen(lockfile, "r")) == NULL) {
59 perror(name); 58 syslog(LOG_NOTICE, "Can't open lockfile `%s': %s", tempfile, strerror(errno));
60 printf("Can't open lockfile \"%s\"\n", tempfile);
61 unlink(tempfile); 59 unlink(tempfile);
62 free(tempfile); 60 free(tempfile);
63 free(lockfile); 61 free(lockfile);
64 return 1; 62 return 1;
65 } 63 }
66 if (fscanf(fp, "%u", &oldpid) != 1) { 64 if (fscanf(fp, "%u", &oldpid) != 1) {
67 perror(name); 65 syslog(LOG_NOTICE, "Can't read old pid from `%s'", tempfile);
68 printf("Can't read old pid from \"%s\"\n", tempfile);
69 fclose(fp); 66 fclose(fp);
70 unlink(tempfile); 67 unlink(tempfile);
71 free(tempfile); 68 free(tempfile);
72 free(lockfile); 69 free(lockfile);
73 return 1; 70 return 1;
74 } 71 }
75 fclose(fp); 72 fclose(fp);
76 if (kill(oldpid,0) == -1) { 73 if (kill(oldpid,0) == -1) {
77 if (errno == ESRCH) { 74 if (errno == ESRCH || errno == EPERM) {
78 printf("Stale lock found for pid %u\n", oldpid); 75 syslog(LOG_NOTICE, "Stale lock found for pid %u\n", oldpid);
79 unlink(lockfile); 76 unlink(lockfile);
80 /* no return, try lock again */ 77 /* no return, try lock again */
81 } else { 78 } else {
82 perror(name); 79 syslog(LOG_NOTICE, "Kill for %u failed: %s",oldpid, strerror(errno));
83 printf("Kill for %u failed\n",oldpid);
84 unlink(tempfile); 80 unlink(tempfile);
85 free(tempfile); 81 free(tempfile);
86 free(lockfile); 82 free(lockfile);
87 return 1; 83 return 1;
88 } 84 }
89 } else { 85 } else {
90 printf("Another %s is already running, pid=%u\n", name, oldpid); 86 syslog(LOG_NOTICE, "Another %s is already running, pid=%u", name, oldpid);
91 unlink(tempfile); 87 unlink(tempfile);
92 free(tempfile); 88 free(tempfile);
93 free(lockfile); 89 free(lockfile);
94 return 1; 90 return 1;
95 } 91 }
106 102
107 lockfile = calloc(PATH_MAX, sizeof(char)); 103 lockfile = calloc(PATH_MAX, sizeof(char));
108 snprintf(lockfile, PATH_MAX, "/var/run/%s.pid", name); 104 snprintf(lockfile, PATH_MAX, "/var/run/%s.pid", name);
109 105
110 if ((fp = fopen(lockfile, "r")) == NULL) { 106 if ((fp = fopen(lockfile, "r")) == NULL) {
111 syslog(LOG_NOTICE, "Can't open lockfile \"%s\"", lockfile); 107 syslog(LOG_NOTICE, "Can't open lockfile `%s': %s", lockfile, strerror(errno));
112 free(lockfile); 108 free(lockfile);
113 return; 109 return;
114 } 110 }
115 111
116 if (fscanf(fp, "%u", &oldpid) != 1) { 112 if (fscanf(fp, "%u", &oldpid) != 1) {
117 syslog(LOG_NOTICE, "Can't read old pid from \"%s\"", lockfile); 113 syslog(LOG_NOTICE, "Can't read old pid from `%s'", lockfile);
118 fclose(fp); 114 fclose(fp);
119 unlink(lockfile); 115 unlink(lockfile);
120 free(lockfile); 116 free(lockfile);
121 return; 117 return;
122 } 118 }

mercurial