/* ************************************************************* */
void
-gen_inprogress_lock ()
+gen_inprogress_lock (pid_t pid)
{
- char lockfile[130];
+ char lockfile[130], pidbuf[16];
int fd;
struct flock fl = { 0 };
+ snprintf(pidbuf, 16, "%.15d", pid);
+
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fd = open (lockfile, O_WRONLY | O_CREAT, 0644);
if (fcntl (fd, F_SETLKW, &fl) == -1)
graceful_exit (68);
+
+ write(fd, pidbuf, strlen(pidbuf));
}
/* ************************************************************* */
/* ************************************************************* */
/* ************************************************************* */
+void
+purge_stale_locks (void)
+{
+ DIR* pdir;
+ struct dirent *dent;
+
+ if (!(pdir = opendir(LOC_INPROGRESSDIR)))
+ graceful_exit(200);
+
+ while ((dent = readdir(pdir)) != NULL)
+ {
+ FILE* ipfile;
+ char* colon;
+ char buf[16];
+ pid_t pid;
+
+ colon = strchr(dent->d_name, ':');
+ /* should never happen */
+ if (!colon)
+ graceful_exit(201);
+
+ if (strncmp(dent->d_name, me->username, colon - dent->d_name))
+ continue;
+
+ if (!(ipfile = fopen(dent->d_name, "r")))
+ graceful_exit(202);
+
+ if (fgets(buf, 16, ipfile) == NULL)
+ graceful_exit(203);
+
+ fclose(ipfile);
+ unlink(dent->d_name);
+
+ pid = atoi(buf);
+
+ kill(pid, SIGHUP);
+ }
+
+ closedir(pdir);
+}
+
int
main (void)
{
endwin ();
+ purge_stale_locks();
+
/* environment */
snprintf (atrcfilename, 81, "@%s", rcfilename);
/* lock */
gen_ttyrec_filename ();
- gen_inprogress_lock ();
/* launch program */
ttyrec_main (me->username);
#ifndef __DGAMELAUNCH_H
#define __DGAMELAUNCH_H
+#include <sys/types.h>
#include <time.h>
/* Default - should work everywhere */
/* dgamelaunch.c function prototypes */
extern void ttyrec_getmaster (void);
extern void gen_ttyrec_filename (void);
-extern void gen_inprogress_lock (void);
+extern void gen_inprogress_lock (pid_t pid);
extern void catch_sighup (int signum);
extern void loadbanner (struct dg_banner *ban);
extern void drawbanner (unsigned int start_line, unsigned int howmany);