From 76d9462bacef6ede4317ac3865d7bb475a71d62a Mon Sep 17 00:00:00 2001 From: Marco Peereboom Date: Tue, 28 Dec 2010 19:36:48 +0000 Subject: now that we have the socket stuff add single_instance option --- xxxterm.1 | 8 ++++++++ xxxterm.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++---------- xxxterm.conf | 1 + 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/xxxterm.1 b/xxxterm.1 index 9df7049..c659845 100644 --- a/xxxterm.1 +++ b/xxxterm.1 @@ -188,6 +188,14 @@ Using the -n url option on subsequent invocations will cause the specified url to be loaded in a new tab. .Pp Only a user with identical UID and GID can use this option. +.It Cm single_instance +If set only one +.Nm +will be permitted to run. +If there is a url specified it will be opened in a new tab in the allready +running +.Nm +session. .El .Pp .Nm diff --git a/xxxterm.c b/xxxterm.c index 7a6d415..881731d 100644 --- a/xxxterm.c +++ b/xxxterm.c @@ -372,6 +372,7 @@ char *ssl_ca_file = NULL; gboolean ssl_strict_certs = FALSE; int enable_socket = 1; int append_next = 1; /* append tab after current tab */ +int single_instance = 0; /* only allow one xxxterm to run */ /* * Session IDs. @@ -793,6 +794,8 @@ config_parse(char *filename) ctrl_click_focus = atoi(val); else if (!strcmp(var, "append_next")) append_next = atoi(val); + else if (!strcmp(var, "single_instance")) + single_instance = atoi(val); else if (!strcmp(var, "read_only_cookies")) read_only_cookies = atoi(val); else if (!strcmp(var, "cookies_enabled")) @@ -4140,6 +4143,33 @@ socket_watcher(gpointer data, gint fd, GdkInputCondition cond) create_new_tab(str, 1); } +int +is_running(void) +{ + int s, len, rv = 1; + struct sockaddr_un sa; + + if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + warn("is_running: socket"); + return (-1); + } + + sa.sun_family = AF_UNIX; + snprintf(sa.sun_path, sizeof(sa.sun_path), "%s/%s/%s", + pwd->pw_dir, XT_DIR, XT_SOCKET_FILE); + len = SUN_LEN(&sa); + + /* connect to see if there is a listener */ + if (connect(s, (struct sockaddr *)&sa, len) == -1) + rv = 0; /* not running */ + else + rv = 1; /* already running */ + + close(s); + + return (rv); +} + int build_socket(void) { @@ -4225,16 +4255,6 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - if (optn) { - while (argc) { - send_url_to_socket(argv[0]); - - argc--; - argv++; - } - exit(0); - } - TAILQ_INIT(&tabs); RB_INIT(&hl); RB_INIT(&js_wl); @@ -4327,6 +4347,23 @@ main(int argc, char *argv[]) else setup_proxy(http_proxy); + /* see if there is already an xxxterm running */ + if (single_instance && is_running()) { + optn = 1; + warnx("already running"); + } + + if (optn) { + while (argc) { + send_url_to_socket(argv[0]); + + argc--; + argv++; + } + exit(0); + } + + /* go graphical */ create_canvas(); focus = restore_saved_tabs(); diff --git a/xxxterm.conf b/xxxterm.conf index c8c62d9..914e04a 100644 --- a/xxxterm.conf +++ b/xxxterm.conf @@ -16,6 +16,7 @@ enable_cookie_whitelist = 1 # ssl_ca_file = /etc/ssl/cert.pem ssl_strict_certs = 0 enable_socket = 0 +single_instance = 0 # only useful if enable_scripts = 0 enable_js_whitelist = 1 -- cgit 1.4.1-2-gfad0