summaryrefslogtreecommitdiffstats
path: root/usbmuxd
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2009-08-16 19:31:43 +0200
committerGravatar Hector Martin2009-08-16 21:50:53 +0200
commit01820bcc6fe3281edabd93dd10f2bb0f612ded41 (patch)
tree9ad6d1aeb0a846c6b1b8a6474a8c53907ae9bfeb /usbmuxd
parentcb7845397842fb813bae9aa2f9d10b75e04ce8e6 (diff)
downloadusbmuxd-01820bcc6fe3281edabd93dd10f2bb0f612ded41.tar.gz
usbmuxd-01820bcc6fe3281edabd93dd10f2bb0f612ded41.tar.bz2
Added lockfile mechanism to prevent multiple running instances.
Diffstat (limited to 'usbmuxd')
-rw-r--r--usbmuxd/main.c38
1 files changed, 37 insertions, 1 deletions
diff --git a/usbmuxd/main.c b/usbmuxd/main.c
index ea332a4..88c31da 100644
--- a/usbmuxd/main.c
+++ b/usbmuxd/main.c
@@ -33,6 +33,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33#include <sys/socket.h> 33#include <sys/socket.h>
34#include <sys/un.h> 34#include <sys/un.h>
35#include <sys/stat.h> 35#include <sys/stat.h>
36#include <sys/fcntl.h>
36#include <getopt.h> 37#include <getopt.h>
37#include <pwd.h> 38#include <pwd.h>
38 39
@@ -42,6 +43,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42#include "client.h" 43#include "client.h"
43 44
44static const char *socket_path = "/var/run/usbmuxd"; 45static const char *socket_path = "/var/run/usbmuxd";
46static const char *lockfile = "/var/run/usbmuxd.lock";
47
45int should_exit; 48int should_exit;
46 49
47struct sigaction sa_old; 50struct sigaction sa_old;
@@ -268,7 +271,9 @@ static void parse_opts(int argc, char **argv)
268int main(int argc, char *argv[]) 271int main(int argc, char *argv[])
269{ 272{
270 int listenfd; 273 int listenfd;
271 int res; 274 int res = 0;
275 FILE *lfd = NULL;
276 struct flock lock;
272 277
273 parse_opts(argc, argv); 278 parse_opts(argc, argv);
274 279
@@ -289,6 +294,22 @@ int main(int argc, char *argv[])
289 294
290 set_signal_handlers(); 295 set_signal_handlers();
291 296
297 lfd = fopen(lockfile, "r");
298 if (lfd) {
299 lock.l_type = 0;
300 lock.l_whence = SEEK_SET;
301 lock.l_start = 0;
302 lock.l_len = 0;
303 fcntl(fileno(lfd), F_GETLK, &lock);
304 fclose(lfd);
305 if (lock.l_type != F_UNLCK) {
306 usbmuxd_log(LL_NOTICE,
307 "another instance is already running (pid %d). exiting.", lock.l_pid);
308 res = -1;
309 goto terminate;
310 }
311 }
312
292 usbmuxd_log(LL_INFO, "Creating socket"); 313 usbmuxd_log(LL_INFO, "Creating socket");
293 listenfd = create_socket(); 314 listenfd = create_socket();
294 if(listenfd < 0) 315 if(listenfd < 0)
@@ -312,6 +333,20 @@ int main(int argc, char *argv[])
312 } 333 }
313 } 334 }
314 335
336 // now open the lockfile and place the lock
337 lfd = fopen(lockfile, "w");
338 if (lfd) {
339 lock.l_type = F_WRLCK;
340 lock.l_whence = SEEK_SET;
341 lock.l_start = 0;
342 lock.l_len = 0;
343 if (fcntl(fileno(lfd), F_SETLK, &lock) == -1) {
344 usbmuxd_log(LL_ERROR, "ERROR: lockfile locking failed!");
345 log_disable_syslog();
346 exit(EXIT_FAILURE);
347 }
348 }
349
315 // drop elevated privileges 350 // drop elevated privileges
316 if (drop_privileges && (getuid() == 0 || geteuid() == 0)) { 351 if (drop_privileges && (getuid() == 0 || geteuid() == 0)) {
317 struct passwd *pw = getpwnam("nobody"); 352 struct passwd *pw = getpwnam("nobody");
@@ -342,6 +377,7 @@ int main(int argc, char *argv[])
342 usb_shutdown(); 377 usb_shutdown();
343 device_shutdown(); 378 device_shutdown();
344 client_shutdown(); 379 client_shutdown();
380terminate:
345 usbmuxd_log(LL_NOTICE, "Shutdown complete"); 381 usbmuxd_log(LL_NOTICE, "Shutdown complete");
346 382
347 log_disable_syslog(); 383 log_disable_syslog();