Commit Diff


commit - afa57f7871c009722674495177041fd7abbb39ef
commit + d0fd368ab5bf6c5efe2a0e3520052b611741d27c
blob - 0bb0365b7a90961f4da62832eeac1408cad18827
blob + c6e0558f0d0d3c0f1d2071966bd248c87931242d
--- ChangeLog
+++ ChangeLog
@@ -1,5 +1,7 @@
 2021-07-15  Omar Polo  <op@omarpolo.com>
 
+	* fs.c (lock_session): use a lockfile to prevent multiple instance of telescope to run at the same time
+
 	* defaults.c (load_default_keys): bind t to toc
 	(load_default_keys): change key for link-select: M-l
 
blob - f8fdf48a0b98a0dbde929173089ccc0f2d238099
blob + cc9ea49f9967b1e6cbb46d86713aea778dee36db
--- fs.c
+++ fs.c
@@ -51,6 +51,7 @@ static int		 fs_send_ui(int, uint32_t, int, const void
 static struct imsgev		*iev_ui;
 static FILE			*session;
 
+static char	lockfile_path[PATH_MAX];
 static char	bookmark_file[PATH_MAX];
 static char	known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
 static char	session_file[PATH_MAX];
@@ -329,6 +330,9 @@ fs_init(void)
 	strlcpy(dir, getenv("HOME"), sizeof(dir));
 	strlcat(dir, "/.telescope", sizeof(dir));
 	mkdir(dir, 0700);
+
+	strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
+	strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
 
 	strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
 	strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
@@ -372,7 +376,29 @@ fs_main(void)
 }
 
 
+
+int
+lock_session(void)
+{
+	struct flock	lock;
+	int		fd;
+
+	if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
+		return -1;
+
+	lock.l_start = 0;
+	lock.l_len = 0;
+	lock.l_type = F_WRLCK;
+	lock.l_whence = SEEK_SET;
 
+	if (fcntl(fd, F_SETLK, &lock) == -1) {
+		close(fd);
+		return -1;
+	}
+
+	return fd;
+}
+
 static int
 parse_khost_line(char *line, char *tmp[3])
 {
blob - 741428052d04e54504f98598880950332a113973
blob + eabd3a0be99236b2069bef671882a337326b23fa
--- telescope.1
+++ telescope.1
@@ -711,6 +711,10 @@ Default configuration file.
 .It Pa ~/.telescope/known_hosts
 Contains a list of host keys for all the hosts the user has visited.
 See the TOFU section for more info.
+.It Pa ~/.telescope/lock
+Lock file used to prevent multiple instance of
+.Nm
+from running at the same time.
 .It Pa ~/.telescope/session
 Contains the list of opened tabs in the last session, one per line.
 Gets written on
blob - 50d24ebd56016cacdc40a6bb41650a14c5640c8e
blob + a8fb4c950667377309b72668f3dfd052ea4a955b
--- telescope.c
+++ telescope.c
@@ -742,6 +742,7 @@ main(int argc, char * const *argv)
 	int		 ch, configtest = 0, fail = 0;
 	int		 has_url = 0;
 	int		 proc = -1;
+	int		 sessionfd;
 	char		 path[PATH_MAX];
 	const char	*url = NEW_TAB_URL;
 	const char	*argv0;
@@ -817,6 +818,11 @@ main(int argc, char * const *argv)
 		exit(0);
 	}
 
+	fs_init();
+	if ((sessionfd = lock_session()) == -1)
+		errx(1, "can't lock session, is another instance of "
+		    "telescope already running?");
+
 	/* Start children. */
 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
 		err(1, "socketpair");
@@ -835,7 +841,6 @@ main(int argc, char * const *argv)
 	setproctitle("ui");
 
 	/* initialize tofu & load certificates */
-	fs_init();
 	tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
 	load_certs(&certs);
 
@@ -866,5 +871,7 @@ main(int argc, char * const *argv)
 	ui_send_net(IMSG_QUIT, 0, NULL, 0);
 	imsg_flush(&iev_fs->ibuf);
 
+	close(sessionfd);
+
 	return 0;
 }
blob - f97dddd629617a69605a31bd5a4b8064fc20be9a
blob + 3c9eb0a2a465a7d8e4a3bfb81a62716d3eac38aa
--- telescope.h
+++ telescope.h
@@ -284,6 +284,7 @@ void		 config_apply_style(void);
 /* fs.c */
 int		 fs_init(void);
 int		 fs_main(void);
+int		 lock_session(void);
 int		 load_certs(struct ohash*);
 int		 load_last_session(void(*)(const char*));