1 /* Required for strdup, snprintf, getopt */
2 #define _XOPEN_SOURCE 500
13 #include <sys/select.h>
15 #include <gdk/gdkkeysyms.h>
17 typedef struct sProperty
23 typedef struct sDevice
33 typedef struct sGuiContext
38 char *post_mount_command;
44 Parses a string of the form name=value and places the components in a Property
45 structure. Returns 0 on success, or -1 if the string wasn't a valid property.
47 int parse_property(char *str, int size, Property *prop)
52 for(i=0; (equals<0 && i<size); ++i)
59 prop->name = malloc(equals+1);
60 strncpy(prop->name, str, equals);
61 prop->name[equals] = 0;
63 prop->value = malloc(size-equals);
64 strncpy(prop->value, str+equals+1, size-equals-1);
65 prop->value[size-equals-1] = 0;
71 Retrieves all properties associated with a /dev node. The returned array is
72 terminated with an entry containing NULL values. Use free_properties to free
75 Property *get_device_properties(char *node)
83 Property *props = NULL;
93 printf("Running udevadm info -q property -n \"%s\"\n", node);
98 execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL);
113 buf = (char *)malloc(bufsize);
125 len = read(pipe_fd[0], buf+pos, bufsize-pos);
134 for(i=0; (newline<0 && i<pos); ++i)
143 /* There was no newline in the buffer but there is more output to
144 be read. Try again with a larger buffer. */
146 buf = (char *)realloc(buf, bufsize);
150 if(parse_property(buf, newline, &prop)==0)
152 /* Reserve space for a sentinel value as well. */
153 props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
154 props[n_props] = prop;
157 memmove(buf, buf+newline+1, pos-newline-1);
168 /* Terminate the array with NULL pointers. */
169 props[n_props].name = NULL;
170 props[n_props].value = NULL;
173 waitpid(pid, NULL, 0);
180 Looks for a property in an array of properties and returns its value. Returns
181 NULL if the property was not found.
183 char *get_property_value(Property *props, char *name)
186 for(i=0; props[i].name; ++i)
187 if(strcmp(props[i].name, name)==0)
188 return props[i].value;
193 Checks if a property has a specific value. A NULL value is matched if the
194 property does not exist.
196 int match_property_value(Property *props, char *name, char *value)
198 char *v = get_property_value(props, name);
201 return strcmp(v, value)==0;
205 Frees an array of properties and all strings contained in it.
207 void free_properties(Property *props)
212 for(i=0; props[i].name; ++i)
215 free(props[i].value);
221 Returns an array of user-mountable devices listed in fstab.
223 char **get_fstab_devices(void)
227 char **devices = NULL;
230 file = setmntent("/etc/fstab", "r");
234 while((me = getmntent(file)))
235 if(hasmntopt(me, "user")!=NULL)
237 devices = (char **)realloc(devices, (n_devices+2)*sizeof(char *));
238 devices[n_devices] = strdup(me->mnt_fsname);
244 devices[n_devices] = NULL;
250 Checks if an array of strings contains the specified string.
252 int is_in_array(char **array, char *str)
257 for(i=0; array[i]; ++i)
258 if(!strcmp(str, array[i]))
264 Frees an array of strings.
266 void free_string_array(char **array)
271 for(i=0; array[i]; ++i)
277 Checks if a partition identified by a sysfs path is on a removable device.
279 int is_removable(char *devpath)
286 len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
287 /* Default to not removable if the path was too long. */
288 if(len+10>=(int)sizeof(fnbuf))
291 /* We got a partition as a parameter, but the removable property is on the
292 disk. Replace the last component with "removable". */
293 for(ptr=fnbuf+len; (ptr>fnbuf && *ptr!='/'); --ptr) ;
294 strcpy(ptr, "/removable");
296 fd = open(fnbuf, O_RDONLY);
305 printf(" Removable\n");
309 printf(" Not removable\n");
316 Checks if a partition's disk or any of its parent devices are connected to any
317 of a set of buses. The device is identified by a sysfs path. The bus array
318 must be terminated with a NULL entry.
320 int check_buses(char *devpath, char **buses)
326 len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
327 /* Default to no match if the path was too long. */
328 if(len+10>=(int)sizeof(fnbuf))
331 for(ptr=fnbuf+len; ptr>fnbuf+12; --ptr)
335 /* Replace the last component with "subsystem". */
336 strcpy(ptr, "/subsystem");
337 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
342 /* Extract the last component of the subsystem symlink. */
343 for(; (len>0 && linkbuf[len-1]!='/'); --len) ;
348 printf(" Subsystem of %s is %s\n", fnbuf, linkbuf+len);
351 if(is_in_array(buses, linkbuf+len))
360 Check if an array of properties describes a device that can be mounted. An
361 array of explicitly allowed devices can be passed in as well. Both arrays must
362 be terminated by a NULL entry.
364 int can_mount(Property *props, char **allowed)
366 static char *removable_buses[] = { "usb", "firewire", 0 };
371 devname = get_property_value(props, "DEVNAME");
372 if(is_in_array(allowed, devname))
375 /* Special case for CD devices, since they are not partitions. Only allow
376 mounting if media is inserted. */
377 if(match_property_value(props, "ID_TYPE", "cd") && match_property_value(props, "ID_CDROM_MEDIA", "1"))
380 /* Only allow mounting partitions. */
381 if(!match_property_value(props, "DEVTYPE", "partition"))
384 devpath = get_property_value(props, "DEVPATH");
385 if(is_removable(devpath))
388 /* Certain buses are removable by nature, but devices only advertise
389 themselves as removable if they support removable media, e.g. memory card
391 bus = get_property_value(props, "ID_BUS");
392 if(is_in_array(removable_buses, bus))
395 return check_buses(devpath, removable_buses);
399 Returns an array of all device nodes in a directory. Symbolic links are
402 char **get_device_nodes(char *dirname)
411 char **checked = NULL;
415 dir = opendir(dirname);
419 while((de = readdir(dir)))
424 /* Ignore . and .. entries. */
425 if(de->d_name[0]=='.' && (de->d_name[1]==0 || (de->d_name[1]=='.' && de->d_name[2]==0)))
428 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", dirname, de->d_name);
432 if(S_ISLNK(st.st_mode))
435 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
443 /* There may be multiple symlinks to the same device. Only include each
444 device once in the returned array. */
447 for(i=0; (!duplicate && i<n_checked); ++i)
448 if(strcmp(node, checked[i])==0)
454 printf("Device %s is a duplicate\n", fnbuf);
458 checked = (char **)realloc(checked, (n_checked+1)*sizeof(char *));
459 checked[n_checked] = strdup(node);
462 nodes = (char **)realloc(nodes, (n_nodes+2)*sizeof(char *));
463 nodes[n_nodes] = strdup(fnbuf);
470 for(i=0; i<n_checked; ++i)
476 nodes[n_nodes] = NULL;
482 Reads the list of mounted devices from /etc/mtab and records the mount points.
484 void check_mounts(Device *devices)
489 file = setmntent("/etc/mtab", "r");
493 while((me = getmntent(file)))
497 for(i=0; devices[i].node; ++i)
498 if(!strcmp(devices[i].devname, me->mnt_fsname))
500 devices[i].mount_point = strdup(me->mnt_dir);
503 printf("Device %s is mounted on %s\n", devices[i].node, devices[i].mount_point);
511 Returns an array of all mountable devices.
513 Device *get_devices(void)
516 Device *devices = NULL;
518 char **mounted = NULL;
522 nodes = get_device_nodes("/dev/disk/by-id");
523 fstab = get_fstab_devices();
525 for(i=0; nodes[i]; ++i)
530 printf("Examining device %s\n", nodes[i]);
532 props = get_device_properties(nodes[i]);
536 printf(" No properties\n");
543 for(j=0; props[j].name; ++j)
544 printf(" %s = %s\n", props[j].name, props[j].value);
547 if(can_mount(props, fstab))
558 printf(" Using device\n");
560 devname = get_property_value(props, "DEVNAME");
562 /* Get a human-readable label for the device. Use filesystem label,
563 filesystem UUID or device node name in order of preference. */
564 label = get_property_value(props, "ID_FS_LABEL");
566 label = get_property_value(props, "ID_FS_UUID");
572 for(ptr=label; *ptr; ++ptr)
577 vendor = get_property_value(props, "ID_VENDOR");
578 model = get_property_value(props, "ID_MODEL");
580 pos = snprintf(buf, sizeof(buf), "%s", label);
582 pos += snprintf(buf+pos, sizeof(buf)-pos, " (%s %s)", vendor, model);
586 /* Reserve space for a sentinel entry. */
587 devices = (Device *)realloc(devices, (n_devices+2)*sizeof(Device));
588 devices[n_devices].node = nodes[i];
589 devices[n_devices].devname = strdup(devname);
590 devices[n_devices].label = strdup(label);
591 devices[n_devices].description = strdup(buf);
592 devices[n_devices].mount_point = NULL;
593 devices[n_devices].time = st.st_mtime;
598 free_properties(props);
602 free_string_array(mounted);
606 /* Terminate the array with NULL pointers. */
607 devices[n_devices].node = NULL;
608 devices[n_devices].devname = NULL;
609 devices[n_devices].label = NULL;
610 devices[n_devices].description = NULL;
611 devices[n_devices].mount_point = NULL;
613 check_mounts(devices);
620 Frees an array of devices and all strings contained in it.
622 void free_devices(Device *devices)
627 for(i=0; devices[i].node; ++i)
629 free(devices[i].node);
630 free(devices[i].devname);
631 free(devices[i].label);
632 free(devices[i].description);
633 if(devices[i].mount_point)
634 free(devices[i].mount_point);
640 Mounts a device if it was not mounted, or unmounts if it was.
642 int toggle_device(Device *device, char *out_buf, int out_size)
644 int umount = !!device->mount_point;
645 char mount_point[1024];
649 struct timeval timeout;
656 /* Find a mount point that does not exist yet. */
661 len = snprintf(mount_point, sizeof(mount_point), "/media/%s", device->label);
662 if(len+2>=(int)sizeof(mount_point))
666 len += snprintf(mount_point+len, sizeof(mount_point)-len, "_%c", suffix);
668 if(access(mount_point, F_OK)<0 && errno==ENOENT)
688 printf("Running pumount %s\n", device->node);
690 printf("Running pmount %s %s\n", device->node, mount_point+7);
698 execl("/usr/bin/pumount", "pumount", device->node, NULL);
700 execl("/usr/bin/pmount", "pmount", device->node, mount_point+7, NULL);
710 FD_SET(pipe_fd[0], &fds);
712 timeout.tv_usec = 200000;
716 /* The write fd for the pipe may be inherited by a fuse server
717 process and stay open indefinitely. */
718 if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout))
722 len = read(pipe_fd[0], out_buf+pos, out_size-pos-1);
727 else if(waitpid(pid, &status, 0))
736 waitpid(pid, &status, 0);
742 if(WIFEXITED(status))
744 if(WEXITSTATUS(status))
745 printf("Command exited successfully\n");
747 printf("Command exited with status %d\n", WEXITSTATUS(status));
749 else if(WIFSIGNALED(status))
750 printf("Command terminated with signal %d\n", WTERMSIG(status));
752 printf("Command exited with unknown result %04X\n", status);
755 if(!WIFEXITED(status) || WEXITSTATUS(status))
760 free(device->mount_point);
761 device->mount_point = NULL;
764 device->mount_point = strdup(mount_point);
770 Callback for selection in the device list changing. Updates the action button
771 label according to device status.
773 void selection_changed(GtkTreeSelection *selection, gpointer user_data)
775 GuiContext *context = (GuiContext *)user_data;
780 if(!gtk_tree_selection_get_selected(selection, &model, &iter))
783 gtk_tree_model_get(model, &iter, 1, &device, -1);
784 gtk_button_set_label(GTK_BUTTON(context->button), (device->mount_point ? "Unmount" : "Mount"));
788 Callback for activating a row in the device list. Mounts or unmounts the
789 device depending on operating mode.
791 void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
793 GuiContext *context = (GuiContext *)user_data;
801 model = gtk_tree_view_get_model(list);
803 if(!gtk_tree_model_get_iter(model, &iter, path))
806 gtk_tree_model_get(model, &iter, 1, &device, -1);
807 ret = toggle_device(device, output, sizeof(output));
812 /* Pmount terminated with nonzero status or a signal. Display an
813 error to the user. */
814 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", output);
816 g_signal_connect(dialog, "response", G_CALLBACK(>k_widget_destroy), dialog);
818 g_signal_connect(dialog, "response", G_CALLBACK(>k_main_quit), NULL);
819 gtk_widget_show_all(dialog);
825 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
826 2, !!device->mount_point,
827 3, device->mount_point,
830 gtk_button_set_label(GTK_BUTTON(context->button), (device->mount_point ? "Unmount" : "Mount"));
835 if(context->post_mount_command && device->mount_point)
838 printf("Running %s in %s\n", context->post_mount_command, device->mount_point);
843 chdir(device->mount_point);
844 execlp(context->post_mount_command, context->post_mount_command, NULL);
854 Callback for the mount/unmount button. Causes the selected row in the device
855 list to be activated.
857 void button_clicked(GtkButton *button, gpointer user_data)
859 GuiContext *context = (GuiContext *)user_data;
860 GtkTreeSelection *selection;
864 GtkTreeViewColumn *column;
866 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(context->list));
867 gtk_tree_selection_get_selected(selection, &model, &iter);
868 path = gtk_tree_model_get_path(model, &iter);
869 column = gtk_tree_view_get_column(GTK_TREE_VIEW(context->list), 0);
870 gtk_tree_view_row_activated(GTK_TREE_VIEW(context->list), path, column);
871 gtk_tree_path_free(path);
877 Global key press callback for the window.
879 gboolean key_press(GtkWidget *widget, GdkEvent *event, gpointer user_data)
881 if(event->key.keyval==GDK_KEY_Escape)
895 printf("pmount-gui\n"
896 "Copyright (c) 2011-2015 Mikko Rasa, Mikkosoft Productions\n\n"
897 "Usage: pmount-gui [-v] [-u] [-r <command>] [-h]\n\n"
899 " -v Increase verbosity\n"
900 " -u Unmount a device (default is mount)\n"
901 " -r Run a command after mounting\n"
902 " -m Start a persistent mount manager\n"
903 " -h Display this help\n");
906 int main(int argc, char **argv)
913 GtkTreeSelection *selection;
914 GtkCellRenderer *mounted_toggle;
924 context.post_mount_command = NULL;
926 gtk_init(&argc, &argv);
928 while((opt = getopt(argc, argv, "vur:mh"))!=-1) switch(opt)
937 context.post_mount_command = optarg;
947 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
948 gtk_container_set_border_width(GTK_CONTAINER(window), 5);
949 g_signal_connect(window, "destroy", G_CALLBACK(>k_main_quit), NULL);
950 g_signal_connect(window, "key-press-event", G_CALLBACK(&key_press), NULL);
952 box = gtk_vbox_new(FALSE, 5);
953 gtk_container_add(GTK_CONTAINER(window), box);
955 viewport = gtk_viewport_new(NULL, NULL);
956 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
957 gtk_box_pack_start(GTK_BOX(box), viewport, TRUE, TRUE, 0);
959 context.list = gtk_tree_view_new();
960 gtk_container_add(GTK_CONTAINER(viewport), context.list);
961 g_signal_connect(context.list, "row-activated", G_CALLBACK(&row_activated), &context);
963 store = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_BOOLEAN, G_TYPE_STRING);
964 gtk_tree_view_set_model(GTK_TREE_VIEW(context.list), GTK_TREE_MODEL(store));
965 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(context.list),
966 -1, "Device", gtk_cell_renderer_text_new(), "text", 0, NULL);
970 GtkTreeViewColumn *mounted_column;
971 GtkCellRenderer *mount_point_renderer;
973 mounted_column = gtk_tree_view_column_new();
974 gtk_tree_view_column_set_title(mounted_column, "Mounted");
976 mounted_toggle = gtk_cell_renderer_toggle_new();
977 gtk_tree_view_column_pack_start(mounted_column, mounted_toggle, FALSE);
978 gtk_tree_view_column_add_attribute(mounted_column, mounted_toggle, "active", 2);
980 mount_point_renderer = gtk_cell_renderer_text_new();
981 gtk_tree_view_column_pack_start(mounted_column, mount_point_renderer, TRUE);
982 gtk_tree_view_column_add_attribute(mounted_column, mount_point_renderer, "text", 3);
984 gtk_tree_view_insert_column(GTK_TREE_VIEW(context.list), mounted_column, -1);
987 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(context.list));
988 g_signal_connect(selection, "changed", G_CALLBACK(&selection_changed), &context);
990 context.button = gtk_button_new_with_label(umount ? "Unmount" : "Mount");
991 g_signal_connect(context.button, "clicked", G_CALLBACK(&button_clicked), &context);
992 gtk_box_pack_start(GTK_BOX(box), context.button, FALSE, TRUE, 0);
994 devices = get_devices();
998 /* Populate the list with devices in appropriate state. */
1000 for(i=0; devices[i].node; ++i)
1001 if(!devices[i].mount_point==!umount || context.manager)
1003 gtk_list_store_append(store, &iter);
1004 gtk_list_store_set(store, &iter,
1005 0, devices[i].description,
1007 2, !!devices[i].mount_point,
1008 3, devices[i].mount_point,
1010 if(devices[i].time>latest)
1012 /* Pre-select the device that appeared on the system most recently. */
1013 latest = devices[i].time;
1014 gtk_tree_selection_select_iter(selection, &iter);
1022 if(n_listed || context.manager)
1023 gtk_widget_show_all(window);
1028 /* Don't show the main window if no devices were listed. */
1029 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
1030 "No devices to %s", (umount ? "unmount" : "mount"));
1031 g_signal_connect(dialog, "response", G_CALLBACK(>k_main_quit), NULL);
1032 gtk_widget_show_all(dialog);
1037 free_devices(devices);