X-Git-Url: http://git.tdb.fi/?p=pmount-gui.git;a=blobdiff_plain;f=main.c;h=bed5e1bf0dd0964c69e6fa161816e6d05a9ec075;hp=cd95cc943cc78cabd0142b2b881367847bda9567;hb=ccc2b1b7ed981618ac13bedf540de24256d4431f;hpb=077926116bdc4f245a9f6b0542eba8dabe1ca01c diff --git a/main.c b/main.c index cd95cc9..bed5e1b 100644 --- a/main.c +++ b/main.c @@ -1,3 +1,5 @@ +/* Required for strdup, snprintf, getopt */ +#define _XOPEN_SOURCE 500 #include #include #include @@ -7,6 +9,7 @@ #include #include #include +#include #include #include @@ -211,6 +214,16 @@ char **get_mounted_devices(void) return get_mount_entries("/etc/mtab", NULL); } +int is_user_mountable(struct mntent *me) +{ + return hasmntopt(me, "user")!=NULL; +} + +char **get_fstab_devices(void) +{ + return get_mount_entries("/etc/fstab", &is_user_mountable); +} + int is_in_array(char **names, char *devname) { int i; @@ -298,11 +311,19 @@ int check_buses(char *devpath, char **buses) return 0; } -int can_mount(Property *props) +int can_mount(Property *props, char **allowed) { static char *removable_buses[] = { "usb", "firewire", 0 }; + char *devname; char *devpath; - int i; + char *bus; + + devname = get_property_value(props, "DEVNAME"); + if(is_in_array(allowed, devname)) + return 1; + + if(match_property_value(props, "ID_TYPE", "cd") && match_property_value(props, "ID_CDROM_MEDIA", "1")) + return 1; if(!match_property_value(props, "DEVTYPE", "partition")) return 0; @@ -311,9 +332,9 @@ int can_mount(Property *props) if(is_removable(devpath)) return 1; - for(i=0; removable_buses[i]; ++i) - if(match_property_value(props, "ID_BUS", removable_buses[i])) - return 1; + bus = get_property_value(props, "ID_BUS"); + if(is_in_array(removable_buses, bus)) + return 1; return check_buses(devpath, removable_buses); } @@ -400,17 +421,21 @@ Device *get_devices(void) Device *devices = NULL; int n_devices = 0; char **mounted = NULL; + char **fstab = NULL; int i; nodes = get_device_nodes("/dev/disk/by-id"); mounted = get_mounted_devices(); + fstab = get_fstab_devices(); for(i=0; nodes[i]; ++i) { + Property *props; + if(verbosity>=1) printf("Examining device %s\n", nodes[i]); - Property *props = get_device_properties(nodes[i]); + props = get_device_properties(nodes[i]); if(!props) { if(verbosity>=2) @@ -425,7 +450,7 @@ Device *get_devices(void) printf(" %s = %s\n", props[j].name, props[j].value); } - if(can_mount(props)) + if(can_mount(props, fstab)) { char *devname; char *label; @@ -545,23 +570,54 @@ void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *colu { char buf[1024]; int pos = 0; - int status; + int status = 0; + fd_set fds; + struct timeval timeout; close(pipe_fd[1]); + FD_ZERO(&fds); + FD_SET(pipe_fd[0], &fds); + timeout.tv_sec = 0; + timeout.tv_usec = 200000; while(1) { - int len; + if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout)) + { + int len; - len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1); - if(len<=0) + len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1); + if(len<=0) + break; + pos += len; + } + else if(waitpid(pid, &status, 0)) + { + pid = 0; break; - pos += len; + } } + if(pid) + waitpid(pid, &status, 0); + buf[pos] = 0; - waitpid(pid, &status, 0); + if(verbosity>=1) + { + if(WIFEXITED(status)) + { + if(WEXITSTATUS(status)) + printf("Command exited successfully\n"); + else + printf("Command exited with status %d\n", WEXITSTATUS(status)); + } + else if(WIFSIGNALED(status)) + printf("Command terminated with signal %d\n", WTERMSIG(status)); + else + printf("Command exited with unknown result %04X\n", status); + } + if(!WIFEXITED(status) || WEXITSTATUS(status)) { GtkWidget *dialog; @@ -573,9 +629,6 @@ void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *colu else gtk_main_quit(); } - else - { - } } (void)column;