1 /* Required for strdup, snprintf, getopt */
2 #define _XOPEN_SOURCE 500
12 #include <sys/select.h>
14 #include <gdk/gdkkeysyms.h>
16 typedef struct sProperty
22 typedef struct sDevice
33 char *post_mount_command = NULL;
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.
39 int parse_property(char *str, int size, Property *prop)
44 for(i=0; (equals<0 && i<size); ++i)
51 prop->name = malloc(equals+1);
52 strncpy(prop->name, str, equals);
53 prop->name[equals] = 0;
55 prop->value = malloc(size-equals);
56 strncpy(prop->value, str+equals+1, size-equals-1);
57 prop->value[size-equals-1] = 0;
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
67 Property *get_device_properties(char *node)
75 Property *props = NULL;
85 printf("Running udevadm info -q property -n \"%s\"\n", node);
90 execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL);
105 buf = (char *)malloc(bufsize);
117 len = read(pipe_fd[0], buf+pos, bufsize-pos);
126 for(i=0; (newline<0 && i<pos); ++i)
135 /* There was no newline in the buffer but there is more output to
136 be read. Try again with a larger buffer. */
138 buf = (char *)realloc(buf, bufsize);
142 if(parse_property(buf, newline, &prop)==0)
144 /* Reserve space for a sentinel value as well. */
145 props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
146 props[n_props] = prop;
149 memmove(buf, buf+newline+1, pos-newline-1);
160 /* Terminate the array with NULL pointers. */
161 props[n_props].name = NULL;
162 props[n_props].value = NULL;
165 waitpid(pid, NULL, 0);
172 Looks for a property in an array of properties and returns its value. Returns
173 NULL if the property was not found.
175 char *get_property_value(Property *props, char *name)
178 for(i=0; props[i].name; ++i)
179 if(strcmp(props[i].name, name)==0)
180 return props[i].value;
185 Checks if a property has a specific value. A NULL value is matched if the
186 property does not exist.
188 int match_property_value(Property *props, char *name, char *value)
190 char *v = get_property_value(props, name);
193 return strcmp(v, value)==0;
197 Frees an array of properties and all strings contained in it.
199 void free_properties(Property *props)
204 for(i=0; props[i].name; ++i)
207 free(props[i].value);
213 Returns an array of user-mountable devices listed in fstab.
215 char **get_fstab_devices(void)
219 char **devices = NULL;
222 file = setmntent("/etc/fstab", "r");
226 while((me = getmntent(file)))
227 if(hasmntopt(me, "user")!=NULL)
229 devices = (char **)realloc(devices, (n_devices+2)*sizeof(char *));
230 devices[n_devices] = strdup(me->mnt_fsname);
236 devices[n_devices] = NULL;
242 Checks if an array of strings contains the specified string.
244 int is_in_array(char **array, char *str)
249 for(i=0; array[i]; ++i)
250 if(!strcmp(str, array[i]))
256 Frees an array of strings.
258 void free_string_array(char **array)
263 for(i=0; array[i]; ++i)
269 Checks if a partition identified by a sysfs path is on a removable device.
271 int is_removable(char *devpath)
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))
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");
288 fd = open(fnbuf, O_RDONLY);
297 printf(" Removable\n");
301 printf(" Not removable\n");
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.
312 int check_buses(char *devpath, char **buses)
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))
323 for(ptr=fnbuf+len; ptr>fnbuf+12; --ptr)
327 /* Replace the last component with "subsystem". */
328 strcpy(ptr, "/subsystem");
329 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
334 /* Extract the last component of the subsystem symlink. */
335 for(; (len>0 && linkbuf[len-1]!='/'); --len) ;
340 printf(" Subsystem of %s is %s\n", fnbuf, linkbuf+len);
343 if(is_in_array(buses, linkbuf+len))
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.
356 int can_mount(Property *props, char **allowed)
358 static char *removable_buses[] = { "usb", "firewire", 0 };
363 devname = get_property_value(props, "DEVNAME");
364 if(is_in_array(allowed, devname))
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"))
372 /* Only allow mounting partitions. */
373 if(!match_property_value(props, "DEVTYPE", "partition"))
376 devpath = get_property_value(props, "DEVPATH");
377 if(is_removable(devpath))
380 /* Certain buses are removable by nature, but devices only advertise
381 themselves as removable if they support removable media, e.g. memory card
383 bus = get_property_value(props, "ID_BUS");
384 if(is_in_array(removable_buses, bus))
387 return check_buses(devpath, removable_buses);
391 Returns an array of all device nodes in a directory. Symbolic links are
394 char **get_device_nodes(char *dirname)
403 char **checked = NULL;
407 dir = opendir(dirname);
411 while((de = readdir(dir)))
416 /* Ignore . and .. entries. */
417 if(de->d_name[0]=='.' && (de->d_name[1]==0 || (de->d_name[1]=='.' && de->d_name[2]==0)))
420 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", dirname, de->d_name);
424 if(S_ISLNK(st.st_mode))
427 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
435 /* There may be multiple symlinks to the same device. Only include each
436 device once in the returned array. */
439 for(i=0; (!duplicate && i<n_checked); ++i)
440 if(strcmp(node, checked[i])==0)
446 printf("Device %s is a duplicate\n", fnbuf);
450 checked = (char **)realloc(checked, (n_checked+1)*sizeof(char *));
451 checked[n_checked] = strdup(node);
454 nodes = (char **)realloc(nodes, (n_nodes+2)*sizeof(char *));
455 nodes[n_nodes] = strdup(fnbuf);
462 for(i=0; i<n_checked; ++i)
468 nodes[n_nodes] = NULL;
474 Reads the list of mounted devices from /etc/mtab and records the mount points.
476 void check_mounts(Device *devices)
481 file = setmntent("/etc/mtab", "r");
485 while((me = getmntent(file)))
489 for(i=0; devices[i].node; ++i)
490 if(!strcmp(devices[i].devname, me->mnt_fsname))
492 devices[i].mount_point = strdup(me->mnt_dir);
495 printf("Device %s is mounted on %s\n", devices[i].node, devices[i].mount_point);
503 Returns an array of all mountable devices.
505 Device *get_devices(void)
508 Device *devices = NULL;
510 char **mounted = NULL;
514 nodes = get_device_nodes("/dev/disk/by-id");
515 fstab = get_fstab_devices();
517 for(i=0; nodes[i]; ++i)
522 printf("Examining device %s\n", nodes[i]);
524 props = get_device_properties(nodes[i]);
528 printf(" No properties\n");
535 for(j=0; props[j].name; ++j)
536 printf(" %s = %s\n", props[j].name, props[j].value);
539 if(can_mount(props, fstab))
550 printf(" Using device\n");
552 devname = get_property_value(props, "DEVNAME");
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");
558 label = get_property_value(props, "ID_FS_UUID");
564 for(ptr=label; *ptr; ++ptr)
569 vendor = get_property_value(props, "ID_VENDOR");
570 model = get_property_value(props, "ID_MODEL");
572 pos = snprintf(buf, sizeof(buf), "%s", label);
574 pos += snprintf(buf+pos, sizeof(buf)-pos, " (%s %s)", vendor, model);
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;
590 free_properties(props);
594 free_string_array(mounted);
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;
605 check_mounts(devices);
612 Frees an array of devices and all strings contained in it.
614 void free_devices(Device *devices)
619 for(i=0; devices[i].node; ++i)
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);
632 Mounts a device if it was not mounted, or unmounts if it was.
634 int toggle_device(Device *device, char *out_buf, int out_size)
636 int umount = !!device->mount_point;
637 char mount_point[1024];
642 struct timeval timeout;
648 len = snprintf(mount_point, sizeof(mount_point), "/media/%s", device->label);
649 if(len>=(int)sizeof(mount_point))
661 printf("Running pumount %s\n", device->node);
663 printf("Running pmount %s %s\n", device->node, mount_point+7);
671 execl("/usr/bin/pumount", "pumount", device->node, NULL);
673 execl("/usr/bin/pmount", "pmount", device->node, mount_point+7, NULL);
683 FD_SET(pipe_fd[0], &fds);
685 timeout.tv_usec = 200000;
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))
695 len = read(pipe_fd[0], out_buf+pos, out_size-pos-1);
700 else if(waitpid(pid, &status, 0))
709 waitpid(pid, &status, 0);
715 if(WIFEXITED(status))
717 if(WEXITSTATUS(status))
718 printf("Command exited successfully\n");
720 printf("Command exited with status %d\n", WEXITSTATUS(status));
722 else if(WIFSIGNALED(status))
723 printf("Command terminated with signal %d\n", WTERMSIG(status));
725 printf("Command exited with unknown result %04X\n", status);
728 if(!WIFEXITED(status) || WEXITSTATUS(status))
733 free(device->mount_point);
734 device->mount_point = NULL;
737 device->mount_point = strdup(mount_point);
743 Callback for activating a row in the device list. Mounts or unmounts the
744 device depending on operating mode.
746 void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
755 model = gtk_tree_view_get_model(list);
757 if(!gtk_tree_model_get_iter(model, &iter, path))
760 gtk_tree_model_get(model, &iter, 1, &device, -1);
761 ret = toggle_device(device, output, sizeof(output));
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", >k_main_quit, NULL);
770 gtk_widget_show_all(dialog);
776 if(post_mount_command && device->mount_point)
779 printf("Running %s in %s\n", post_mount_command, device->mount_point);
784 chdir(device->mount_point);
785 execlp(post_mount_command, post_mount_command, NULL);
795 Callback for the mount/unmount button. Causes the selected row in the device
796 list to be activated.
798 void button_clicked(GtkButton *button, gpointer user_data)
800 GtkWidget *list = (GtkWidget *)user_data;
801 GtkTreeSelection *selection;
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);
816 Global key press callback for the window.
818 gboolean key_press(GtkWidget *widget, GdkEvent *event, gpointer user_data)
820 if(event->key.keyval==GDK_KEY_Escape)
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"
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");
844 int main(int argc, char **argv)
851 GtkTreeSelection *selection;
861 gtk_init(&argc, &argv);
863 while((opt = getopt(argc, argv, "vur:h"))!=-1) switch(opt)
872 post_mount_command = optarg;
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(>k_main_quit), NULL);
882 g_signal_connect(window, "key-press-event", G_CALLBACK(&key_press), NULL);
884 box = gtk_vbox_new(FALSE, 5);
885 gtk_container_add(GTK_CONTAINER(window), box);
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);
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);
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);
900 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
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);
906 devices = get_devices();
910 /* Populate the list with devices in appropriate state. */
912 for(i=0; devices[i].node; ++i)
913 if(!devices[i].mount_point==!umount)
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)
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);
930 gtk_widget_show_all(window);
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(>k_main_quit), NULL);
939 gtk_widget_show_all(dialog);
944 free_devices(devices);