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
32 char *post_mount_command = NULL;
35 Parses a string of the form name=value and places the components in a Property
36 structure. Returns 0 on success, or -1 if the string wasn't a valid property.
38 int parse_property(char *str, int size, Property *prop)
43 for(i=0; (equals<0 && i<size); ++i)
50 prop->name = malloc(equals+1);
51 strncpy(prop->name, str, equals);
52 prop->name[equals] = 0;
54 prop->value = malloc(size-equals);
55 strncpy(prop->value, str+equals+1, size-equals-1);
56 prop->value[size-equals-1] = 0;
62 Retrieves all properties associated with a /dev node. The returned array is
63 terminated with an entry containing NULL values. Use free_properties to free
66 Property *get_device_properties(char *node)
74 Property *props = NULL;
84 printf("Running udevadm info -q property -n \"%s\"\n", node);
89 execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL);
104 buf = (char *)malloc(bufsize);
116 len = read(pipe_fd[0], buf+pos, bufsize-pos);
125 for(i=0; (newline<0 && i<pos); ++i)
134 /* There was no newline in the buffer but there is more output to
135 be read. Try again with a larger buffer. */
137 buf = (char *)realloc(buf, bufsize);
141 if(parse_property(buf, newline, &prop)==0)
143 /* Reserve space for a sentinel value as well. */
144 props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
145 props[n_props] = prop;
148 memmove(buf, buf+newline+1, pos-newline-1);
159 /* Terminate the array with NULL pointers. */
160 props[n_props].name = NULL;
161 props[n_props].value = NULL;
164 waitpid(pid, NULL, 0);
171 Looks for a property in an array of properties and returns its value. Returns
172 NULL if the property was not found.
174 char *get_property_value(Property *props, char *name)
177 for(i=0; props[i].name; ++i)
178 if(strcmp(props[i].name, name)==0)
179 return props[i].value;
184 Checks if a property has a specific value. A NULL value is matched if the
185 property does not exist.
187 int match_property_value(Property *props, char *name, char *value)
189 char *v = get_property_value(props, name);
192 return strcmp(v, value)==0;
196 Frees an array of properties and all strings contained in it.
198 void free_properties(Property *props)
203 for(i=0; props[i].name; ++i)
206 free(props[i].value);
212 Reads device names from an fstab/mtab file.
214 char **get_mount_entries(char *filename, int (*predicate)(struct mntent *))
218 char **devices = NULL;
221 file = setmntent(filename, "r");
225 while((me = getmntent(file)))
226 if(!predicate || predicate(me))
228 devices = (char **)realloc(devices, (n_devices+2)*sizeof(char *));
229 devices[n_devices] = strdup(me->mnt_fsname);
235 devices[n_devices] = NULL;
241 Returns an array of all currently mounted devices.
243 char **get_mounted_devices(void)
245 return get_mount_entries("/etc/mtab", NULL);
249 Checks if an fstab entry has the user option set.
251 int is_user_mountable(struct mntent *me)
253 return hasmntopt(me, "user")!=NULL;
257 Returns an array of user-mountable devices listed in fstab.
259 char **get_fstab_devices(void)
261 return get_mount_entries("/etc/fstab", &is_user_mountable);
265 Checks if an array of strings contains the specified string.
267 int is_in_array(char **array, char *str)
272 for(i=0; array[i]; ++i)
273 if(!strcmp(str, array[i]))
279 Frees an array of strings.
281 void free_string_array(char **array)
286 for(i=0; array[i]; ++i)
292 Checks if a partition identified by a sysfs path is on a removable device.
294 int is_removable(char *devpath)
301 len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
302 /* Default to not removable if the path was too long. */
303 if(len+10>=(int)sizeof(fnbuf))
306 /* We got a partition as a parameter, but the removable property is on the
307 disk. Replace the last component with "removable". */
308 for(ptr=fnbuf+len; (ptr>fnbuf && *ptr!='/'); --ptr) ;
309 strcpy(ptr, "/removable");
311 fd = open(fnbuf, O_RDONLY);
320 printf(" Removable\n");
324 printf(" Not removable\n");
331 Checks if a partition's disk or any of its parent devices are connected to any
332 of a set of buses. The device is identified by a sysfs path. The bus array
333 must be terminated with a NULL entry.
335 int check_buses(char *devpath, char **buses)
341 len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
342 /* Default to no match if the path was too long. */
343 if(len+10>=(int)sizeof(fnbuf))
346 for(ptr=fnbuf+len; ptr>fnbuf+12; --ptr)
350 /* Replace the last component with "subsystem". */
351 strcpy(ptr, "/subsystem");
352 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
357 /* Extract the last component of the subsystem symlink. */
358 for(; (len>0 && linkbuf[len-1]!='/'); --len) ;
363 printf(" Subsystem of %s is %s\n", fnbuf, linkbuf+len);
366 if(is_in_array(buses, linkbuf+len))
375 Check if an array of properties describes a device that can be mounted. An
376 array of explicitly allowed devices can be passed in as well. Both arrays must
377 be terminated by a NULL entry.
379 int can_mount(Property *props, char **allowed)
381 static char *removable_buses[] = { "usb", "firewire", 0 };
386 devname = get_property_value(props, "DEVNAME");
387 if(is_in_array(allowed, devname))
390 /* Special case for CD devices, since they are not partitions. Only allow
391 mounting if media is inserted. */
392 if(match_property_value(props, "ID_TYPE", "cd") && match_property_value(props, "ID_CDROM_MEDIA", "1"))
395 /* Only allow mounting partitions. */
396 if(!match_property_value(props, "DEVTYPE", "partition"))
399 devpath = get_property_value(props, "DEVPATH");
400 if(is_removable(devpath))
403 /* Certain buses are removable by nature, but devices only advertise
404 themselves as removable if they support removable media, e.g. memory card
406 bus = get_property_value(props, "ID_BUS");
407 if(is_in_array(removable_buses, bus))
410 return check_buses(devpath, removable_buses);
414 Returns an array of all device nodes in a directory. Symbolic links are
417 char **get_device_nodes(char *dirname)
426 char **checked = NULL;
430 dir = opendir(dirname);
434 while((de = readdir(dir)))
439 /* Ignore . and .. entries. */
440 if(de->d_name[0]=='.' && (de->d_name[1]==0 || (de->d_name[1]=='.' && de->d_name[2]==0)))
443 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", dirname, de->d_name);
447 if(S_ISLNK(st.st_mode))
450 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
458 /* There may be multiple symlinks to the same device. Only include each
459 device once in the returned array. */
462 for(i=0; (!duplicate && i<n_checked); ++i)
463 if(strcmp(node, checked[i])==0)
469 printf("Device %s is a duplicate\n", fnbuf);
473 checked = (char **)realloc(checked, (n_checked+1)*sizeof(char *));
474 checked[n_checked] = strdup(node);
477 nodes = (char **)realloc(nodes, (n_nodes+2)*sizeof(char *));
478 nodes[n_nodes] = strdup(fnbuf);
485 for(i=0; i<n_checked; ++i)
491 nodes[n_nodes] = NULL;
496 /** Returns an array of all mountable devices. */
497 Device *get_devices(void)
500 Device *devices = NULL;
502 char **mounted = NULL;
506 nodes = get_device_nodes("/dev/disk/by-id");
507 mounted = get_mounted_devices();
508 fstab = get_fstab_devices();
510 for(i=0; nodes[i]; ++i)
515 printf("Examining device %s\n", nodes[i]);
517 props = get_device_properties(nodes[i]);
521 printf(" No properties\n");
528 for(j=0; props[j].name; ++j)
529 printf(" %s = %s\n", props[j].name, props[j].value);
532 if(can_mount(props, fstab))
543 printf(" Using device\n");
545 devname = get_property_value(props, "DEVNAME");
547 /* Get a human-readable label for the device. Use filesystem label,
548 filesystem UUID or device node name in order of preference. */
549 label = get_property_value(props, "ID_FS_LABEL");
551 label = get_property_value(props, "ID_FS_UUID");
557 for(ptr=label; *ptr; ++ptr)
562 vendor = get_property_value(props, "ID_VENDOR");
563 model = get_property_value(props, "ID_MODEL");
565 pos = snprintf(buf, sizeof(buf), "%s", label);
567 pos += snprintf(buf+pos, sizeof(buf)-pos, " (%s %s)", vendor, model);
571 /* Reserve space for a sentinel entry. */
572 devices = (Device *)realloc(devices, (n_devices+2)*sizeof(Device));
573 devices[n_devices].node = nodes[i];
574 devices[n_devices].label = strdup(label);
575 devices[n_devices].description = strdup(buf);
576 devices[n_devices].mounted = is_in_array(mounted, devname);
577 devices[n_devices].time = st.st_mtime;
582 free_properties(props);
586 free_string_array(mounted);
590 /* Terminate the array with NULL pointers. */
591 devices[n_devices].node = NULL;
592 devices[n_devices].label = NULL;
593 devices[n_devices].description = NULL;
600 Frees an array of devices and all strings contained in it.
602 void free_devices(Device *devices)
607 for(i=0; devices[i].node; ++i)
609 free(devices[i].node);
610 free(devices[i].label);
611 free(devices[i].description);
617 Callback for activating a row in the device list. Mounts or unmounts the
618 device depending on operating mode.
620 void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
624 int umount = *(int *)user_data;
629 struct timeval timeout;
634 model = gtk_tree_view_get_model(list);
636 if(!gtk_tree_model_get_iter(model, &iter, path))
639 gtk_tree_model_get(model, &iter, 1, &device, -1);
650 printf("Running pumount %s\n", device->node);
652 printf("Running pmount %s %s\n", device->node, device->label);
660 execl("/usr/bin/pumount", "pumount", device->node, NULL);
662 execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL);
672 FD_SET(pipe_fd[0], &fds);
674 timeout.tv_usec = 200000;
678 /* The write fd for the pipe may be inherited by a fuse server
679 process and stay open indefinitely. */
680 if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout))
684 len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1);
689 else if(waitpid(pid, &status, 0))
698 waitpid(pid, &status, 0);
704 if(WIFEXITED(status))
706 if(WEXITSTATUS(status))
707 printf("Command exited successfully\n");
709 printf("Command exited with status %d\n", WEXITSTATUS(status));
711 else if(WIFSIGNALED(status))
712 printf("Command terminated with signal %d\n", WTERMSIG(status));
714 printf("Command exited with unknown result %04X\n", status);
717 if(!WIFEXITED(status) || WEXITSTATUS(status))
721 /* Pmount terminated with nonzero status or a signal. Display an
722 error to the user. */
723 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf);
724 g_signal_connect(dialog, "response", >k_main_quit, NULL);
725 gtk_widget_show_all(dialog);
731 if(post_mount_command && !umount)
735 len = snprintf(workdir, sizeof(workdir), "/media/%s", device->label);
736 if(len<(int)sizeof(workdir))
739 printf("Running %s in %s\n", post_mount_command, workdir);
745 execlp(post_mount_command, post_mount_command, NULL);
755 Callback for the mount/unmount button. Causes the selected row in the device
756 list to be activated.
758 void button_clicked(GtkButton *button, gpointer user_data)
760 GtkWidget *list = (GtkWidget *)user_data;
761 GtkTreeSelection *selection;
766 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
767 gtk_tree_selection_get_selected(selection, &model, &iter);
768 path = gtk_tree_model_get_path(model, &iter);
769 gtk_tree_view_row_activated(GTK_TREE_VIEW(list), path, gtk_tree_view_get_column(GTK_TREE_VIEW(list), 0));
770 gtk_tree_path_free(path);
776 Global key press callback for the window.
778 gboolean key_press(GtkWidget *widget, GdkEvent *event, gpointer user_data)
780 if(event->key.keyval==GDK_KEY_Escape)
792 int main(int argc, char **argv)
799 GtkTreeSelection *selection;
809 gtk_init(&argc, &argv);
811 while((opt = getopt(argc, argv, "vur:"))!=-1) switch(opt)
820 post_mount_command = optarg;
824 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
825 gtk_container_set_border_width(GTK_CONTAINER(window), 5);
826 g_signal_connect(window, "destroy", G_CALLBACK(>k_main_quit), NULL);
827 g_signal_connect(window, "key-press-event", G_CALLBACK(&key_press), NULL);
829 box = gtk_vbox_new(FALSE, 5);
830 gtk_container_add(GTK_CONTAINER(window), box);
832 viewport = gtk_viewport_new(NULL, NULL);
833 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
834 gtk_box_pack_start(GTK_BOX(box), viewport, TRUE, TRUE, 0);
836 list = gtk_tree_view_new();
837 gtk_container_add(GTK_CONTAINER(viewport), list);
838 g_signal_connect(list, "row-activated", G_CALLBACK(&row_activated), &umount);
840 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
841 gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
842 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),
843 -1, "Device", gtk_cell_renderer_text_new(), "text", 0, NULL);
845 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
847 button = gtk_button_new_with_label(umount ? "Unmount" : "Mount");
848 g_signal_connect(button, "clicked", G_CALLBACK(&button_clicked), list);
849 gtk_box_pack_start(GTK_BOX(box), button, FALSE, TRUE, 0);
851 devices = get_devices();
855 /* Populate the list with devices in appropriate state. */
857 for(i=0; devices[i].node; ++i)
858 if(!devices[i].mounted==!umount)
860 gtk_list_store_append(store, &iter);
861 gtk_list_store_set(store, &iter, 0, devices[i].description, 1, &devices[i], -1);
862 if(devices[i].time>latest)
864 /* Pre-select the device that appeared on the system most recently. */
865 latest = devices[i].time;
866 gtk_tree_selection_select_iter(selection, &iter);
875 gtk_widget_show_all(window);
880 /* Don't show the main window if no devices were listed. */
881 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
882 "No devices to %s", (umount ? "unmount" : "mount"));
883 g_signal_connect(dialog, "response", G_CALLBACK(>k_main_quit), NULL);
884 gtk_widget_show_all(dialog);
889 free_devices(devices);