summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Bastien Nocera2009-08-03 21:22:37 +0200
committerGravatar Nikias Bassen2009-08-03 21:22:37 +0200
commitb7d4f48d7e85c43f0dd1111619acf79aba535371 (patch)
tree5ad019d11c072ef537e1d3980f35b5cef7f9dc7a /src
parent544f0420cd265194cd0c610f74ef928a60358e68 (diff)
downloadusbmuxd-b7d4f48d7e85c43f0dd1111619acf79aba535371.tar.gz
usbmuxd-b7d4f48d7e85c43f0dd1111619acf79aba535371.tar.bz2
Add udev mode of operation
When starting up, force background operation when in udev mode and don't error out when already running. When disconnecting, check if there are any devices left-over, and exit if not. Signed-off-by: Nikias Bassen <nikias@gmx.li>
Diffstat (limited to 'src')
-rw-r--r--src/main.c25
1 files changed, 21 insertions, 4 deletions
diff --git a/src/main.c b/src/main.c
index ee633b6..de68242 100644
--- a/src/main.c
+++ b/src/main.c
@@ -58,6 +58,7 @@ 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 udev = 0;
61 62
62struct device_info { 63struct device_info {
63 uint32_t device_id; 64 uint32_t device_id;
@@ -1030,6 +1031,7 @@ static void usage()
1030 printf("\t-f|--foreground do not daemonize\n"); 1031 printf("\t-f|--foreground do not daemonize\n");
1031 printf("\t-e|--exit-on-no-devices exit if no device is attached\n"); 1032 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"); 1033 printf("\t-d|--drop-privileges drop privileges after startup\n");
1034 printf("\t-u|--udev use udev mode of operations\n");
1033 printf("\n"); 1035 printf("\n");
1034} 1036}
1035 1037
@@ -1041,12 +1043,13 @@ static void parse_opts(int argc, char **argv)
1041 {"verbose", 0, NULL, 'v'}, 1043 {"verbose", 0, NULL, 'v'},
1042 {"exit-on-no-devices", 0, NULL, 'e'}, 1044 {"exit-on-no-devices", 0, NULL, 'e'},
1043 {"drop-privileges", 0, NULL, 'd'}, 1045 {"drop-privileges", 0, NULL, 'd'},
1046 {"udev", 0, NULL, 'u'},
1044 {NULL, 0, NULL, 0} 1047 {NULL, 0, NULL, 0}
1045 }; 1048 };
1046 int c; 1049 int c;
1047 1050
1048 while (1) { 1051 while (1) {
1049 c = getopt_long(argc, argv, "hfved", longopts, (int *) 0); 1052 c = getopt_long(argc, argv, "hfvedu", longopts, (int *) 0);
1050 if (c == -1) { 1053 if (c == -1) {
1051 break; 1054 break;
1052 } 1055 }
@@ -1067,11 +1070,17 @@ static void parse_opts(int argc, char **argv)
1067 case 'd': 1070 case 'd':
1068 drop_privileges = 1; 1071 drop_privileges = 1;
1069 break; 1072 break;
1073 case 'u':
1074 udev = 1;
1075 break;
1070 default: 1076 default:
1071 usage(); 1077 usage();
1072 exit(2); 1078 exit(2);
1073 } 1079 }
1074 } 1080 }
1081
1082 if (udev)
1083 foreground = 0;
1075} 1084}
1076 1085
1077/** 1086/**
@@ -1173,9 +1182,12 @@ int main(int argc, char **argv)
1173 fcntl(fileno(lfd), F_GETLK, &lock); 1182 fcntl(fileno(lfd), F_GETLK, &lock);
1174 fclose(lfd); 1183 fclose(lfd);
1175 if (lock.l_type != F_UNLCK) { 1184 if (lock.l_type != F_UNLCK) {
1176 logmsg(LOG_NOTICE, 1185 if (!udev) {
1177 "another instance is already running. exiting."); 1186 logmsg(LOG_NOTICE,
1178 return -1; 1187 "another instance is already running. exiting.");
1188 return -1;
1189 }
1190 return 0;
1179 } 1191 }
1180 } 1192 }
1181 1193
@@ -1265,6 +1277,7 @@ int main(int argc, char **argv)
1265 if (result <= 0) { 1277 if (result <= 0) {
1266 if (result == 0) { 1278 if (result == 0) {
1267 // cleanup 1279 // cleanup
1280 int num_children = 0;
1268 for (i = 0; i < children_capacity; i++) { 1281 for (i = 0; i < children_capacity; i++) {
1269 if (children[i]) { 1282 if (children[i]) {
1270 if (children[i]->dead != 0) { 1283 if (children[i]->dead != 0) {
@@ -1277,6 +1290,7 @@ int main(int argc, char **argv)
1277 children[i] = NULL; 1290 children[i] = NULL;
1278 cnt++; 1291 cnt++;
1279 } else { 1292 } else {
1293 num_children++;
1280 cnt = 0; 1294 cnt = 0;
1281 } 1295 }
1282 } else { 1296 } else {
@@ -1284,6 +1298,9 @@ int main(int argc, char **argv)
1284 } 1298 }
1285 } 1299 }
1286 1300
1301 if (num_children == 0 && udev)
1302 break;
1303
1287 if ((children_capacity > DEFAULT_CHILDREN_CAPACITY) 1304 if ((children_capacity > DEFAULT_CHILDREN_CAPACITY)
1288 && ((children_capacity - cnt) <= 1305 && ((children_capacity - cnt) <=
1289 DEFAULT_CHILDREN_CAPACITY)) { 1306 DEFAULT_CHILDREN_CAPACITY)) {