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
34 char *post_mount_command = NULL;
37 Parses a string of the form name=value and places the components in a Property
38 structure. Returns 0 on success, or -1 if the string wasn't a valid property.
40 int parse_property(char *str, int size, Property *prop)
45 for(i=0; (equals<0 && i<size); ++i)
52 prop->name = malloc(equals+1);
53 strncpy(prop->name, str, equals);
54 prop->name[equals] = 0;
56 prop->value = malloc(size-equals);
57 strncpy(prop->value, str+equals+1, size-equals-1);
58 prop->value[size-equals-1] = 0;
64 Retrieves all properties associated with a /dev node. The returned array is
65 terminated with an entry containing NULL values. Use free_properties to free
68 Property *get_device_properties(char *node)
76 Property *props = NULL;
86 printf("Running udevadm info -q property -n \"%s\"\n", node);
91 execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL);
106 buf = (char *)malloc(bufsize);
118 len = read(pipe_fd[0], buf+pos, bufsize-pos);
127 for(i=0; (newline<0 && i<pos); ++i)
136 /* There was no newline in the buffer but there is more output to
137 be read. Try again with a larger buffer. */
139 buf = (char *)realloc(buf, bufsize);
143 if(parse_property(buf, newline, &prop)==0)
145 /* Reserve space for a sentinel value as well. */
146 props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
147 props[n_props] = prop;
150 memmove(buf, buf+newline+1, pos-newline-1);
161 /* Terminate the array with NULL pointers. */
162 props[n_props].name = NULL;
163 props[n_props].value = NULL;
166 waitpid(pid, NULL, 0);
173 Looks for a property in an array of properties and returns its value. Returns
174 NULL if the property was not found.
176 char *get_property_value(Property *props, char *name)
179 for(i=0; props[i].name; ++i)
180 if(strcmp(props[i].name, name)==0)
181 return props[i].value;
186 Checks if a property has a specific value. A NULL value is matched if the
187 property does not exist.
189 int match_property_value(Property *props, char *name, char *value)
191 char *v = get_property_value(props, name);
194 return strcmp(v, value)==0;
198 Frees an array of properties and all strings contained in it.
200 void free_properties(Property *props)
205 for(i=0; props[i].name; ++i)
208 free(props[i].value);
214 Returns an array of user-mountable devices listed in fstab.
216 char **get_fstab_devices(void)
220 char **devices = NULL;
223 file = setmntent("/etc/fstab", "r");
227 while((me = getmntent(file)))
228 if(hasmntopt(me, "user")!=NULL)
230 devices = (char **)realloc(devices, (n_devices+2)*sizeof(char *));
231 devices[n_devices] = strdup(me->mnt_fsname);
237 devices[n_devices] = NULL;
243 Checks if an array of strings contains the specified string.
245 int is_in_array(char **array, char *str)
250 for(i=0; array[i]; ++i)
251 if(!strcmp(str, array[i]))
257 Frees an array of strings.
259 void free_string_array(char **array)
264 for(i=0; array[i]; ++i)
270 Checks if a partition identified by a sysfs path is on a removable device.
272 int is_removable(char *devpath)
279 len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
280 /* Default to not removable if the path was too long. */
281 if(len+10>=(int)sizeof(fnbuf))
284 /* We got a partition as a parameter, but the removable property is on the
285 disk. Replace the last component with "removable". */
286 for(ptr=fnbuf+len; (ptr>fnbuf && *ptr!='/'); --ptr) ;
287 strcpy(ptr, "/removable");
289 fd = open(fnbuf, O_RDONLY);
298 printf(" Removable\n");
302 printf(" Not removable\n");
309 Checks if a partition's disk or any of its parent devices are connected to any
310 of a set of buses. The device is identified by a sysfs path. The bus array
311 must be terminated with a NULL entry.
313 int check_buses(char *devpath, char **buses)
319 len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
320 /* Default to no match if the path was too long. */
321 if(len+10>=(int)sizeof(fnbuf))
324 for(ptr=fnbuf+len; ptr>fnbuf+12; --ptr)
328 /* Replace the last component with "subsystem". */
329 strcpy(ptr, "/subsystem");
330 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
335 /* Extract the last component of the subsystem symlink. */
336 for(; (len>0 && linkbuf[len-1]!='/'); --len) ;
341 printf(" Subsystem of %s is %s\n", fnbuf, linkbuf+len);
344 if(is_in_array(buses, linkbuf+len))
353 Check if an array of properties describes a device that can be mounted. An
354 array of explicitly allowed devices can be passed in as well. Both arrays must
355 be terminated by a NULL entry.
357 int can_mount(Property *props, char **allowed)
359 static char *removable_buses[] = { "usb", "firewire", 0 };
364 devname = get_property_value(props, "DEVNAME");
365 if(is_in_array(allowed, devname))
368 /* Special case for CD devices, since they are not partitions. Only allow
369 mounting if media is inserted. */
370 if(match_property_value(props, "ID_TYPE", "cd") && match_property_value(props, "ID_CDROM_MEDIA", "1"))
373 /* Only allow mounting partitions. */
374 if(!match_property_value(props, "DEVTYPE", "partition"))
377 devpath = get_property_value(props, "DEVPATH");
378 if(is_removable(devpath))
381 /* Certain buses are removable by nature, but devices only advertise
382 themselves as removable if they support removable media, e.g. memory card
384 bus = get_property_value(props, "ID_BUS");
385 if(is_in_array(removable_buses, bus))
388 return check_buses(devpath, removable_buses);
392 Returns an array of all device nodes in a directory. Symbolic links are
395 char **get_device_nodes(char *dirname)
404 char **checked = NULL;
408 dir = opendir(dirname);
412 while((de = readdir(dir)))
417 /* Ignore . and .. entries. */
418 if(de->d_name[0]=='.' && (de->d_name[1]==0 || (de->d_name[1]=='.' && de->d_name[2]==0)))
421 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", dirname, de->d_name);
425 if(S_ISLNK(st.st_mode))
428 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
436 /* There may be multiple symlinks to the same device. Only include each
437 device once in the returned array. */
440 for(i=0; (!duplicate && i<n_checked); ++i)
441 if(strcmp(node, checked[i])==0)
447 printf("Device %s is a duplicate\n", fnbuf);
451 checked = (char **)realloc(checked, (n_checked+1)*sizeof(char *));
452 checked[n_checked] = strdup(node);
455 nodes = (char **)realloc(nodes, (n_nodes+2)*sizeof(char *));
456 nodes[n_nodes] = strdup(fnbuf);
463 for(i=0; i<n_checked; ++i)
469 nodes[n_nodes] = NULL;
475 Reads the list of mounted devices from /etc/mtab and records the mount points.
477 void check_mounts(Device *devices)
482 file = setmntent("/etc/mtab", "r");
486 while((me = getmntent(file)))
490 for(i=0; devices[i].node; ++i)
491 if(!strcmp(devices[i].devname, me->mnt_fsname))
493 devices[i].mount_point = strdup(me->mnt_dir);
496 printf("Device %s is mounted on %s\n", devices[i].node, devices[i].mount_point);
504 Returns an array of all mountable devices.
506 Device *get_devices(void)
509 Device *devices = NULL;
511 char **mounted = NULL;
515 nodes = get_device_nodes("/dev/disk/by-id");
516 fstab = get_fstab_devices();
518 for(i=0; nodes[i]; ++i)
523 printf("Examining device %s\n", nodes[i]);
525 props = get_device_properties(nodes[i]);
529 printf(" No properties\n");
536 for(j=0; props[j].name; ++j)
537 printf(" %s = %s\n", props[j].name, props[j].value);
540 if(can_mount(props, fstab))
551 printf(" Using device\n");
553 devname = get_property_value(props, "DEVNAME");
555 /* Get a human-readable label for the device. Use filesystem label,
556 filesystem UUID or device node name in order of preference. */
557 label = get_property_value(props, "ID_FS_LABEL");
559 label = get_property_value(props, "ID_FS_UUID");
565 for(ptr=label; *ptr; ++ptr)
570 vendor = get_property_value(props, "ID_VENDOR");
571 model = get_property_value(props, "ID_MODEL");
573 pos = snprintf(buf, sizeof(buf), "%s", label);
575 pos += snprintf(buf+pos, sizeof(buf)-pos, " (%s %s)", vendor, model);
579 /* Reserve space for a sentinel entry. */
580 devices = (Device *)realloc(devices, (n_devices+2)*sizeof(Device));
581 devices[n_devices].node = nodes[i];
582 devices[n_devices].devname = strdup(devname);
583 devices[n_devices].label = strdup(label);
584 devices[n_devices].description = strdup(buf);
585 devices[n_devices].mount_point = NULL;
586 devices[n_devices].time = st.st_mtime;
591 free_properties(props);
595 free_string_array(mounted);
599 /* Terminate the array with NULL pointers. */
600 devices[n_devices].node = NULL;
601 devices[n_devices].devname = NULL;
602 devices[n_devices].label = NULL;
603 devices[n_devices].description = NULL;
604 devices[n_devices].mount_point = NULL;
606 check_mounts(devices);
613 Frees an array of devices and all strings contained in it.
615 void free_devices(Device *devices)
620 for(i=0; devices[i].node; ++i)
622 free(devices[i].node);
623 free(devices[i].devname);
624 free(devices[i].label);
625 free(devices[i].description);
626 if(devices[i].mount_point)
627 free(devices[i].mount_point);
633 Mounts a device if it was not mounted, or unmounts if it was.
635 int toggle_device(Device *device, char *out_buf, int out_size)
637 int umount = !!device->mount_point;
638 char mount_point[1024];
642 struct timeval timeout;
649 /* Find a mount point that does not exist yet. */
654 len = snprintf(mount_point, sizeof(mount_point), "/media/%s", device->label);
655 if(len+2>=(int)sizeof(mount_point))
659 len += snprintf(mount_point+len, sizeof(mount_point)-len, "_%c", suffix);
661 if(access(mount_point, F_OK)<0 && errno==ENOENT)
681 printf("Running pumount %s\n", device->node);
683 printf("Running pmount %s %s\n", device->node, mount_point+7);
691 execl("/usr/bin/pumount", "pumount", device->node, NULL);
693 execl("/usr/bin/pmount", "pmount", device->node, mount_point+7, NULL);
703 FD_SET(pipe_fd[0], &fds);
705 timeout.tv_usec = 200000;
709 /* The write fd for the pipe may be inherited by a fuse server
710 process and stay open indefinitely. */
711 if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout))
715 len = read(pipe_fd[0], out_buf+pos, out_size-pos-1);
720 else if(waitpid(pid, &status, 0))
729 waitpid(pid, &status, 0);
735 if(WIFEXITED(status))
737 if(WEXITSTATUS(status))
738 printf("Command exited successfully\n");
740 printf("Command exited with status %d\n", WEXITSTATUS(status));
742 else if(WIFSIGNALED(status))
743 printf("Command terminated with signal %d\n", WTERMSIG(status));
745 printf("Command exited with unknown result %04X\n", status);
748 if(!WIFEXITED(status) || WEXITSTATUS(status))
753 free(device->mount_point);
754 device->mount_point = NULL;
757 device->mount_point = strdup(mount_point);
763 Callback for activating a row in the device list. Mounts or unmounts the
764 device depending on operating mode.
766 void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
775 model = gtk_tree_view_get_model(list);
777 if(!gtk_tree_model_get_iter(model, &iter, path))
780 gtk_tree_model_get(model, &iter, 1, &device, -1);
781 ret = toggle_device(device, output, sizeof(output));
786 /* Pmount terminated with nonzero status or a signal. Display an
787 error to the user. */
788 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", output);
789 g_signal_connect(dialog, "response", >k_main_quit, NULL);
790 gtk_widget_show_all(dialog);
796 if(post_mount_command && device->mount_point)
799 printf("Running %s in %s\n", post_mount_command, device->mount_point);
804 chdir(device->mount_point);
805 execlp(post_mount_command, post_mount_command, NULL);
815 Callback for the mount/unmount button. Causes the selected row in the device
816 list to be activated.
818 void button_clicked(GtkButton *button, gpointer user_data)
820 GtkWidget *list = (GtkWidget *)user_data;
821 GtkTreeSelection *selection;
826 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
827 gtk_tree_selection_get_selected(selection, &model, &iter);
828 path = gtk_tree_model_get_path(model, &iter);
829 gtk_tree_view_row_activated(GTK_TREE_VIEW(list), path, gtk_tree_view_get_column(GTK_TREE_VIEW(list), 0));
830 gtk_tree_path_free(path);
836 Global key press callback for the window.
838 gboolean key_press(GtkWidget *widget, GdkEvent *event, gpointer user_data)
840 if(event->key.keyval==GDK_KEY_Escape)
854 printf("pmount-gui\n"
855 "Copyright (c) 2011-2015 Mikko Rasa, Mikkosoft Productions\n\n"
856 "Usage: pmount-gui [-v] [-u] [-r <command>] [-h]\n\n"
858 " -v Increase verbosity\n"
859 " -u Unmount a device (default is mount)\n"
860 " -r Run a command after mounting\n"
861 " -h Display this help\n");
864 int main(int argc, char **argv)
871 GtkTreeSelection *selection;
881 gtk_init(&argc, &argv);
883 while((opt = getopt(argc, argv, "vur:h"))!=-1) switch(opt)
892 post_mount_command = optarg;
899 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
900 gtk_container_set_border_width(GTK_CONTAINER(window), 5);
901 g_signal_connect(window, "destroy", G_CALLBACK(>k_main_quit), NULL);
902 g_signal_connect(window, "key-press-event", G_CALLBACK(&key_press), NULL);
904 box = gtk_vbox_new(FALSE, 5);
905 gtk_container_add(GTK_CONTAINER(window), box);
907 viewport = gtk_viewport_new(NULL, NULL);
908 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
909 gtk_box_pack_start(GTK_BOX(box), viewport, TRUE, TRUE, 0);
911 list = gtk_tree_view_new();
912 gtk_container_add(GTK_CONTAINER(viewport), list);
913 g_signal_connect(list, "row-activated", G_CALLBACK(&row_activated), NULL);
915 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
916 gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
917 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),
918 -1, "Device", gtk_cell_renderer_text_new(), "text", 0, NULL);
920 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
922 button = gtk_button_new_with_label(umount ? "Unmount" : "Mount");
923 g_signal_connect(button, "clicked", G_CALLBACK(&button_clicked), list);
924 gtk_box_pack_start(GTK_BOX(box), button, FALSE, TRUE, 0);
926 devices = get_devices();
930 /* Populate the list with devices in appropriate state. */
932 for(i=0; devices[i].node; ++i)
933 if(!devices[i].mount_point==!umount)
935 gtk_list_store_append(store, &iter);
936 gtk_list_store_set(store, &iter, 0, devices[i].description, 1, &devices[i], -1);
937 if(devices[i].time>latest)
939 /* Pre-select the device that appeared on the system most recently. */
940 latest = devices[i].time;
941 gtk_tree_selection_select_iter(selection, &iter);
950 gtk_widget_show_all(window);
955 /* Don't show the main window if no devices were listed. */
956 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
957 "No devices to %s", (umount ? "unmount" : "mount"));
958 g_signal_connect(dialog, "response", G_CALLBACK(>k_main_quit), NULL);
959 gtk_widget_show_all(dialog);
964 free_devices(devices);