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
34 Parses a string of the form name=value and places the components in a Property
35 structure. Returns 0 on success, or -1 if the string wasn't a valid property.
37 int parse_property(char *str, int size, Property *prop)
42 for(i=0; (equals<0 && i<size); ++i)
49 prop->name = malloc(equals+1);
50 strncpy(prop->name, str, equals);
51 prop->name[equals] = 0;
53 prop->value = malloc(size-equals);
54 strncpy(prop->value, str+equals+1, size-equals-1);
55 prop->value[size-equals-1] = 0;
61 Retrieves all properties associated with a /dev node. The returned array is
62 terminated with an entry containing NULL values. Use free_properties to free
65 Property *get_device_properties(char *node)
77 printf("Running udevadm info -q property -n \"%s\"\n", node);
82 execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL);
92 Property *props = NULL;
98 buf = (char *)malloc(bufsize);
110 len = read(pipe_fd[0], buf+pos, bufsize-pos);
119 for(i=0; (newline<0 && i<pos); ++i)
128 /* There was no newline in the buffer but there is more output to
129 be read. Try again with a larger buffer. */
131 buf = (char *)realloc(buf, bufsize);
135 if(parse_property(buf, newline, &prop)==0)
137 /* Reserve space for a sentinel value as well. */
138 props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
139 props[n_props] = prop;
142 memmove(buf, buf+newline+1, pos-newline-1);
153 /* Terminate the array with NULL pointers. */
154 props[n_props].name = NULL;
155 props[n_props].value = NULL;
158 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 Reads device names from an fstab/mtab file.
216 char **get_mount_entries(char *filename, int (*predicate)(struct mntent *))
220 char **devices = NULL;
223 file = setmntent(filename, "r");
227 while((me = getmntent(file)))
228 if(!predicate || predicate(me))
230 devices = (char **)realloc(devices, (n_devices+2)*sizeof(char *));
231 devices[n_devices] = strdup(me->mnt_fsname);
237 devices[n_devices] = NULL;
243 Returns an array of all currently mounted devices.
245 char **get_mounted_devices(void)
247 return get_mount_entries("/etc/mtab", NULL);
251 Checks if an fstab entry has the user option set.
253 int is_user_mountable(struct mntent *me)
255 return hasmntopt(me, "user")!=NULL;
259 Returns an array of user-mountable devices listed in fstab.
261 char **get_fstab_devices(void)
263 return get_mount_entries("/etc/fstab", &is_user_mountable);
267 Checks if an array of strings contains the specified string.
269 int is_in_array(char **array, char *str)
274 for(i=0; array[i]; ++i)
275 if(!strcmp(str, array[i]))
281 Frees an array of strings.
283 void free_string_array(char **array)
288 for(i=0; array[i]; ++i)
294 Checks if a partition identified by a sysfs path is on a removable device.
296 int is_removable(char *devpath)
303 len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
304 /* Default to not removable if the path was too long. */
305 if(len+10>=(int)sizeof(fnbuf))
308 /* We got a partition as a parameter, but the removable property is on the
309 disk. Replace the last component with "removable". */
310 for(ptr=fnbuf+len; (ptr>fnbuf && *ptr!='/'); --ptr) ;
311 strcpy(ptr, "/removable");
313 fd = open(fnbuf, O_RDONLY);
322 printf(" Removable\n");
326 printf(" Not removable\n");
333 Checks if a partition's disk or any of its parent devices are connected to any
334 of a set of buses. The device is identified by a sysfs path. The bus array
335 must be terminated with a NULL entry.
337 int check_buses(char *devpath, char **buses)
343 len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
344 /* Default to no match if the path was too long. */
345 if(len+10>=(int)sizeof(fnbuf))
348 for(ptr=fnbuf+len; ptr>fnbuf+12; --ptr)
352 /* Replace the last component with "subsystem". */
353 strcpy(ptr, "/subsystem");
354 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
359 /* Extract the last component of the subsystem symlink. */
360 for(; (len>0 && linkbuf[len-1]!='/'); --len) ;
365 printf(" Subsystem of %s is %s\n", fnbuf, linkbuf+len);
368 if(is_in_array(buses, linkbuf+len))
377 Check if an array of properties describes a device that can be mounted. An
378 array of explicitly allowed devices can be passed in as well. Both arrays must
379 be terminated by a NULL entry.
381 int can_mount(Property *props, char **allowed)
383 static char *removable_buses[] = { "usb", "firewire", 0 };
388 devname = get_property_value(props, "DEVNAME");
389 if(is_in_array(allowed, devname))
392 /* Special case for CD devices, since they are not partitions. Only allow
393 mounting if media is inserted. */
394 if(match_property_value(props, "ID_TYPE", "cd") && match_property_value(props, "ID_CDROM_MEDIA", "1"))
397 /* Only allow mounting partitions. */
398 if(!match_property_value(props, "DEVTYPE", "partition"))
401 devpath = get_property_value(props, "DEVPATH");
402 if(is_removable(devpath))
405 /* Certain buses are removable by nature, but devices only advertise
406 themselves as removable if they support removable media, e.g. memory card
408 bus = get_property_value(props, "ID_BUS");
409 if(is_in_array(removable_buses, bus))
412 return check_buses(devpath, removable_buses);
416 Returns an array of all device nodes in a directory. Symbolic links are
419 char **get_device_nodes(char *dirname)
428 char **checked = NULL;
432 dir = opendir(dirname);
436 while((de = readdir(dir)))
441 /* Ignore . and .. entries. */
442 if(de->d_name[0]=='.' && (de->d_name[1]==0 || (de->d_name[1]=='.' && de->d_name[2]==0)))
445 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", dirname, de->d_name);
449 if(S_ISLNK(st.st_mode))
452 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
460 /* There may be multiple symlinks to the same device. Only include each
461 device once in the returned array. */
464 for(i=0; (!duplicate && i<n_checked); ++i)
465 if(strcmp(node, checked[i])==0)
471 printf("Device %s is a duplicate\n", fnbuf);
475 checked = (char **)realloc(checked, (n_checked+1)*sizeof(char *));
476 checked[n_checked] = strdup(node);
479 nodes = (char **)realloc(nodes, (n_nodes+2)*sizeof(char *));
480 nodes[n_nodes] = strdup(fnbuf);
487 for(i=0; i<n_checked; ++i)
493 nodes[n_nodes] = NULL;
498 /** Returns an array of all mountable devices. */
499 Device *get_devices(void)
502 Device *devices = NULL;
504 char **mounted = NULL;
508 nodes = get_device_nodes("/dev/disk/by-id");
509 mounted = get_mounted_devices();
510 fstab = get_fstab_devices();
512 for(i=0; nodes[i]; ++i)
517 printf("Examining device %s\n", nodes[i]);
519 props = get_device_properties(nodes[i]);
523 printf(" No properties\n");
530 for(j=0; props[j].name; ++j)
531 printf(" %s = %s\n", props[j].name, props[j].value);
534 if(can_mount(props, fstab))
545 printf(" Using device\n");
547 devname = get_property_value(props, "DEVNAME");
549 /* Get a human-readable label for the device. Use filesystem label,
550 filesystem UUID or device node name in order of preference. */
551 label = get_property_value(props, "ID_FS_LABEL");
553 label = get_property_value(props, "ID_FS_UUID");
559 for(ptr=label; *ptr; ++ptr)
564 vendor = get_property_value(props, "ID_VENDOR");
565 model = get_property_value(props, "ID_MODEL");
567 pos = snprintf(buf, sizeof(buf), "%s", label);
569 pos += snprintf(buf+pos, sizeof(buf)-pos, " (%s %s)", vendor, model);
573 /* Reserve space for a sentinel entry. */
574 devices = (Device *)realloc(devices, (n_devices+2)*sizeof(Device));
575 devices[n_devices].node = nodes[i];
576 devices[n_devices].label = strdup(label);
577 devices[n_devices].description = strdup(buf);
578 devices[n_devices].mounted = is_in_array(mounted, devname);
579 devices[n_devices].time = st.st_mtime;
584 free_properties(props);
588 free_string_array(mounted);
592 /* Terminate the array with NULL pointers. */
593 devices[n_devices].node = NULL;
594 devices[n_devices].label = NULL;
595 devices[n_devices].description = NULL;
602 Frees an array of devices and all strings contained in it.
604 void free_devices(Device *devices)
609 for(i=0; devices[i].node; ++i)
611 free(devices[i].node);
612 free(devices[i].label);
613 free(devices[i].description);
619 Callback for activating a row in the device list. Mounts or unmounts the
620 device depending on operating mode.
622 void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
626 int umount = *(int *)user_data;
628 model = gtk_tree_view_get_model(list);
630 if(gtk_tree_model_get_iter(model, &iter, path))
636 gtk_tree_model_get(model, &iter, 1, &device, -1);
647 printf("Running pumount %s\n", device->node);
649 printf("Running pmount %s %s\n", device->node, device->label);
657 execl("/usr/bin/pumount", "pumount", device->node, NULL);
659 execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL);
669 struct timeval timeout;
673 FD_SET(pipe_fd[0], &fds);
675 timeout.tv_usec = 200000;
679 /* The write fd for the pipe may be inherited by a fuse server
680 process and stay open indefinitely. */
681 if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout))
685 len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1);
690 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);
736 Callback for the mount/unmount button. Causes the selected row in the device
737 list to be activated.
739 void button_clicked(GtkButton *button, gpointer user_data)
741 GtkWidget *list = (GtkWidget *)user_data;
742 GtkTreeSelection *selection;
747 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
748 gtk_tree_selection_get_selected(selection, &model, &iter);
749 path = gtk_tree_model_get_path(model, &iter);
750 gtk_tree_view_row_activated(GTK_TREE_VIEW(list), path, gtk_tree_view_get_column(GTK_TREE_VIEW(list), 0));
751 gtk_tree_path_free(path);
757 Global key press callback for the window.
759 gboolean key_press(GtkWidget *widget, GdkEvent *event, gpointer user_data)
761 if(event->key.keyval==GDK_KEY_Escape)
773 int main(int argc, char **argv)
780 GtkTreeSelection *selection;
790 gtk_init(&argc, &argv);
792 while((opt = getopt(argc, argv, "vu"))!=-1) switch(opt)
802 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
803 gtk_container_set_border_width(GTK_CONTAINER(window), 5);
804 g_signal_connect(window, "destroy", G_CALLBACK(>k_main_quit), NULL);
805 g_signal_connect(window, "key-press-event", G_CALLBACK(&key_press), NULL);
807 box = gtk_vbox_new(FALSE, 5);
808 gtk_container_add(GTK_CONTAINER(window), box);
810 viewport = gtk_viewport_new(NULL, NULL);
811 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
812 gtk_box_pack_start(GTK_BOX(box), viewport, TRUE, TRUE, 0);
814 list = gtk_tree_view_new();
815 gtk_container_add(GTK_CONTAINER(viewport), list);
816 g_signal_connect(list, "row-activated", G_CALLBACK(&row_activated), &umount);
818 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
819 gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
820 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),
821 -1, "Device", gtk_cell_renderer_text_new(), "text", 0, NULL);
823 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
825 button = gtk_button_new_with_label(umount ? "Unmount" : "Mount");
826 g_signal_connect(button, "clicked", G_CALLBACK(&button_clicked), list);
827 gtk_box_pack_start(GTK_BOX(box), button, FALSE, TRUE, 0);
829 devices = get_devices();
833 /* Populate the list with devices in appropriate state. */
835 for(i=0; devices[i].node; ++i)
836 if(!devices[i].mounted==!umount)
838 gtk_list_store_append(store, &iter);
839 gtk_list_store_set(store, &iter, 0, devices[i].description, 1, &devices[i], -1);
840 if(devices[i].time>latest)
842 /* Pre-select the device that appeared on the system most recently. */
843 latest = devices[i].time;
844 gtk_tree_selection_select_iter(selection, &iter);
853 gtk_widget_show_all(window);
858 /* Don't show the main window if no devices were listed. */
859 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
860 "No devices to %s", (umount ? "unmount" : "mount"));
861 g_signal_connect(dialog, "response", G_CALLBACK(>k_main_quit), NULL);
862 gtk_widget_show_all(dialog);
867 free_devices(devices);