} Device;
int verbosity = 0;
+char *post_mount_command = NULL;
/**
Parses a string of the form name=value and places the components in a Property
{
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);
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);
-
- while(1)
- {
- int newline;
- int i;
- Property prop;
+ return NULL;
+ }
- 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;
- }
+ /* Parent process */
+ close(pipe_fd[1]);
- newline = -1;
- for(i=0; (newline<0 && i<pos); ++i)
- if(buf[i]=='\n')
- newline = i;
+ bufsize = 256;
+ buf = (char *)malloc(bufsize);
- if(newline<0)
- {
- if(eof)
- break;
-
- /* There was no newline in the buffer but there is more output to
- be read. Try again with a larger buffer. */
- bufsize *= 2;
- buf = (char *)realloc(buf, bufsize);
- continue;
- }
+ while(1)
+ {
+ int newline;
+ int i;
+ Property prop;
- if(parse_property(buf, newline, &prop)==0)
- {
- /* Reserve space for a sentinel value as well. */
- props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
- props[n_props] = prop;
- ++n_props;
+ if(!eof)
+ {
+ int len;
- memmove(buf, buf+newline+1, pos-newline-1);
- pos -= newline+1;
- }
- else
+ len = read(pipe_fd[0], buf+pos, bufsize-pos);
+ if(len==0)
+ eof = 1;
+ else if(len==-1)
break;
+ pos += len;
}
- free(buf);
+ newline = -1;
+ for(i=0; (newline<0 && i<pos); ++i)
+ if(buf[i]=='\n')
+ newline = i;
- if(props)
+ if(newline<0)
{
- /* Terminate the array with NULL pointers. */
- props[n_props].name = NULL;
- props[n_props].value = NULL;
+ if(eof)
+ break;
+
+ /* There was no newline in the buffer but there is more output to
+ be read. Try again with a larger buffer. */
+ bufsize *= 2;
+ buf = (char *)realloc(buf, bufsize);
+ continue;
}
- waitpid(pid, NULL, 0);
- close(pipe_fd[0]);
+ if(parse_property(buf, newline, &prop)==0)
+ {
+ /* Reserve space for a sentinel value as well. */
+ props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
+ props[n_props] = prop;
+ ++n_props;
- return props;
+ memmove(buf, buf+newline+1, pos-newline-1);
+ pos -= newline+1;
+ }
+ else
+ break;
}
- else
- {
- close(pipe_fd[0]);
- close(pipe_fd[1]);
- return NULL;
+ free(buf);
+
+ if(props)
+ {
+ /* Terminate the array with NULL pointers. */
+ props[n_props].name = NULL;
+ props[n_props].value = NULL;
}
+
+ waitpid(pid, NULL, 0);
+ close(pipe_fd[0]);
+
+ return props;
}
/**
}
/**
-Callback for activating a row in the device list. Mounts or unmounts the
-device depending on operating mode.
+Mounts a device if it was not mounted, or unmounts if it was.
*/
-void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
+int toggle_device(Device *device, char *out_buf, int out_size)
{
- GtkTreeModel *model;
- GtkTreeIter iter;
- int umount = *(int *)user_data;
+ int umount = !!device->mounted;
+ int pos = 0;
+ int status = 0;
+ fd_set fds;
+ struct timeval timeout;
+ int pid;
+ int pipe_fd[2];
- model = gtk_tree_view_get_model(list);
+ out_buf[0] = 0;
+
+ pipe(pipe_fd);
- if(gtk_tree_model_get_iter(model, &iter, path))
+ pid = fork();
+ if(pid==0)
{
- Device *device;
- int pid;
- int pipe_fd[2];
+ /* 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);
+ }
- gtk_tree_model_get(model, &iter, 1, &device, -1);
+ close(pipe_fd[0]);
+ dup2(pipe_fd[1], 1);
+ dup2(pipe_fd[1], 2);
- pipe(pipe_fd);
+ 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 -1;
+
+ /* Parent process */
- pid = fork();
- if(pid==0)
+ 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))
{
- /* 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;
+
+ len = read(pipe_fd[0], out_buf+pos, out_size-pos-1);
+ if(len<=0)
+ break;
+ pos += len;
+ }
+ else if(waitpid(pid, &status, 0))
+ {
+ pid = 0;
+ break;
+ }
+ }
- close(pipe_fd[0]);
- dup2(pipe_fd[1], 1);
- dup2(pipe_fd[1], 2);
+ close(pipe_fd[0]);
+ if(pid)
+ waitpid(pid, &status, 0);
- if(umount)
- execl("/usr/bin/pumount", "pumount", device->node, NULL);
+ out_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))
+ return -1;
- if(pid)
- waitpid(pid, &status, 0);
+ device->mounted = !device->mounted;
- buf[pos] = 0;
+ return 0;
+}
+/**
+Callback for activating a row in the device list. Mounts or unmounts the
+device depending on operating mode.
+*/
+void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
+{
+ GtkTreeModel *model;
+ GtkTreeIter iter;
+ Device *device;
+ int ret;
+ char output[1024];
+ int pid;
+
+ model = gtk_tree_view_get_model(list);
+
+ if(!gtk_tree_model_get_iter(model, &iter, path))
+ return;
+
+ gtk_tree_model_get(model, &iter, 1, &device, -1);
+ ret = toggle_device(device, output, sizeof(output));
+ if(ret)
+ {
+ 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", output);
+ g_signal_connect(dialog, "response", >k_main_quit, NULL);
+ gtk_widget_show_all(dialog);
+ return;
+ }
+
+ gtk_main_quit();
+
+ if(post_mount_command && device->mounted)
+ {
+ 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();
}
}
(void)column;
+ (void)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 <command>] [-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;
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;
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);
list = gtk_tree_view_new();
gtk_container_add(GTK_CONTAINER(viewport), list);
- g_signal_connect(list, "row-activated", G_CALLBACK(&row_activated), &umount);
+ g_signal_connect(list, "row-activated", G_CALLBACK(&row_activated), NULL);
store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));