Open ckeen opened 6 years ago
On the remotehost are also leftover temporary files which should be deleted on clean exit.
The following patch removes the lock file after the socket is closed. This is especially necessary if you edit with root permissions (-s "sudo sam"), after that you cannot run sam as normal user anymore until the lock file is manually removed.
diff -urN sam/sam/io.c sam/sam/io.c
--- sam/sam/io.c 2019-09-01 10:59:12.538944194 +0200
+++ sam/sam/io.c 2019-09-01 10:23:17.058306863 +0200
@@ -269,20 +269,25 @@
close(p2[0]);
}
+char lockpath[FILENAME_MAX + 1] = {0};
+int lockfd = -1;
+
void
removesocket(void)
{
close(exfd);
unlink(exname);
exname[0] = 0;
+
+ close(lockfd);
+ unlink(lockpath);
+ lockpath[0] = 0;
}
bool
canlocksocket(const char *machine)
{
- int fd = -1;
const char *path = getenv("SAMSOCKPATH")? getenv("SAMSOCKPATH") : getenv("HOME");
- char lockpath[FILENAME_MAX + 1] = {0};
if (!path){
fputs("could not determine command socket path\n", stderr);
@@ -290,12 +295,12 @@
}
snprintf(lockpath, PATH_MAX, "%s/.sam.%s.lock", path, machine? machine : "localhost");
- fd = open(lockpath, O_CREAT | O_RDWR, 0644);
- if (fd < 0)
+ lockfd = open(lockpath, O_CREAT | O_RDWR, 0644);
+ if (lockfd < 0)
return false;
- if (lockf(fd, F_TLOCK, 0) != 0)
- return close(fd), false;
+ if (lockf(lockfd, F_TLOCK, 0) != 0)
+ return close(lockfd), false;
return true;
}
These patches are additionally required to remove the lock files for remote editing.
diff -urN sam/sam/sam.c sam/sam/sam.c
--- sam/sam/sam.c 2019-09-02 09:16:30.512755295 +0200
+++ sam/sam/sam.c 2019-09-02 16:33:27.815468741 +0200
@@ -154,8 +154,14 @@
void
rmsocket(void)
{
- if (rmsocketname)
+ if (rmsocketname){
unlink(rmsocketname);
+
+ char lockpath[FILENAME_MAX + 1] = {0};
+ const char *path = getenv("SAMSOCKPATH")? getenv("SAMSOCKPATH") : getenv("HOME");
+ snprintf(lockpath, PATH_MAX, "%s/.sam.localhost.lock", path);
+ unlink(lockpath);
+ }
}
int
diff -urN sam/samterm/main.c sam/samterm/main.c
--- sam/samterm/main.c 2019-09-02 09:16:30.512755295 +0200
+++ sam/samterm/main.c 2019-09-02 16:33:37.451801775 +0200
@@ -36,8 +36,14 @@
void
removeext(void)
{
- if (exname)
+ if (exname){
unlink(exname);
+
+ char lockpath[FILENAME_MAX + 1] = {0};
+ const char *path = getenv("SAMSOCKPATH")? getenv("SAMSOCKPATH") : getenv("HOME");
+ snprintf(lockpath, PATH_MAX, "%s/.sam.%s.lock", path, machine);
+ unlink(lockpath);
+ }
}
int
I start sam with sam -r remotehost, then do editing (or not), quit with 'q' and .sam.remotehost.lock is still in my home. Shouldn't that get cleaned up?