]> git.tdb.fi Git - pmount-gui.git/blob - main.c
Utilize early returns to reduce excess indentation
[pmount-gui.git] / main.c
1 /* Required for strdup, snprintf, getopt */
2 #define _XOPEN_SOURCE 500
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <dirent.h>
9 #include <mntent.h>
10 #include <sys/stat.h>
11 #include <sys/wait.h>
12 #include <sys/select.h>
13 #include <gtk/gtk.h>
14 #include <gdk/gdkkeysyms.h>
15
16 typedef struct sProperty
17 {
18         char *name;
19         char *value;
20 } Property;
21
22 typedef struct sDevice
23 {
24         char *node;
25         char *label;
26         char *description;
27         int mounted;
28         time_t time;
29 } Device;
30
31 int verbosity = 0;
32
33 /**
34 Parses a string of the form name=value and places the components in a Property
35 structure.  Returns 0 on success, or -1 if the string wasn't a valid property.
36 */
37 int parse_property(char *str, int size, Property *prop)
38 {
39         int equals = -1;
40         int i;
41
42         for(i=0; (equals<0 && i<size); ++i)
43                 if(str[i]=='=')
44                         equals = i;
45
46         if(equals<0)
47                 return -1;
48
49         prop->name = malloc(equals+1);
50         strncpy(prop->name, str, equals);
51         prop->name[equals] = 0;
52
53         prop->value = malloc(size-equals);
54         strncpy(prop->value, str+equals+1, size-equals-1);
55         prop->value[size-equals-1] = 0;
56
57         return 0;
58 }
59
60 /**
61 Retrieves all properties associated with a /dev node.  The returned array is
62 terminated with an entry containing NULL values.  Use free_properties to free
63 the array.
64 */
65 Property *get_device_properties(char *node)
66 {
67         int pid;
68         int pipe_fd[2];
69         char *buf;
70         int bufsize;
71         int pos = 0;
72         int eof = 0;
73         Property *props = NULL;
74         int n_props = 0;
75
76         pipe(pipe_fd);
77
78         pid = fork();
79         if(pid==0)
80         {
81                 /* Child process */
82                 if(verbosity>=2)
83                         printf("Running udevadm info -q property -n \"%s\"\n", node);
84
85                 close(pipe_fd[0]);
86                 dup2(pipe_fd[1], 1);
87
88                 execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL);
89                 _exit(1);
90         }
91         else if(pid<0)
92         {
93                 close(pipe_fd[0]);
94                 close(pipe_fd[1]);
95
96                 return NULL;
97         }
98
99         /* Parent process */
100         close(pipe_fd[1]);
101
102         bufsize = 256;
103         buf = (char *)malloc(bufsize);
104
105         while(1)
106         {
107                 int newline;
108                 int i;
109                 Property prop;
110
111                 if(!eof)
112                 {
113                         int len;
114
115                         len = read(pipe_fd[0], buf+pos, bufsize-pos);
116                         if(len==0)
117                                 eof = 1;
118                         else if(len==-1)
119                                 break;
120                         pos += len;
121                 }
122
123                 newline = -1;
124                 for(i=0; (newline<0 && i<pos); ++i)
125                         if(buf[i]=='\n')
126                                 newline = i;
127
128                 if(newline<0)
129                 {
130                         if(eof)
131                                 break;
132
133                         /* There was no newline in the buffer but there is more output to
134                         be read.  Try again with a larger buffer. */
135                         bufsize *= 2;
136                         buf = (char *)realloc(buf, bufsize);
137                         continue;
138                 }
139
140                 if(parse_property(buf, newline, &prop)==0)
141                 {
142                         /* Reserve space for a sentinel value as well. */
143                         props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
144                         props[n_props] = prop;
145                         ++n_props;
146
147                         memmove(buf, buf+newline+1, pos-newline-1);
148                         pos -= newline+1;
149                 }
150                 else
151                         break;
152         }
153
154         free(buf);
155
156         if(props)
157         {
158                 /* Terminate the array with NULL pointers. */
159                 props[n_props].name = NULL;
160                 props[n_props].value = NULL;
161         }
162
163         waitpid(pid, NULL, 0);
164         close(pipe_fd[0]);
165
166         return props;
167 }
168
169 /**
170 Looks for a property in an array of properties and returns its value.  Returns
171 NULL if the property was not found.
172 */
173 char *get_property_value(Property *props, char *name)
174 {
175         int i;
176         for(i=0; props[i].name; ++i)
177                 if(strcmp(props[i].name, name)==0)
178                         return props[i].value;
179         return NULL;
180 }
181
182 /**
183 Checks if a property has a specific value.  A NULL value is matched if the
184 property does not exist.
185 */
186 int match_property_value(Property *props, char *name, char *value)
187 {
188         char *v = get_property_value(props, name);
189         if(!v)
190                 return value==NULL;
191         return strcmp(v, value)==0;
192 }
193
194 /**
195 Frees an array of properties and all strings contained in it.
196 */
197 void free_properties(Property *props)
198 {
199         int i;
200         if(!props)
201                 return;
202         for(i=0; props[i].name; ++i)
203         {
204                 free(props[i].name);
205                 free(props[i].value);
206         }
207         free(props);
208 }
209
210 /**
211 Reads device names from an fstab/mtab file.
212 */
213 char **get_mount_entries(char *filename, int (*predicate)(struct mntent *))
214 {
215         FILE *file;
216         struct mntent *me;
217         char **devices = NULL;
218         int n_devices = 0;
219
220         file = setmntent(filename, "r");
221         if(!file)
222                 return NULL;
223
224         while((me = getmntent(file)))
225                 if(!predicate || predicate(me))
226                 {
227                         devices = (char **)realloc(devices, (n_devices+2)*sizeof(char *));
228                         devices[n_devices] = strdup(me->mnt_fsname);
229                         ++n_devices;
230                 }
231
232         endmntent(file);
233         if(devices)
234                 devices[n_devices] = NULL;
235
236         return devices;
237 }
238
239 /**
240 Returns an array of all currently mounted devices.
241 */
242 char **get_mounted_devices(void)
243 {
244         return get_mount_entries("/etc/mtab", NULL);
245 }
246
247 /**
248 Checks if an fstab entry has the user option set.
249 */
250 int is_user_mountable(struct mntent *me)
251 {
252         return hasmntopt(me, "user")!=NULL;
253 }
254
255 /**
256 Returns an array of user-mountable devices listed in fstab.
257 */
258 char **get_fstab_devices(void)
259 {
260         return get_mount_entries("/etc/fstab", &is_user_mountable);
261 }
262
263 /**
264 Checks if an array of strings contains the specified string.
265 */
266 int is_in_array(char **array, char *str)
267 {
268         int i;
269         if(!array || !str)
270                 return 0;
271         for(i=0; array[i]; ++i)
272                 if(!strcmp(str, array[i]))
273                         return 1;
274         return 0;
275 }
276
277 /**
278 Frees an array of strings.
279 */
280 void free_string_array(char **array)
281 {
282         int i;
283         if(!array)
284                 return;
285         for(i=0; array[i]; ++i)
286                 free(array[i]);
287         free(array);
288 }
289
290 /**
291 Checks if a partition identified by a sysfs path is on a removable device.
292 */
293 int is_removable(char *devpath)
294 {
295         char fnbuf[256];
296         int len;
297         char *ptr;
298         int fd;
299
300         len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
301         /* Default to not removable if the path was too long. */
302         if(len+10>=(int)sizeof(fnbuf))
303                 return 0;
304
305         /* We got a partition as a parameter, but the removable property is on the
306         disk.  Replace the last component with "removable". */
307         for(ptr=fnbuf+len; (ptr>fnbuf && *ptr!='/'); --ptr) ;
308         strcpy(ptr, "/removable");
309
310         fd = open(fnbuf, O_RDONLY);
311         if(fd!=-1)
312         {
313                 char c;
314                 read(fd, &c, 1);
315                 close(fd);
316                 if(c=='1')
317                 {
318                         if(verbosity>=2)
319                                 printf("  Removable\n");
320                         return 1;
321                 }
322                 if(verbosity>=2)
323                         printf("  Not removable\n");
324         }
325
326         return 0;
327 }
328
329 /**
330 Checks if a partition's disk or any of its parent devices are connected to any
331 of a set of buses.  The device is identified by a sysfs path.  The bus array
332 must be terminated with a NULL entry.
333 */
334 int check_buses(char *devpath, char **buses)
335 {
336         char fnbuf[256];
337         char *ptr;
338         int len;
339
340         len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
341         /* Default to no match if the path was too long. */
342         if(len+10>=(int)sizeof(fnbuf))
343                 return 0;
344
345         for(ptr=fnbuf+len; ptr>fnbuf+12; --ptr)
346                 if(*ptr=='/')
347                 {
348                         char linkbuf[256];
349                         /* Replace the last component with "subsystem". */
350                         strcpy(ptr, "/subsystem");
351                         len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
352
353                         if(len!=-1)
354                         {
355                                 linkbuf[len] = 0;
356                                 /* Extract the last component of the subsystem symlink. */
357                                 for(; (len>0 && linkbuf[len-1]!='/'); --len) ;
358
359                                 if(verbosity>=2)
360                                 {
361                                         *ptr = 0;
362                                         printf("  Subsystem of %s is %s\n", fnbuf, linkbuf+len);
363                                 }
364
365                                 if(is_in_array(buses, linkbuf+len))
366                                         return 1;
367                         }
368                 }
369
370         return 0;
371 }
372
373 /**
374 Check if an array of properties describes a device that can be mounted.  An
375 array of explicitly allowed devices can be passed in as well.  Both arrays must
376 be terminated by a NULL entry.
377 */
378 int can_mount(Property *props, char **allowed)
379 {
380         static char *removable_buses[] = { "usb", "firewire", 0 };
381         char *devname;
382         char *devpath;
383         char *bus;
384
385         devname = get_property_value(props, "DEVNAME");
386         if(is_in_array(allowed, devname))
387                 return 1;
388
389         /* Special case for CD devices, since they are not partitions.  Only allow
390         mounting if media is inserted. */
391         if(match_property_value(props, "ID_TYPE", "cd") && match_property_value(props, "ID_CDROM_MEDIA", "1"))
392                 return 1;
393
394         /* Only allow mounting partitions. */
395         if(!match_property_value(props, "DEVTYPE", "partition"))
396                 return 0;
397
398         devpath = get_property_value(props, "DEVPATH");
399         if(is_removable(devpath))
400                 return 1;
401
402         /* Certain buses are removable by nature, but devices only advertise
403         themselves as removable if they support removable media, e.g. memory card
404         readers. */
405         bus = get_property_value(props, "ID_BUS");
406         if(is_in_array(removable_buses, bus))
407                 return 1;
408
409         return check_buses(devpath, removable_buses);
410 }
411
412 /**
413 Returns an array of all device nodes in a directory.  Symbolic links are
414 dereferenced.
415 */
416 char **get_device_nodes(char *dirname)
417 {
418         DIR *dir;
419         struct dirent *de;
420         char fnbuf[256];
421         char linkbuf[256];
422         struct stat st;
423         char **nodes = NULL;
424         int n_nodes = 0;
425         char **checked = NULL;
426         int n_checked = 0;
427         int i;
428
429         dir = opendir(dirname);
430         if(!dir)
431                 return NULL;
432
433         while((de = readdir(dir)))
434         {
435                 char *node;
436                 int duplicate = 0;
437
438                 /* Ignore . and .. entries. */
439                 if(de->d_name[0]=='.' && (de->d_name[1]==0 || (de->d_name[1]=='.' && de->d_name[2]==0)))
440                         continue;
441
442                 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", dirname, de->d_name);
443
444                 node = fnbuf;
445                 lstat(fnbuf, &st);
446                 if(S_ISLNK(st.st_mode))
447                 {
448                         int len;
449                         len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
450                         if(len!=-1)
451                         {
452                                 linkbuf[len] = 0;
453                                 node = linkbuf;
454                         }
455                 }
456
457                 /* There may be multiple symlinks to the same device.  Only include each
458                 device once in the returned array. */
459                 if(checked)
460                 {
461                         for(i=0; (!duplicate && i<n_checked); ++i)
462                                 if(strcmp(node, checked[i])==0)
463                                         duplicate = 1;
464                 }
465                 if(duplicate)
466                 {
467                         if(verbosity>=2)
468                                 printf("Device %s is a duplicate\n", fnbuf);
469                         continue;
470                 }
471
472                 checked = (char **)realloc(checked, (n_checked+1)*sizeof(char *));
473                 checked[n_checked] = strdup(node);
474                 ++n_checked;
475
476                 nodes = (char **)realloc(nodes, (n_nodes+2)*sizeof(char *));
477                 nodes[n_nodes] = strdup(fnbuf);
478                 ++n_nodes;
479         }
480
481         closedir(dir);
482         if(checked)
483         {
484                 for(i=0; i<n_checked; ++i)
485                         free(checked[i]);
486                 free(checked);
487         }
488
489         if(nodes)
490                 nodes[n_nodes] = NULL;
491
492         return nodes;
493 }
494
495 /** Returns an array of all mountable devices. */
496 Device *get_devices(void)
497 {
498         char **nodes = NULL;
499         Device *devices = NULL;
500         int n_devices = 0;
501         char **mounted = NULL;
502         char **fstab = NULL;
503         int i;
504
505         nodes = get_device_nodes("/dev/disk/by-id");
506         mounted = get_mounted_devices();
507         fstab = get_fstab_devices();
508
509         for(i=0; nodes[i]; ++i)
510         {
511                 Property *props;
512
513                 if(verbosity>=1)
514                         printf("Examining device %s\n", nodes[i]);
515
516                 props = get_device_properties(nodes[i]);
517                 if(!props)
518                 {
519                         if(verbosity>=2)
520                                 printf("  No properties\n");
521                         continue;
522                 }
523
524                 if(verbosity>=2)
525                 {
526                         int j;
527                         for(j=0; props[j].name; ++j)
528                                 printf("  %s = %s\n", props[j].name, props[j].value);
529                 }
530
531                 if(can_mount(props, fstab))
532                 {
533                         char *devname;
534                         char *label;
535                         char *vendor;
536                         char *model;
537                         char buf[256];
538                         char pos;
539                         struct stat st;
540
541                         if(verbosity>=1)
542                                 printf("  Using device\n");
543
544                         devname = get_property_value(props, "DEVNAME");
545
546                         /* Get a human-readable label for the device.  Use filesystem label,
547                         filesystem UUID or device node name in order of preference. */
548                         label = get_property_value(props, "ID_FS_LABEL");
549                         if(!label)
550                                 label = get_property_value(props, "ID_FS_UUID");
551                         if(!label)
552                         {
553                                 char *ptr;
554
555                                 label = devname;
556                                 for(ptr=label; *ptr; ++ptr)
557                                         if(*ptr=='/')
558                                                 label = ptr+1;
559                         }
560
561                         vendor = get_property_value(props, "ID_VENDOR");
562                         model = get_property_value(props, "ID_MODEL");
563
564                         pos = snprintf(buf, sizeof(buf), "%s", label);
565                         if(vendor && model)
566                                 pos += snprintf(buf+pos, sizeof(buf)-pos, " (%s %s)", vendor, model);
567
568                         stat(nodes[i], &st);
569
570                         /* Reserve space for a sentinel entry. */
571                         devices = (Device *)realloc(devices, (n_devices+2)*sizeof(Device));
572                         devices[n_devices].node = nodes[i];
573                         devices[n_devices].label = strdup(label);
574                         devices[n_devices].description = strdup(buf);
575                         devices[n_devices].mounted = is_in_array(mounted, devname);
576                         devices[n_devices].time = st.st_mtime;
577                         ++n_devices;
578                 }
579                 else
580                         free(nodes[i]);
581                 free_properties(props);
582         }
583
584         free(nodes);
585         free_string_array(mounted);
586
587         if(devices)
588         {
589                 /* Terminate the array with NULL pointers. */
590                 devices[n_devices].node = NULL;
591                 devices[n_devices].label = NULL;
592                 devices[n_devices].description = NULL;
593         }
594
595         return devices;
596 }
597
598 /**
599 Frees an array of devices and all strings contained in it.
600 */
601 void free_devices(Device *devices)
602 {
603         int i;
604         if(!devices)
605                 return;
606         for(i=0; devices[i].node; ++i)
607         {
608                 free(devices[i].node);
609                 free(devices[i].label);
610                 free(devices[i].description);
611         }
612         free(devices);
613 }
614
615 /**
616 Callback for activating a row in the device list.  Mounts or unmounts the
617 device depending on operating mode.
618 */
619 void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
620 {
621         GtkTreeModel *model;
622         GtkTreeIter iter;
623         int umount = *(int *)user_data;
624         char buf[1024];
625         int pos = 0;
626         int status = 0;
627         fd_set fds;
628         struct timeval timeout;
629         Device *device;
630         int pid;
631         int pipe_fd[2];
632
633         model = gtk_tree_view_get_model(list);
634
635         if(!gtk_tree_model_get_iter(model, &iter, path))
636                 return;
637
638         gtk_tree_model_get(model, &iter, 1, &device, -1);
639
640         pipe(pipe_fd);
641
642         pid = fork();
643         if(pid==0)
644         {
645                 /* Child process */
646                 if(verbosity>=1)
647                 {
648                         if(umount)
649                                 printf("Running pumount %s\n", device->node);
650                         else
651                                 printf("Running pmount %s %s\n", device->node, device->label);
652                 }
653
654                 close(pipe_fd[0]);
655                 dup2(pipe_fd[1], 1);
656                 dup2(pipe_fd[1], 2);
657
658                 if(umount)
659                         execl("/usr/bin/pumount", "pumount", device->node, NULL);
660                 else
661                         execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL);
662                 _exit(1);
663         }
664         else if(pid<0)
665                 return;
666
667         /* Parent process */
668
669         close(pipe_fd[1]);
670         FD_ZERO(&fds);
671         FD_SET(pipe_fd[0], &fds);
672         timeout.tv_sec = 0;
673         timeout.tv_usec = 200000;
674
675         while(1)
676         {
677                 /* The write fd for the pipe may be inherited by a fuse server
678                 process and stay open indefinitely. */
679                 if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout))
680                 {
681                         int len;
682
683                         len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1);
684                         if(len<=0)
685                                 break;
686                         pos += len;
687                 }
688                 else if(waitpid(pid, &status, 0))
689                 {
690                         pid = 0;
691                         break;
692                 }
693         }
694
695         if(pid)
696                 waitpid(pid, &status, 0);
697
698         buf[pos] = 0;
699
700         if(verbosity>=1)
701         {
702                 if(WIFEXITED(status))
703                 {
704                         if(WEXITSTATUS(status))
705                                 printf("Command exited successfully\n");
706                         else
707                                 printf("Command exited with status %d\n", WEXITSTATUS(status));
708                 }
709                 else if(WIFSIGNALED(status))
710                         printf("Command terminated with signal %d\n", WTERMSIG(status));
711                 else
712                         printf("Command exited with unknown result %04X\n", status);
713         }
714
715         if(!WIFEXITED(status) || WEXITSTATUS(status))
716         {
717                 GtkWidget *dialog;
718
719                 /* Pmount terminated with nonzero status or a signal.  Display an
720                 error to the user. */
721                 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf);
722                 g_signal_connect(dialog, "response", &gtk_main_quit, NULL);
723                 gtk_widget_show_all(dialog);
724                 return;
725         }
726
727         gtk_main_quit();
728
729         (void)column;
730 }
731
732 /**
733 Callback for the mount/unmount button.  Causes the selected row in the device
734 list to be activated.
735 */
736 void button_clicked(GtkButton *button, gpointer user_data)
737 {
738         GtkWidget *list = (GtkWidget *)user_data;
739         GtkTreeSelection *selection;
740         GtkTreeIter iter;
741         GtkTreeModel *model;
742         GtkTreePath *path;
743
744         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
745         gtk_tree_selection_get_selected(selection, &model, &iter);
746         path = gtk_tree_model_get_path(model, &iter);
747         gtk_tree_view_row_activated(GTK_TREE_VIEW(list), path, gtk_tree_view_get_column(GTK_TREE_VIEW(list), 0));
748         gtk_tree_path_free(path);
749
750         (void)button;
751 }
752
753 /**
754 Global key press callback for the window.
755 */
756 gboolean key_press(GtkWidget *widget, GdkEvent *event, gpointer user_data)
757 {
758         if(event->key.keyval==GDK_KEY_Escape)
759         {
760                 gtk_main_quit();
761                 return TRUE;
762         }
763
764         (void)widget;
765         (void)user_data;
766
767         return FALSE;
768 }
769
770 int main(int argc, char **argv)
771 {
772         GtkWidget *window;
773         GtkWidget *box;
774         GtkWidget *viewport;
775         GtkWidget *list;
776         GtkListStore *store;
777         GtkTreeSelection *selection;
778         GtkWidget *button;
779         GtkTreeIter iter;
780         Device *devices;
781         int i;
782         time_t latest;
783         int opt;
784         int umount = 0;
785         int n_listed;
786
787         gtk_init(&argc, &argv);
788
789         while((opt = getopt(argc, argv, "vu"))!=-1) switch(opt)
790         {
791         case 'v':
792                 ++verbosity;
793                 break;
794         case 'u':
795                 umount = 1;
796                 break;
797         }
798
799         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
800         gtk_container_set_border_width(GTK_CONTAINER(window), 5);
801         g_signal_connect(window, "destroy", G_CALLBACK(&gtk_main_quit), NULL);
802         g_signal_connect(window, "key-press-event", G_CALLBACK(&key_press), NULL);
803
804         box = gtk_vbox_new(FALSE, 5);
805         gtk_container_add(GTK_CONTAINER(window), box);
806
807         viewport = gtk_viewport_new(NULL, NULL);
808         gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
809         gtk_box_pack_start(GTK_BOX(box), viewport, TRUE, TRUE, 0);
810
811         list = gtk_tree_view_new();
812         gtk_container_add(GTK_CONTAINER(viewport), list);
813         g_signal_connect(list, "row-activated", G_CALLBACK(&row_activated), &umount);
814
815         store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
816         gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
817         gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),
818                 -1, "Device", gtk_cell_renderer_text_new(), "text", 0, NULL);
819
820         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
821
822         button = gtk_button_new_with_label(umount ? "Unmount" : "Mount");
823         g_signal_connect(button, "clicked", G_CALLBACK(&button_clicked), list);
824         gtk_box_pack_start(GTK_BOX(box), button, FALSE, TRUE, 0);
825
826         devices = get_devices();
827         n_listed = 0;
828         if(devices)
829         {
830                 /* Populate the list with devices in appropriate state. */
831                 latest = 0;
832                 for(i=0; devices[i].node; ++i)
833                         if(!devices[i].mounted==!umount)
834                         {
835                                 gtk_list_store_append(store, &iter);
836                                 gtk_list_store_set(store, &iter, 0, devices[i].description, 1, &devices[i], -1);
837                                 if(devices[i].time>latest)
838                                 {
839                                         /* Pre-select the device that appeared on the system most recently. */
840                                         latest = devices[i].time;
841                                         gtk_tree_selection_select_iter(selection, &iter);
842                                 }
843
844                                 ++n_listed;
845                         }
846
847         }
848
849         if(n_listed)
850                 gtk_widget_show_all(window);
851         else
852         {
853                 GtkWidget *dialog;
854
855                 /* Don't show the main window if no devices were listed. */
856                 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
857                         "No devices to %s", (umount ? "unmount" : "mount"));
858                 g_signal_connect(dialog, "response", G_CALLBACK(&gtk_main_quit), NULL);
859                 gtk_widget_show_all(dialog);
860         }
861
862         gtk_main();
863
864         free_devices(devices);
865
866         return 0;
867 }