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