Make get_device_properties robust against long property values
[pmount-gui.git] / main.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <dirent.h>
5 #include <mntent.h>
6 #include <sys/stat.h>
7 #include <sys/wait.h>
8 #include <gtk/gtk.h>
9
10 typedef struct sProperty
11 {
12         char *name;
13         char *value;
14 } Property;
15
16 typedef struct sDevice
17 {
18         char *node;
19         char *label;
20         char *description;
21         int mounted;
22         time_t time;
23 } Device;
24
25 int verbosity = 0;
26
27 int parse_property(char *str, int size, Property *prop)
28 {
29         int equals = -1;
30         int i;
31
32         for(i=0; (equals<0 && i<size); ++i)
33                 if(str[i]=='=')
34                         equals = i;
35
36         if(equals<0)
37                 return -1;
38
39         prop->name = malloc(equals+1);
40         strncpy(prop->name, str, equals);
41         prop->name[equals] = 0;
42
43         prop->value = malloc(size-equals);
44         strncpy(prop->value, str+equals+1, size-equals-1);
45         prop->value[size-equals-1] = 0;
46
47         return 0;
48 }
49
50 Property *get_device_properties(char *node)
51 {
52         int pid;
53         int pipe_fd[2];
54
55         pipe(pipe_fd);
56
57         pid = fork();
58         if(pid==0)
59         {
60                 if(verbosity>=2)
61                         printf("Running udevadm info -q property -n \"%s\"\n", node);
62
63                 close(pipe_fd[0]);
64                 dup2(pipe_fd[1], 1);
65
66                 execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL);
67                 _exit(1);
68         }
69         else if(pid>0)
70         {
71                 char *buf;
72                 int bufsize;
73                 int pos = 0;
74                 int eof = 0;
75                 Property *props = NULL;
76                 int n_props = 0;
77
78                 close(pipe_fd[1]);
79
80                 bufsize = 256;
81                 buf = (char *)malloc(bufsize);
82
83                 while(1)
84                 {
85                         int newline;
86                         int i;
87                         Property prop;
88
89                         if(!eof)
90                         {
91                                 int len;
92
93                                 len = read(pipe_fd[0], buf+pos, bufsize-pos);
94                                 if(len==0)
95                                         eof = 1;
96                                 else if(len==-1)
97                                         break;
98                                 pos += len;
99                         }
100
101                         newline = -1;
102                         for(i=0; (newline<0 && i<pos); ++i)
103                                 if(buf[i]=='\n')
104                                         newline = i;
105
106                         if(newline<0)
107                         {
108                                 if(eof)
109                                         break;
110                                 bufsize *= 2;
111                                 buf = (char *)realloc(buf, bufsize);
112                                 continue;
113                         }
114
115                         if(parse_property(buf, newline, &prop)==0)
116                         {
117                                 props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
118                                 props[n_props] = prop;
119                                 ++n_props;
120
121                                 memmove(buf, buf+newline+1, pos-newline-1);
122                                 pos -= newline+1;
123                         }
124                         else
125                                 break;
126                 }
127
128                 free(buf);
129
130                 if(props)
131                 {
132                         props[n_props].name = NULL;
133                         props[n_props].value = NULL;
134                 }
135
136                 waitpid(pid, NULL, 0);
137                 close(pipe_fd[0]);
138
139                 return props;
140         }
141         else
142         {
143                 close(pipe_fd[0]);
144                 close(pipe_fd[1]);
145
146                 return NULL;
147         }
148 }
149
150 char *get_property_value(Property *props, char *name)
151 {
152         int i;
153         for(i=0; props[i].name; ++i)
154                 if(strcmp(props[i].name, name)==0)
155                         return props[i].value;
156         return NULL;
157 }
158
159 int match_property_value(Property *props, char *name, char *value)
160 {
161         char *v = get_property_value(props, name);
162         if(!v)
163                 return value==NULL;
164         return strcmp(v, value)==0;
165 }
166
167 void free_properties(Property *props)
168 {
169         int i;
170         if(!props)
171                 return;
172         for(i=0; props[i].name; ++i)
173         {
174                 free(props[i].name);
175                 free(props[i].value);
176         }
177         free(props);
178 }
179
180 char **get_mounted_devices(void)
181 {
182         FILE *mtab;
183         struct mntent *me;
184         char **mounted = NULL;
185         int n_mounted = 0;
186
187         mtab = setmntent("/etc/mtab", "r");
188         if(!mtab)
189                 return NULL;
190
191         while((me = getmntent(mtab)))
192         {
193                 mounted = (char **)realloc(mounted, (n_mounted+2)*sizeof(char *));
194                 mounted[n_mounted] = strdup(me->mnt_fsname);
195                 ++n_mounted;
196         }
197
198         endmntent(mtab);
199         mounted[n_mounted] = NULL;
200
201         return mounted;
202 }
203
204 int is_mounted(char **mounted, char *devname)
205 {
206         int i;
207         for(i=0; mounted[i]; ++i)
208                 if(!strcmp(devname, mounted[i]))
209                         return 1;
210         return 0;
211 }
212
213 void free_mounted_devices(char **mounted)
214 {
215         int i;
216         if(!mounted)
217                 return;
218         for(i=0; mounted[i]; ++i)
219                 free(mounted[i]);
220         free(mounted);
221 }
222
223 char **get_device_nodes(char *dirname)
224 {
225         DIR *dir;
226         struct dirent *de;
227         char fnbuf[256];
228         char linkbuf[256];
229         struct stat st;
230         char **nodes = NULL;
231         int n_nodes = 0;
232         char **checked = NULL;
233         int n_checked = 0;
234         int i;
235
236         dir = opendir(dirname);
237         if(!dir)
238                 return NULL;
239
240         while((de = readdir(dir)))
241         {
242                 char *node;
243                 int duplicate = 0;
244
245                 if(de->d_name[0]=='.' && (de->d_name[1]==0 || (de->d_name[1]=='.' && de->d_name[2]==0)))
246                         continue;
247
248                 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", dirname, de->d_name);
249
250                 node = fnbuf;
251                 lstat(fnbuf, &st);
252                 if(S_ISLNK(st.st_mode))
253                 {
254                         int len;
255                         len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
256                         if(len!=-1)
257                         {
258                                 linkbuf[len] = 0;
259                                 node = linkbuf;
260                         }
261                 }
262
263                 if(checked)
264                 {
265                         for(i=0; (!duplicate && i<n_checked); ++i)
266                                 if(strcmp(node, checked[i])==0)
267                                         duplicate = 1;
268                 }
269                 if(duplicate)
270                 {
271                         if(verbosity>=1)
272                                 printf("Device %s is a duplicate\n", fnbuf);
273                         continue;
274                 }
275
276                 checked = (char **)realloc(checked, (n_checked+1)*sizeof(char *));
277                 checked[n_checked] = strdup(node);
278                 ++n_checked;
279
280                 nodes = (char **)realloc(nodes, (n_nodes+2)*sizeof(char *));
281                 nodes[n_nodes] = strdup(fnbuf);
282                 ++n_nodes;
283         }
284
285         closedir(dir);
286         if(checked)
287         {
288                 for(i=0; i<n_checked; ++i)
289                         free(checked[i]);
290                 free(checked);
291         }
292
293         if(nodes)
294                 nodes[n_nodes] = NULL;
295
296         return nodes;
297 }
298
299 Device *get_devices(void)
300 {
301         char **nodes = NULL;
302         Device *devices = NULL;
303         int n_devices = 0;
304         char **mounted = NULL;
305         int i;
306
307         nodes = get_device_nodes("/dev/disk/by-id");
308         mounted = get_mounted_devices();
309
310         for(i=0; nodes[i]; ++i)
311         {
312                 if(verbosity>=1)
313                         printf("Examining device %s\n", nodes[i]);
314
315                 Property *props = get_device_properties(nodes[i]);
316                 if(!props)
317                 {
318                         if(verbosity>=2)
319                                 printf("  No properties\n");
320                         continue;
321                 }
322
323                 if(verbosity>=2)
324                 {
325                         int j;
326                         for(j=0; props[j].name; ++j)
327                                 printf("  %s = %s\n", props[j].name, props[j].value);
328                 }
329
330                 if(match_property_value(props, "ID_BUS", "usb") && match_property_value(props, "DEVTYPE", "partition"))
331                 {
332                         char *devname;
333                         char *label;
334                         char *vendor;
335                         char *model;
336                         char buf[256];
337                         char pos;
338                         struct stat st;
339
340                         if(verbosity>=1)
341                                 printf("  Using device\n");
342
343                         devname = get_property_value(props, "DEVNAME");
344
345                         label = get_property_value(props, "ID_FS_LABEL");
346                         if(!label)
347                                 label = get_property_value(props, "ID_FS_UUID");
348                         if(!label)
349                         {
350                                 char *ptr;
351
352                                 label = devname;
353                                 for(ptr=label; *ptr; ++ptr)
354                                         if(*ptr=='/')
355                                                 label = ptr+1;
356                         }
357
358                         vendor = get_property_value(props, "ID_VENDOR");
359                         model = get_property_value(props, "ID_MODEL");
360
361                         pos = snprintf(buf, sizeof(buf), "%s", label);
362                         if(vendor && model)
363                                 pos += snprintf(buf+pos, sizeof(buf)-pos, " (%s %s)", vendor, model);
364
365                         stat(nodes[i], &st);
366
367                         devices = (Device *)realloc(devices, (n_devices+2)*sizeof(Device));
368                         devices[n_devices].node = nodes[i];
369                         devices[n_devices].label = strdup(label);
370                         devices[n_devices].description = strdup(buf);
371                         devices[n_devices].mounted = is_mounted(mounted, devname);
372                         devices[n_devices].time = st.st_mtime;
373                         ++n_devices;
374                 }
375                 else
376                         free(nodes[i]);
377                 free_properties(props);
378         }
379
380         free(nodes);
381         free_mounted_devices(mounted);
382
383         if(devices)
384         {
385                 devices[n_devices].node = NULL;
386                 devices[n_devices].label = NULL;
387                 devices[n_devices].description = NULL;
388         }
389
390         return devices;
391 }
392
393 void free_devices(Device *devices)
394 {
395         int i;
396         if(!devices)
397                 return;
398         for(i=0; devices[i].node; ++i)
399         {
400                 free(devices[i].node);
401                 free(devices[i].label);
402         }
403         free(devices);
404 }
405
406 void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
407 {
408         GtkTreeModel *model;
409         GtkTreeIter iter;
410         int umount = *(int *)user_data;
411
412         model = gtk_tree_view_get_model(list);
413
414         if(gtk_tree_model_get_iter(model, &iter, path))
415         {
416                 Device *device;
417                 int pid;
418                 int pipe_fd[2];
419
420                 gtk_tree_model_get(model, &iter, 1, &device, -1);
421
422                 pipe(pipe_fd);
423
424                 pid = fork();
425                 if(pid==0)
426                 {
427                         if(verbosity>=2)
428                         {
429                                 if(umount)
430                                         printf("Running pumount %s\n", device->node);
431                                 else
432                                         printf("Running pmount %s %s\n", device->node, device->label);
433                         }
434
435                         close(pipe_fd[0]);
436                         dup2(pipe_fd[1], 1);
437                         dup2(pipe_fd[1], 2);
438
439                         if(umount)
440                                 execl("/usr/bin/pumount", "pumount", device->node, NULL);
441                         else
442                                 execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL);
443                         _exit(1);
444                 }
445                 else if(pid>0)
446                 {
447                         char buf[1024];
448                         int pos = 0;
449                         int status;
450
451                         close(pipe_fd[1]);
452
453                         while(1)
454                         {
455                                 int len;
456
457                                 len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1);
458                                 if(len<=0)
459                                         break;
460                                 pos += len;
461                         }
462
463                         buf[pos] = 0;
464
465                         waitpid(pid, &status, 0);
466                         if(!WIFEXITED(status) || WEXITSTATUS(status))
467                         {
468                                 GtkWidget *dialog;
469
470                                 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf);
471                                 g_signal_connect(dialog, "response", &gtk_main_quit, NULL);
472                                 gtk_widget_show_all(dialog);
473                         }
474                         else
475                                 gtk_main_quit();
476                 }
477                 else
478                 {
479                 }
480         }
481
482         (void)column;
483 }
484
485 int main(int argc, char **argv)
486 {
487         GtkWidget *window;
488         GtkWidget *viewport;
489         GtkWidget *list;
490         GtkListStore *store;
491         GtkTreeSelection *selection;
492         GtkTreeIter iter;
493         Device *devices;
494         int i;
495         time_t latest;
496         int opt;
497         int umount = 0;
498         int n_listed;
499
500         gtk_init(&argc, &argv);
501
502         while((opt = getopt(argc, argv, "vu"))!=-1) switch(opt)
503         {
504         case 'v':
505                 ++verbosity;
506                 break;
507         case 'u':
508                 umount = 1;
509                 break;
510         }
511
512         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
513         gtk_container_set_border_width(GTK_CONTAINER(window), 5);
514         g_signal_connect(window, "destroy", &gtk_main_quit, NULL);
515
516         viewport = gtk_viewport_new(NULL, NULL);
517         gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
518         gtk_container_add(GTK_CONTAINER(window), viewport);
519
520         list = gtk_tree_view_new();
521         gtk_container_add(GTK_CONTAINER(viewport), list);
522         g_signal_connect(list, "row-activated", (GCallback)&row_activated, &umount);
523
524         store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
525         gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
526         gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),
527                 -1, "Device", gtk_cell_renderer_text_new(), "text", 0, NULL);
528
529         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
530
531         devices = get_devices();
532         n_listed = 0;
533         if(devices)
534         {
535                 latest = 0;
536                 for(i=0; devices[i].node; ++i)
537                         if(!devices[i].mounted==!umount)
538                         {
539                                 gtk_list_store_append(store, &iter);
540                                 gtk_list_store_set(store, &iter, 0, devices[i].description, 1, &devices[i], -1);
541                                 if(devices[i].time>latest)
542                                 {
543                                         latest = devices[i].time;
544                                         gtk_tree_selection_select_iter(selection, &iter);
545                                 }
546
547                                 ++n_listed;
548                         }
549
550         }
551
552         if(n_listed)
553                 gtk_widget_show_all(window);
554         else
555         {
556                 GtkWidget *dialog;
557
558                 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
559                         "No devices to %s", (umount ? "unmount" : "mount"));
560                 g_signal_connect(dialog, "response", &gtk_main_quit, NULL);
561                 gtk_widget_show_all(dialog);
562         }
563
564         gtk_main();
565
566         free_devices(devices);
567
568         return 0;
569 }