11 #include <gdk/gdkkeysyms.h>
13 typedef struct sProperty
19 typedef struct sDevice
30 int parse_property(char *str, int size, Property *prop)
35 for(i=0; (equals<0 && i<size); ++i)
42 prop->name = malloc(equals+1);
43 strncpy(prop->name, str, equals);
44 prop->name[equals] = 0;
46 prop->value = malloc(size-equals);
47 strncpy(prop->value, str+equals+1, size-equals-1);
48 prop->value[size-equals-1] = 0;
53 Property *get_device_properties(char *node)
64 printf("Running udevadm info -q property -n \"%s\"\n", node);
69 execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL);
78 Property *props = NULL;
84 buf = (char *)malloc(bufsize);
96 len = read(pipe_fd[0], buf+pos, bufsize-pos);
105 for(i=0; (newline<0 && i<pos); ++i)
114 buf = (char *)realloc(buf, bufsize);
118 if(parse_property(buf, newline, &prop)==0)
120 props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
121 props[n_props] = prop;
124 memmove(buf, buf+newline+1, pos-newline-1);
135 props[n_props].name = NULL;
136 props[n_props].value = NULL;
139 waitpid(pid, NULL, 0);
153 char *get_property_value(Property *props, char *name)
156 for(i=0; props[i].name; ++i)
157 if(strcmp(props[i].name, name)==0)
158 return props[i].value;
162 int match_property_value(Property *props, char *name, char *value)
164 char *v = get_property_value(props, name);
167 return strcmp(v, value)==0;
170 void free_properties(Property *props)
175 for(i=0; props[i].name; ++i)
178 free(props[i].value);
183 char **get_mount_entries(char *filename, int (*predicate)(struct mntent *))
187 char **devices = NULL;
190 file = setmntent(filename, "r");
194 while((me = getmntent(file)))
195 if(!predicate || predicate(me))
197 devices = (char **)realloc(devices, (n_devices+2)*sizeof(char *));
198 devices[n_devices] = strdup(me->mnt_fsname);
204 devices[n_devices] = NULL;
209 char **get_mounted_devices(void)
211 return get_mount_entries("/etc/mtab", NULL);
214 int is_user_mountable(struct mntent *me)
216 return hasmntopt(me, "user")!=NULL;
219 char **get_fstab_devices(void)
221 return get_mount_entries("/etc/fstab", &is_user_mountable);
224 int is_in_array(char **names, char *devname)
227 if(!names || !devname)
229 for(i=0; names[i]; ++i)
230 if(!strcmp(devname, names[i]))
235 void free_device_names(char **names)
240 for(i=0; names[i]; ++i)
245 int is_removable(char *devpath)
252 len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
253 if(len+10>=(int)sizeof(fnbuf))
256 for(ptr=fnbuf+len; (ptr>fnbuf && *ptr!='/'); --ptr) ;
257 strcpy(ptr, "/removable");
258 fd = open(fnbuf, O_RDONLY);
267 printf(" Removable\n");
271 printf(" Not removable\n");
277 int check_buses(char *devpath, char **buses)
283 len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
284 if(len+10>=(int)sizeof(fnbuf))
287 for(ptr=fnbuf+len; ptr>fnbuf+12; --ptr)
291 strcpy(ptr, "/subsystem");
292 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
299 for(; (len>0 && linkbuf[len-1]!='/'); --len) ;
301 printf(" Subsystem of %s is %s\n", fnbuf, linkbuf+len);
302 for(i=0; buses[i]; ++i)
303 if(strcmp(linkbuf+len, buses[i])==0)
311 int can_mount(Property *props, char **allowed)
313 static char *removable_buses[] = { "usb", "firewire", 0 };
318 devname = get_property_value(props, "DEVNAME");
319 if(is_in_array(allowed, devname))
322 if(!match_property_value(props, "DEVTYPE", "partition"))
325 devpath = get_property_value(props, "DEVPATH");
326 if(is_removable(devpath))
329 bus = get_property_value(props, "ID_BUS");
330 if(is_in_array(removable_buses, bus))
333 return check_buses(devpath, removable_buses);
336 char **get_device_nodes(char *dirname)
345 char **checked = NULL;
349 dir = opendir(dirname);
353 while((de = readdir(dir)))
358 if(de->d_name[0]=='.' && (de->d_name[1]==0 || (de->d_name[1]=='.' && de->d_name[2]==0)))
361 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", dirname, de->d_name);
365 if(S_ISLNK(st.st_mode))
368 len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
378 for(i=0; (!duplicate && i<n_checked); ++i)
379 if(strcmp(node, checked[i])==0)
385 printf("Device %s is a duplicate\n", fnbuf);
389 checked = (char **)realloc(checked, (n_checked+1)*sizeof(char *));
390 checked[n_checked] = strdup(node);
393 nodes = (char **)realloc(nodes, (n_nodes+2)*sizeof(char *));
394 nodes[n_nodes] = strdup(fnbuf);
401 for(i=0; i<n_checked; ++i)
407 nodes[n_nodes] = NULL;
412 Device *get_devices(void)
415 Device *devices = NULL;
417 char **mounted = NULL;
421 nodes = get_device_nodes("/dev/disk/by-id");
422 mounted = get_mounted_devices();
423 fstab = get_fstab_devices();
425 for(i=0; nodes[i]; ++i)
428 printf("Examining device %s\n", nodes[i]);
430 Property *props = get_device_properties(nodes[i]);
434 printf(" No properties\n");
441 for(j=0; props[j].name; ++j)
442 printf(" %s = %s\n", props[j].name, props[j].value);
445 if(can_mount(props, fstab))
456 printf(" Using device\n");
458 devname = get_property_value(props, "DEVNAME");
460 label = get_property_value(props, "ID_FS_LABEL");
462 label = get_property_value(props, "ID_FS_UUID");
468 for(ptr=label; *ptr; ++ptr)
473 vendor = get_property_value(props, "ID_VENDOR");
474 model = get_property_value(props, "ID_MODEL");
476 pos = snprintf(buf, sizeof(buf), "%s", label);
478 pos += snprintf(buf+pos, sizeof(buf)-pos, " (%s %s)", vendor, model);
482 devices = (Device *)realloc(devices, (n_devices+2)*sizeof(Device));
483 devices[n_devices].node = nodes[i];
484 devices[n_devices].label = strdup(label);
485 devices[n_devices].description = strdup(buf);
486 devices[n_devices].mounted = is_in_array(mounted, devname);
487 devices[n_devices].time = st.st_mtime;
492 free_properties(props);
496 free_device_names(mounted);
500 devices[n_devices].node = NULL;
501 devices[n_devices].label = NULL;
502 devices[n_devices].description = NULL;
508 void free_devices(Device *devices)
513 for(i=0; devices[i].node; ++i)
515 free(devices[i].node);
516 free(devices[i].label);
517 free(devices[i].description);
522 void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
526 int umount = *(int *)user_data;
528 model = gtk_tree_view_get_model(list);
530 if(gtk_tree_model_get_iter(model, &iter, path))
536 gtk_tree_model_get(model, &iter, 1, &device, -1);
546 printf("Running pumount %s\n", device->node);
548 printf("Running pmount %s %s\n", device->node, device->label);
556 execl("/usr/bin/pumount", "pumount", device->node, NULL);
558 execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL);
573 len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1);
581 waitpid(pid, &status, 0);
582 if(!WIFEXITED(status) || WEXITSTATUS(status))
586 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf);
587 g_signal_connect(dialog, "response", >k_main_quit, NULL);
588 gtk_widget_show_all(dialog);
601 gboolean key_press(GtkWidget *widget, GdkEvent *event, gpointer user_data)
603 if(event->key.keyval==GDK_KEY_Escape)
615 int main(int argc, char **argv)
621 GtkTreeSelection *selection;
630 gtk_init(&argc, &argv);
632 while((opt = getopt(argc, argv, "vu"))!=-1) switch(opt)
642 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
643 gtk_container_set_border_width(GTK_CONTAINER(window), 5);
644 g_signal_connect(window, "destroy", G_CALLBACK(>k_main_quit), NULL);
645 g_signal_connect(window, "key-press-event", G_CALLBACK(&key_press), NULL);
647 viewport = gtk_viewport_new(NULL, NULL);
648 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
649 gtk_container_add(GTK_CONTAINER(window), viewport);
651 list = gtk_tree_view_new();
652 gtk_container_add(GTK_CONTAINER(viewport), list);
653 g_signal_connect(list, "row-activated", G_CALLBACK(&row_activated), &umount);
655 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
656 gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
657 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),
658 -1, "Device", gtk_cell_renderer_text_new(), "text", 0, NULL);
660 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
662 devices = get_devices();
667 for(i=0; devices[i].node; ++i)
668 if(!devices[i].mounted==!umount)
670 gtk_list_store_append(store, &iter);
671 gtk_list_store_set(store, &iter, 0, devices[i].description, 1, &devices[i], -1);
672 if(devices[i].time>latest)
674 latest = devices[i].time;
675 gtk_tree_selection_select_iter(selection, &iter);
684 gtk_widget_show_all(window);
689 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
690 "No devices to %s", (umount ? "unmount" : "mount"));
691 g_signal_connect(dialog, "response", G_CALLBACK(>k_main_quit), NULL);
692 gtk_widget_show_all(dialog);
697 free_devices(devices);