summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2009-08-04 13:11:34 +0200
committerGravatar Nikias Bassen2009-08-04 13:11:34 +0200
commit679f6cd2904aa84f65a4f0fc38a353f5ac7b9e46 (patch)
tree18a4d1c48db5b0caf02ee68ee5cf6acebfd4ef6c
parent6184b8e69890187a902b5bc4aa4c065ed9fd23c2 (diff)
downloadusbmuxd-679f6cd2904aa84f65a4f0fc38a353f5ac7b9e46.tar.gz
usbmuxd-679f6cd2904aa84f65a4f0fc38a353f5ac7b9e46.tar.bz2
udev operation mode; -x and -X options to exit a running instance
In udev mode (-u|--udev) the return value is 0 if another instance is already running -x|--exit tells a running instance to exit by sending signal 3 (SIGQUIT) -X|--force-exit tells a running instance to exit even if devices are still connected by sending signal 15 (SIGTERM)
-rw-r--r--src/main.c61
1 files changed, 55 insertions, 6 deletions
diff --git a/src/main.c b/src/main.c
index ee633b6..c567a66 100644
--- a/src/main.c
+++ b/src/main.c
@@ -58,6 +58,9 @@ static int verbose = DEBUG_LEVEL;
58static int foreground = 0; 58static int foreground = 0;
59static int exit_on_no_devices = 0; 59static int exit_on_no_devices = 0;
60static int drop_privileges = 0; 60static int drop_privileges = 0;
61static int opt_udev = 0;
62static int opt_exit = 0;
63static int exit_signal = 0;
61 64
62struct device_info { 65struct device_info {
63 uint32_t device_id; 66 uint32_t device_id;
@@ -1030,6 +1033,10 @@ static void usage()
1030 printf("\t-f|--foreground do not daemonize\n"); 1033 printf("\t-f|--foreground do not daemonize\n");
1031 printf("\t-e|--exit-on-no-devices exit if no device is attached\n"); 1034 printf("\t-e|--exit-on-no-devices exit if no device is attached\n");
1032 printf("\t-d|--drop-privileges drop privileges after startup\n"); 1035 printf("\t-d|--drop-privileges drop privileges after startup\n");
1036 printf("\t-u|--udev udev operation mode\n");
1037 printf("\t-x|--exit tell a running instance to exit\n");
1038 printf("\t-X|--force-exit tell a running instance to exit, even if\n");
1039 printf("\t there are still devices connected\n");
1033 printf("\n"); 1040 printf("\n");
1034} 1041}
1035 1042
@@ -1041,12 +1048,15 @@ static void parse_opts(int argc, char **argv)
1041 {"verbose", 0, NULL, 'v'}, 1048 {"verbose", 0, NULL, 'v'},
1042 {"exit-on-no-devices", 0, NULL, 'e'}, 1049 {"exit-on-no-devices", 0, NULL, 'e'},
1043 {"drop-privileges", 0, NULL, 'd'}, 1050 {"drop-privileges", 0, NULL, 'd'},
1051 {"udev", 0, NULL, 'u'},
1052 {"exit", 0, NULL, 'x'},
1053 {"force-exit", 0, NULL, 'X'},
1044 {NULL, 0, NULL, 0} 1054 {NULL, 0, NULL, 0}
1045 }; 1055 };
1046 int c; 1056 int c;
1047 1057
1048 while (1) { 1058 while (1) {
1049 c = getopt_long(argc, argv, "hfved", longopts, (int *) 0); 1059 c = getopt_long(argc, argv, "hfveduxX", longopts, (int *) 0);
1050 if (c == -1) { 1060 if (c == -1) {
1051 break; 1061 break;
1052 } 1062 }
@@ -1067,11 +1077,24 @@ static void parse_opts(int argc, char **argv)
1067 case 'd': 1077 case 'd':
1068 drop_privileges = 1; 1078 drop_privileges = 1;
1069 break; 1079 break;
1080 case 'u':
1081 opt_udev = 1;
1082 break;
1083 case 'x':
1084 opt_exit = 1;
1085 exit_signal = SIGQUIT;
1086 break;
1087 case 'X':
1088 opt_exit = 1;
1089 exit_signal = SIGTERM;
1090 break;
1070 default: 1091 default:
1071 usage(); 1092 usage();
1072 exit(2); 1093 exit(2);
1073 } 1094 }
1074 } 1095 }
1096 if (opt_udev)
1097 foreground = 0;
1075} 1098}
1076 1099
1077/** 1100/**
@@ -1141,6 +1164,7 @@ int main(int argc, char **argv)
1141 int children_capacity = DEFAULT_CHILDREN_CAPACITY; 1164 int children_capacity = DEFAULT_CHILDREN_CAPACITY;
1142 int i; 1165 int i;
1143 int result = 0; 1166 int result = 0;
1167 int exit_val = 0;
1144 int cnt = 0; 1168 int cnt = 0;
1145 FILE *lfd = NULL; 1169 FILE *lfd = NULL;
1146 struct flock lock; 1170 struct flock lock;
@@ -1154,7 +1178,7 @@ int main(int argc, char **argv)
1154 openlog("usbmuxd", LOG_PID, 0); 1178 openlog("usbmuxd", LOG_PID, 0);
1155 } 1179 }
1156 1180
1157 if (verbose >= 2) 1181 if (verbose >= 1)
1158 logmsg(LOG_NOTICE, "starting"); 1182 logmsg(LOG_NOTICE, "starting");
1159 1183
1160 // signal(SIGHUP, reload_conf); // none yet 1184 // signal(SIGHUP, reload_conf); // none yet
@@ -1173,12 +1197,35 @@ int main(int argc, char **argv)
1173 fcntl(fileno(lfd), F_GETLK, &lock); 1197 fcntl(fileno(lfd), F_GETLK, &lock);
1174 fclose(lfd); 1198 fclose(lfd);
1175 if (lock.l_type != F_UNLCK) { 1199 if (lock.l_type != F_UNLCK) {
1176 logmsg(LOG_NOTICE, 1200 if (opt_exit) {
1177 "another instance is already running. exiting."); 1201 if (lock.l_pid && !kill(lock.l_pid, 0)) {
1178 return -1; 1202 logmsg(LOG_NOTICE, "sending signal %d to instance with pid %d", exit_signal, lock.l_pid);
1203 if (kill(lock.l_pid, exit_signal) < 0) {
1204 logmsg(LOG_ERR, "Error: could not deliver signal %d to pid %d", exit_signal, lock.l_pid);
1205 }
1206 exit_val = 0;
1207 goto terminate;
1208 } else {
1209 logmsg(LOG_ERR, "Error: could not determine pid of the other running instance!");
1210 exit_val = -1;
1211 goto terminate;
1212 }
1213 } else {
1214 logmsg(LOG_NOTICE,
1215 "another instance is already running (pid %d). exiting.", lock.l_pid);
1216 if (!opt_udev) {
1217 exit_val = -1;
1218 }
1219 goto terminate;
1220 }
1179 } 1221 }
1180 } 1222 }
1181 1223
1224 if (opt_exit) {
1225 logmsg(LOG_NOTICE, "no running instance found, none killed. exiting.");
1226 goto terminate;
1227 }
1228
1182 if (exit_on_no_devices) { 1229 if (exit_on_no_devices) {
1183 if (devices_attached() <= 0) { 1230 if (devices_attached() <= 0) {
1184 logmsg(LOG_NOTICE, "no devices attached. exiting."); 1231 logmsg(LOG_NOTICE, "no devices attached. exiting.");
@@ -1220,6 +1267,7 @@ int main(int argc, char **argv)
1220 lock.l_len = 0; 1267 lock.l_len = 0;
1221 if (fcntl(fileno(lfd), F_SETLK, &lock) == -1) { 1268 if (fcntl(fileno(lfd), F_SETLK, &lock) == -1) {
1222 logmsg(LOG_ERR, "ERROR: lockfile locking failed!"); 1269 logmsg(LOG_ERR, "ERROR: lockfile locking failed!");
1270 exit(EXIT_FAILURE);
1223 } 1271 }
1224 } 1272 }
1225 // drop elevated privileges 1273 // drop elevated privileges
@@ -1388,8 +1436,9 @@ int main(int argc, char **argv)
1388 fclose(lfd); 1436 fclose(lfd);
1389 } 1437 }
1390 1438
1439terminate:
1391 if (verbose >= 1) 1440 if (verbose >= 1)
1392 logmsg(LOG_NOTICE, "usbmuxd: terminated"); 1441 logmsg(LOG_NOTICE, "terminated");
1393 if (!foreground) { 1442 if (!foreground) {
1394 closelog(); 1443 closelog();
1395 } 1444 }