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
39 char *post_mount_command;
45 Parses a string of the form name=value and places the components in a Property
46 structure. Returns 0 on success, or -1 if the string wasn't a valid property.
48 int parse_property(char *str, int size, Property *prop)
53 for(i=0; (equals<0 && i<size); ++i)
60 prop->name = malloc(equals+1);
61 strncpy(prop->name, str, equals);
62 prop->name[equals] = 0;
64 prop->value = malloc(size-equals);
65 strncpy(prop->value, str+equals+1, size-equals-1);
66 prop->value[size-equals-1] = 0;
72 Retrieves all properties associated with a /dev node. The returned array is
73 terminated with an entry containing NULL values. Use free_properties to free
76 Property *get_device_properties(char *node)
84 Property *props = NULL;
94 printf("Running udevadm info -q property -n \"%s\"\n", node);
99 execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL);
114 buf = (char *)malloc(bufsize);
126 len = read(pipe_fd[0], buf+pos, bufsize-pos);
135 for(i=0; (newline<0 && i<pos); ++i)
144 /* There was no newline in the buffer but there is more output to
145 be read. Try again with a larger buffer. */
147 buf = (char *)realloc(buf, bufsize);
151 if(parse_property(buf, newline, &prop)==0)
153 /* Reserve space for a sentinel value as well. */
154 props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
155 props[n_props] = prop;
158 memmove(buf, buf+newline+1, pos-newline-1);
169 /* Terminate the array with NULL pointers. */
170 props[n_props].name = NULL;
171 props[n_props].value = NULL;
174 waitpid(pid, NULL, 0);
181 Looks for a property in an array of properties and returns its value. Returns
182 NULL if the property was not found.
184 char *get_property_value(Property *props, char *name)
187 for(i=0; props[i].name; ++i)
188 if(strcmp(props[i].name, name)==0)
189 return props[i].value;
194 Checks if a property has a specific value. A NULL value is matched if the
195 property does not exist.
197 int match_property_value(Property *props, char *name, char *value)
199 char *v = get_property_value(props, name);
202 return strcmp(v, value)==0;
206 Frees an array of properties and all strings contained in it.
208 void free_properties(Property *props)
213 for(i=0; props[i].name; ++i)
216 free(props[i].value);
222 Returns an array of user-mountable devices listed in fstab.
224 char **get_fstab_devices(void)
228 char **devices = NULL;
231 file = setmntent("/etc/fstab", "r");
235 while((me = getmntent(file)))
236 if(hasmntopt(me, "user")!=NULL)
238 devices = (char **)realloc(devices, (n_devices+2)*sizeof(char *));
239 devices[n_devices] = strdup(me->mnt_fsname);
245 devices[n_devices] = NULL;
251 Checks if an array of strings contains the specified string.
253 int is_in_array(char **array, char *str)
258 for(i=0; array[i]; ++i)
259 if(!strcmp(str, array[i]))
265 Frees an array of strings.
267 void free_string_array(char **array)
272 for(i=0; array[i]; ++i)
278 Checks if a partition identified by a sysfs path is on a removable device.
280 int is_removable(char *devpath)
287 len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
288 /* Default to not removable if the path was too long. */
289 if(len+10>=(int)sizeof(fnbuf))
292 /* We got a partition as a parameter, but the removable property is on the
293 disk. Replace the last component with "removable". */
294 for(ptr=fnbuf+len; (ptr>fnbuf && *ptr!='/'); --ptr) ;
295 strcpy(ptr, "/removable");
297 fd = open(fnbuf, O_RDONLY);
306 printf(" Removable\n");
310 printf(" Not removable\n");
317 Checks if a partition's disk or any of its parent devices are connected to any
318 of a set of buses. The device is identified by a sysfs path. The bus array
319 must be terminated with a NULL entry.
321 int check_buses(char *devpath, char **buses)
327 len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
328 /* Default to no match if the path was too long. */
329 if(len+10>=(int)sizeof(fnbuf))
332 for(ptr=fnbuf+len; ptr>fnbuf+12; --ptr)
336 /* Replace the last component with "subsystem". */
337 strcpy(ptr, "/subsystem");
338 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
343 /* Extract the last component of the subsystem symlink. */
344 for(; (len>0 && linkbuf[len-1]!='/'); --len) ;
349 printf(" Subsystem of %s is %s\n", fnbuf, linkbuf+len);
352 if(is_in_array(buses, linkbuf+len))
361 Check if an array of properties describes a device that can be mounted. An
362 array of explicitly allowed devices can be passed in as well. Both arrays must
363 be terminated by a NULL entry.
365 int can_mount(Property *props, char **allowed)
367 static char *removable_buses[] = { "usb", "firewire", 0 };
372 devname = get_property_value(props, "DEVNAME");
373 if(is_in_array(allowed, devname))
376 /* Special case for CD devices, since they are not partitions. Only allow
377 mounting if media is inserted. */
378 if(match_property_value(props, "ID_TYPE", "cd") && match_property_value(props, "ID_CDROM_MEDIA", "1"))
381 /* Only allow mounting partitions. */
382 if(!match_property_value(props, "DEVTYPE", "partition"))
385 devpath = get_property_value(props, "DEVPATH");
386 if(is_removable(devpath))
389 /* Certain buses are removable by nature, but devices only advertise
390 themselves as removable if they support removable media, e.g. memory card
392 bus = get_property_value(props, "ID_BUS");
393 if(is_in_array(removable_buses, bus))
396 return check_buses(devpath, removable_buses);
400 Returns an array of all device nodes in a directory. Symbolic links are
403 char **get_device_nodes(char *dirname)
412 char **checked = NULL;
416 dir = opendir(dirname);
420 while((de = readdir(dir)))
425 /* Ignore . and .. entries. */
426 if(de->d_name[0]=='.' && (de->d_name[1]==0 || (de->d_name[1]=='.' && de->d_name[2]==0)))
429 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", dirname, de->d_name);
433 if(S_ISLNK(st.st_mode))
436 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
444 /* There may be multiple symlinks to the same device. Only include each
445 device once in the returned array. */
448 for(i=0; (!duplicate && i<n_checked); ++i)
449 if(strcmp(node, checked[i])==0)
455 printf("Device %s is a duplicate\n", fnbuf);
459 checked = (char **)realloc(checked, (n_checked+1)*sizeof(char *));
460 checked[n_checked] = strdup(node);
463 nodes = (char **)realloc(nodes, (n_nodes+2)*sizeof(char *));
464 nodes[n_nodes] = strdup(fnbuf);
471 for(i=0; i<n_checked; ++i)
477 nodes[n_nodes] = NULL;
483 Reads the list of mounted devices from /etc/mtab and records the mount points.
485 void check_mounts(Device *devices)
490 file = setmntent("/etc/mtab", "r");
494 while((me = getmntent(file)))
498 for(i=0; devices[i].node; ++i)
499 if(!strcmp(devices[i].devname, me->mnt_fsname))
501 devices[i].mount_point = strdup(me->mnt_dir);
504 printf("Device %s is mounted on %s\n", devices[i].node, devices[i].mount_point);
512 Returns an array of all mountable devices.
514 Device *get_devices(void)
517 Device *devices = NULL;
519 char **mounted = NULL;
523 nodes = get_device_nodes("/dev/disk/by-id");
524 fstab = get_fstab_devices();
526 for(i=0; nodes[i]; ++i)
531 printf("Examining device %s\n", nodes[i]);
533 props = get_device_properties(nodes[i]);
537 printf(" No properties\n");
544 for(j=0; props[j].name; ++j)
545 printf(" %s = %s\n", props[j].name, props[j].value);
548 if(can_mount(props, fstab))
559 printf(" Using device\n");
561 devname = get_property_value(props, "DEVNAME");
563 /* Get a human-readable label for the device. Use filesystem label,
564 filesystem UUID or device node name in order of preference. */
565 label = get_property_value(props, "ID_FS_LABEL");
567 label = get_property_value(props, "ID_FS_UUID");
573 for(ptr=label; *ptr; ++ptr)
578 vendor = get_property_value(props, "ID_VENDOR");
579 model = get_property_value(props, "ID_MODEL");
581 pos = snprintf(buf, sizeof(buf), "%s", label);
583 pos += snprintf(buf+pos, sizeof(buf)-pos, " (%s %s)", vendor, model);
587 /* Reserve space for a sentinel entry. */
588 devices = (Device *)realloc(devices, (n_devices+2)*sizeof(Device));
589 devices[n_devices].node = nodes[i];
590 devices[n_devices].devname = strdup(devname);
591 devices[n_devices].label = strdup(label);
592 devices[n_devices].description = strdup(buf);
593 devices[n_devices].mount_point = NULL;
594 devices[n_devices].time = st.st_mtime;
599 free_properties(props);
603 free_string_array(mounted);
607 /* Terminate the array with NULL pointers. */
608 devices[n_devices].node = NULL;
609 devices[n_devices].devname = NULL;
610 devices[n_devices].label = NULL;
611 devices[n_devices].description = NULL;
612 devices[n_devices].mount_point = NULL;
614 check_mounts(devices);
621 Frees an array of devices and all strings contained in it.
623 void free_devices(Device *devices)
628 for(i=0; devices[i].node; ++i)
630 free(devices[i].node);
631 free(devices[i].devname);
632 free(devices[i].label);
633 free(devices[i].description);
634 if(devices[i].mount_point)
635 free(devices[i].mount_point);
641 Mounts a device if it was not mounted, or unmounts if it was.
643 int toggle_device(Device *device, char *out_buf, int out_size)
645 int umount = !!device->mount_point;
646 char mount_point[1024];
650 struct timeval timeout;
657 /* Find a mount point that does not exist yet. */
662 len = snprintf(mount_point, sizeof(mount_point), "/media/%s", device->label);
663 if(len+2>=(int)sizeof(mount_point))
667 len += snprintf(mount_point+len, sizeof(mount_point)-len, "_%c", suffix);
669 if(access(mount_point, F_OK)<0 && errno==ENOENT)
689 printf("Running pumount %s\n", device->node);
691 printf("Running pmount %s %s\n", device->node, mount_point+7);
699 execl("/usr/bin/pumount", "pumount", device->node, NULL);
701 execl("/usr/bin/pmount", "pmount", device->node, mount_point+7, NULL);
711 FD_SET(pipe_fd[0], &fds);
713 timeout.tv_usec = 200000;
717 /* The write fd for the pipe may be inherited by a fuse server
718 process and stay open indefinitely. */
719 if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout))
723 len = read(pipe_fd[0], out_buf+pos, out_size-pos-1);
728 else if(waitpid(pid, &status, 0))
737 waitpid(pid, &status, 0);
743 if(WIFEXITED(status))
745 if(WEXITSTATUS(status))
746 printf("Command exited successfully\n");
748 printf("Command exited with status %d\n", WEXITSTATUS(status));
750 else if(WIFSIGNALED(status))
751 printf("Command terminated with signal %d\n", WTERMSIG(status));
753 printf("Command exited with unknown result %04X\n", status);
756 if(!WIFEXITED(status) || WEXITSTATUS(status))
761 free(device->mount_point);
762 device->mount_point = NULL;
765 device->mount_point = strdup(mount_point);
771 Refreshes both the internal devices array and the GUI list.
773 int refresh_devices(GuiContext *context, int umount)
776 GtkTreeSelection *selection;
782 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(context->list)));
783 gtk_list_store_clear(store);
785 free_devices(context->devices);
787 context->devices = get_devices();
788 if(!context->devices)
791 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(context->list));
793 /* Populate the list with devices in appropriate state. */
796 for(i=0; context->devices[i].node; ++i)
800 dev = &context->devices[i];
801 if(umount<0 || !dev->mount_point==!umount)
803 gtk_list_store_append(store, &iter);
804 gtk_list_store_set(store, &iter,
807 2, !!dev->mount_point,
812 /* Pre-select the device that appeared on the system most recently. */
814 gtk_tree_selection_select_iter(selection, &iter);
825 Callback for selection in the device list changing. Updates the action button
826 label according to device status.
828 void selection_changed(GtkTreeSelection *selection, gpointer user_data)
830 GuiContext *context = (GuiContext *)user_data;
835 if(!gtk_tree_selection_get_selected(selection, &model, &iter))
838 gtk_tree_model_get(model, &iter, 1, &device, -1);
839 gtk_button_set_label(GTK_BUTTON(context->button), (device->mount_point ? "Unmount" : "Mount"));
843 Callback for activating a row in the device list. Mounts or unmounts the
844 device depending on operating mode.
846 void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
848 GuiContext *context = (GuiContext *)user_data;
856 model = gtk_tree_view_get_model(list);
858 if(!gtk_tree_model_get_iter(model, &iter, path))
861 gtk_tree_model_get(model, &iter, 1, &device, -1);
862 ret = toggle_device(device, output, sizeof(output));
867 /* Pmount terminated with nonzero status or a signal. Display an
868 error to the user. */
869 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", output);
871 g_signal_connect(dialog, "response", G_CALLBACK(>k_widget_destroy), dialog);
873 g_signal_connect(dialog, "response", G_CALLBACK(>k_main_quit), NULL);
874 gtk_widget_show_all(dialog);
880 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
881 2, !!device->mount_point,
882 3, device->mount_point,
885 gtk_button_set_label(GTK_BUTTON(context->button), (device->mount_point ? "Unmount" : "Mount"));
890 if(context->post_mount_command && device->mount_point)
893 printf("Running %s in %s\n", context->post_mount_command, device->mount_point);
898 chdir(device->mount_point);
899 execlp(context->post_mount_command, context->post_mount_command, NULL);
909 Callback for the mount/unmount button. Causes the selected row in the device
910 list to be activated.
912 void button_clicked(GtkButton *button, gpointer user_data)
914 GuiContext *context = (GuiContext *)user_data;
915 GtkTreeSelection *selection;
919 GtkTreeViewColumn *column;
921 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(context->list));
922 gtk_tree_selection_get_selected(selection, &model, &iter);
923 path = gtk_tree_model_get_path(model, &iter);
924 column = gtk_tree_view_get_column(GTK_TREE_VIEW(context->list), 0);
925 gtk_tree_view_row_activated(GTK_TREE_VIEW(context->list), path, column);
926 gtk_tree_path_free(path);
932 Global key press callback for the window.
934 gboolean key_press(GtkWidget *widget, GdkEvent *event, gpointer user_data)
936 if(event->key.keyval==GDK_KEY_Escape)
950 printf("pmount-gui\n"
951 "Copyright (c) 2011-2015 Mikko Rasa, Mikkosoft Productions\n\n"
952 "Usage: pmount-gui [-v] [-u] [-r <command>] [-h]\n\n"
954 " -v Increase verbosity\n"
955 " -u Unmount a device (default is mount)\n"
956 " -r Run a command after mounting\n"
957 " -m Start a persistent mount manager\n"
958 " -h Display this help\n");
961 int main(int argc, char **argv)
968 GtkTreeSelection *selection;
969 GtkCellRenderer *mounted_toggle;
975 context.devices = NULL;
976 context.post_mount_command = NULL;
978 gtk_init(&argc, &argv);
980 while((opt = getopt(argc, argv, "vur:mh"))!=-1) switch(opt)
989 context.post_mount_command = optarg;
999 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1000 gtk_container_set_border_width(GTK_CONTAINER(window), 5);
1001 g_signal_connect(window, "destroy", G_CALLBACK(>k_main_quit), NULL);
1002 g_signal_connect(window, "key-press-event", G_CALLBACK(&key_press), NULL);
1004 box = gtk_vbox_new(FALSE, 5);
1005 gtk_container_add(GTK_CONTAINER(window), box);
1007 viewport = gtk_viewport_new(NULL, NULL);
1008 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
1009 gtk_box_pack_start(GTK_BOX(box), viewport, TRUE, TRUE, 0);
1011 context.list = gtk_tree_view_new();
1012 gtk_container_add(GTK_CONTAINER(viewport), context.list);
1013 g_signal_connect(context.list, "row-activated", G_CALLBACK(&row_activated), &context);
1015 store = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_BOOLEAN, G_TYPE_STRING);
1016 gtk_tree_view_set_model(GTK_TREE_VIEW(context.list), GTK_TREE_MODEL(store));
1017 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(context.list),
1018 -1, "Device", gtk_cell_renderer_text_new(), "text", 0, NULL);
1022 GtkTreeViewColumn *mounted_column;
1023 GtkCellRenderer *mount_point_renderer;
1025 mounted_column = gtk_tree_view_column_new();
1026 gtk_tree_view_column_set_title(mounted_column, "Mounted");
1028 mounted_toggle = gtk_cell_renderer_toggle_new();
1029 gtk_tree_view_column_pack_start(mounted_column, mounted_toggle, FALSE);
1030 gtk_tree_view_column_add_attribute(mounted_column, mounted_toggle, "active", 2);
1032 mount_point_renderer = gtk_cell_renderer_text_new();
1033 gtk_tree_view_column_pack_start(mounted_column, mount_point_renderer, TRUE);
1034 gtk_tree_view_column_add_attribute(mounted_column, mount_point_renderer, "text", 3);
1036 gtk_tree_view_insert_column(GTK_TREE_VIEW(context.list), mounted_column, -1);
1039 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(context.list));
1040 g_signal_connect(selection, "changed", G_CALLBACK(&selection_changed), &context);
1042 context.button = gtk_button_new_with_label(umount ? "Unmount" : "Mount");
1043 g_signal_connect(context.button, "clicked", G_CALLBACK(&button_clicked), &context);
1044 gtk_box_pack_start(GTK_BOX(box), context.button, FALSE, TRUE, 0);
1046 n_listed = refresh_devices(&context, umount);
1048 if(n_listed || context.manager)
1049 gtk_widget_show_all(window);
1054 /* Don't show the main window if no devices were listed. */
1055 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
1056 "No devices to %s", (umount ? "unmount" : "mount"));
1057 g_signal_connect(dialog, "response", G_CALLBACK(>k_main_quit), NULL);
1058 gtk_widget_show_all(dialog);
1063 free_devices(context.devices);