X-Git-Url: http://git.tdb.fi/?p=pmount-gui.git;a=blobdiff_plain;f=main.c;h=ae08cbb7cda87de794b03d673e40a6de691e721e;hp=f8b4c2ef664eabe93f1c92f0fa5fb2b062f19e56;hb=a76de4d866743180e75f51088dd6d1407dcded9f;hpb=201d78e7bebc0fbf56dceab237d6ee1af8d77c6b diff --git a/main.c b/main.c index f8b4c2e..ae08cbb 100644 --- a/main.c +++ b/main.c @@ -29,6 +29,7 @@ typedef struct sDevice } Device; int verbosity = 0; +char *post_mount_command = NULL; /** Parses a string of the form name=value and places the components in a Property @@ -66,6 +67,12 @@ Property *get_device_properties(char *node) { int pid; int pipe_fd[2]; + char *buf; + int bufsize; + int pos = 0; + int eof = 0; + Property *props = NULL; + int n_props = 0; pipe(pipe_fd); @@ -82,91 +89,82 @@ Property *get_device_properties(char *node) execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL); _exit(1); } - else if(pid>0) + else if(pid<0) { - /* Parent process */ - char *buf; - int bufsize; - int pos = 0; - int eof = 0; - Property *props = NULL; - int n_props = 0; - + close(pipe_fd[0]); close(pipe_fd[1]); - bufsize = 256; - buf = (char *)malloc(bufsize); + return NULL; + } - while(1) - { - int newline; - int i; - Property prop; + /* Parent process */ + close(pipe_fd[1]); - if(!eof) - { - int len; - - len = read(pipe_fd[0], buf+pos, bufsize-pos); - if(len==0) - eof = 1; - else if(len==-1) - break; - pos += len; - } + bufsize = 256; + buf = (char *)malloc(bufsize); - newline = -1; - for(i=0; (newline<0 && i=1) + { + if(umount) + printf("Running pumount %s\n", device->node); + else + printf("Running pmount %s %s\n", device->node, device->label); + } + + close(pipe_fd[0]); + dup2(pipe_fd[1], 1); + dup2(pipe_fd[1], 2); + + if(umount) + execl("/usr/bin/pumount", "pumount", device->node, NULL); + else + execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL); + _exit(1); + } + else if(pid<0) + return; - gtk_tree_model_get(model, &iter, 1, &device, -1); + /* Parent process */ - pipe(pipe_fd); + close(pipe_fd[1]); + FD_ZERO(&fds); + FD_SET(pipe_fd[0], &fds); + timeout.tv_sec = 0; + timeout.tv_usec = 200000; - pid = fork(); - if(pid==0) + while(1) + { + /* The write fd for the pipe may be inherited by a fuse server + process and stay open indefinitely. */ + if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout)) { - /* Child process */ - if(verbosity>=1) - { - if(umount) - printf("Running pumount %s\n", device->node); - else - printf("Running pmount %s %s\n", device->node, device->label); - } + int len; - close(pipe_fd[0]); - dup2(pipe_fd[1], 1); - dup2(pipe_fd[1], 2); + 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; + } + } - if(umount) - execl("/usr/bin/pumount", "pumount", device->node, NULL); + close(pipe_fd[0]); + if(pid) + waitpid(pid, &status, 0); + + buf[pos] = 0; + + if(verbosity>=1) + { + if(WIFEXITED(status)) + { + if(WEXITSTATUS(status)) + printf("Command exited successfully\n"); else - execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL); - _exit(1); + printf("Command exited with status %d\n", WEXITSTATUS(status)); } - else if(pid>0) - { - /* Parent process */ - char buf[1024]; - int pos = 0; - 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) - { - /* The write fd for the pipe may be inherited by a fuse server - process and stay open indefinitely. */ - if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout)) - { - int len; + else if(WIFSIGNALED(status)) + printf("Command terminated with signal %d\n", WTERMSIG(status)); + else + printf("Command exited with unknown result %04X\n", status); + } - 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; - } - } + if(!WIFEXITED(status) || WEXITSTATUS(status)) + { + GtkWidget *dialog; - if(pid) - waitpid(pid, &status, 0); + /* Pmount terminated with nonzero status or a signal. Display an + error to the user. */ + dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf); + g_signal_connect(dialog, "response", >k_main_quit, NULL); + gtk_widget_show_all(dialog); + return; + } - buf[pos] = 0; + gtk_main_quit(); + if(post_mount_command && !umount) + { + char workdir[256]; + int len; + len = snprintf(workdir, sizeof(workdir), "/media/%s", device->label); + if(len<(int)sizeof(workdir)) + { 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); - } + printf("Running %s in %s\n", post_mount_command, workdir); - if(!WIFEXITED(status) || WEXITSTATUS(status)) + pid = fork(); + if(pid==0) { - GtkWidget *dialog; - - /* Pmount terminated with nonzero status or a signal. Display an - error to the user. */ - dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf); - g_signal_connect(dialog, "response", >k_main_quit, NULL); - gtk_widget_show_all(dialog); + chdir(workdir); + execlp(post_mount_command, post_mount_command, NULL); + _exit(1); } - else - gtk_main_quit(); } } @@ -770,6 +789,18 @@ gboolean key_press(GtkWidget *widget, GdkEvent *event, gpointer user_data) return FALSE; } +void show_help(void) +{ + printf("pmount-gui\n" + "Copyright (c) 2011-2015 Mikko Rasa, Mikkosoft Productions\n\n" + "Usage: pmount-gui [-v] [-u] [-r ] [-h]\n\n" + "Options:\n" + " -v Increase verbosity\n" + " -u Unmount a device (default is mount)\n" + " -r Run a command after mounting\n" + " -h Display this help\n"); +} + int main(int argc, char **argv) { GtkWidget *window; @@ -789,7 +820,7 @@ int main(int argc, char **argv) gtk_init(&argc, &argv); - while((opt = getopt(argc, argv, "vu"))!=-1) switch(opt) + while((opt = getopt(argc, argv, "vur:h"))!=-1) switch(opt) { case 'v': ++verbosity; @@ -797,6 +828,12 @@ int main(int argc, char **argv) case 'u': umount = 1; break; + case 'r': + post_mount_command = optarg; + break; + case 'h': + show_help(); + return 0; } window = gtk_window_new(GTK_WINDOW_TOPLEVEL);