Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions dgamelaunch.c
Original file line number Diff line number Diff line change
Expand Up @@ -2500,10 +2500,11 @@ purge_stale_locks (int game)
while ((dent = readdir (pdir)) != NULL)
{
FILE *ipfile;
char *colon, *fn;
char *colon, *fn, *fn_in;
char buf[16];
pid_t pid;
size_t len;
struct stat pstat;
int seconds = 0;

if (!strcmp (dent->d_name, ".") || !strcmp (dent->d_name, ".."))
Expand All @@ -2515,16 +2516,36 @@ purge_stale_locks (int game)
debug_write("purge_stale_locks !colon");
graceful_exit (201);
}
if (colon - dent->d_name != strlen(me->username))
continue;
if (strncmp (dent->d_name, me->username, colon - dent->d_name))
continue;

len = strlen (dent->d_name) + strlen(dgl_format_str(game, me, myconfig[game]->inprogressdir, NULL)) + 1;
fn = malloc (len);

snprintf (fn, len, "%s%s", dgl_format_str(game, me, myconfig[game]->inprogressdir, NULL), dent->d_name);

/* skip .in files */
if (len >= 4) {
char *tmp = fn + len - 4;
if (!strcmp(tmp, ".in")) {
fn[len-4] = '\0';
printf("%s", fn);
/* unlink .in file if it's orphaned */
if (stat(fn, &pstat)) {
fn[len-4] = '.';
unlink(fn);
}
free(fn);
continue;
}
}

if (colon - dent->d_name != strlen(me->username))
continue;
if (strncmp (dent->d_name, me->username, colon - dent->d_name))
continue;

fn_in = malloc(len + 3);
snprintf (fn_in, len + 3, "%s.in", fn);

if (!(ipfile = fopen (fn, "r"))) {
debug_write("purge_stale_locks fopen inprogressdir fail");
graceful_exit (202);
Expand Down Expand Up @@ -2608,6 +2629,8 @@ purge_stale_locks (int game)
/* Don't remove the lock file until the process is dead. */
unlink (fn);
free (fn);
unlink (fn_in);
free (fn_in);
}

closedir (pdir);
Expand Down
12 changes: 9 additions & 3 deletions dgl-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ dgl_exec_cmdqueue(struct dg_cmdpart *queue, int game, struct dg_user *me)
idle_alarm_set_enabled(0);
/* launch program */
ttyrec_main (userchoice, me->username,
dgl_format_str(userchoice, me, myconfig[userchoice]->ttyrecdir, NULL),
strdup(dgl_format_str(userchoice, me, myconfig[userchoice]->ttyrecdir, NULL)),
strdup(dgl_format_str(userchoice, me, myconfig[userchoice]->inprogressdir, NULL)),
gen_ttyrec_filename());
idle_alarm_set_enabled(1);
played = 1;
Expand Down Expand Up @@ -612,7 +613,7 @@ populate_games (int xgame, int *l, struct dg_user *me)
DIR *pdir;
struct dirent *pdirent;
struct stat pstat;
char fullname[130], ttyrecname[130], pidws[80], playername[DGL_PLAYERNAMELEN+1];
char fullname[130], fullname_in[135], ttyrecname[130], pidws[80], playername[DGL_PLAYERNAMELEN+1];
char *replacestr, *dir, *p;
struct dg_game **games = NULL;
struct flock fl = { 0 };
Expand Down Expand Up @@ -641,12 +642,17 @@ populate_games (int xgame, int *l, struct dg_user *me)
char *inprog = NULL;
if (!strcmp (pdirent->d_name, ".") || !strcmp (pdirent->d_name, ".."))
continue;
if (strlen(pdirent->d_name) >= 3) {
char *tmp = pdirent->d_name + strlen(pdirent->d_name) - 3;
if (!strcmp(tmp, ".in")) continue;
}

inprog = dgl_format_str(game, me, myconfig[game]->inprogressdir, NULL);

if (!inprog) continue;

snprintf (fullname, 130, "%s%s", inprog, pdirent->d_name);
snprintf (fullname_in, 135, "%s.in", fullname);

fd = 0;
/* O_RDWR here should be O_RDONLY, but we need to test for
Expand All @@ -671,7 +677,7 @@ populate_games (int xgame, int *l, struct dg_user *me)
if (!ttrecdir) continue;
snprintf (ttyrecname, 130, "%s%s", ttrecdir, replacestr);

if (!stat (ttyrecname, &pstat))
if (!stat (fullname_in, &pstat))
{
/* now it's a valid game for sure */
games = realloc (games, sizeof (struct dg_game) * (len + 1));
Expand Down
36 changes: 30 additions & 6 deletions ttyrec.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* SUCH DAMAGE.
*/

/* 1999-02-22 Arkadiusz Mi�kiewicz <misiek@misiek.eu.org>
/* 1999-02-22 Arkadiusz Miśkiewicz <misiek@misiek.eu.org>
* - added Native Language Support
*/

Expand Down Expand Up @@ -78,10 +78,12 @@ pid_t dgl_parent;
pid_t child, subchild;
pid_t input_child;
char* ipfile = NULL;
char* inputrec_file = NULL;

volatile int wait_for_menu = 0;

FILE *fscript;
FILE *inputrec;
int master;

struct termios tt;
Expand Down Expand Up @@ -132,9 +134,9 @@ ttyrec_id(int game, char *username, char *ttyrec_filename)
}

int
ttyrec_main (int game, char *username, char *ttyrec_path, char* ttyrec_filename)
ttyrec_main (int game, char *username, char *ttyrec_path, char *inprog_path, char* ttyrec_filename)
{
char dirname[100];
char dirname[100], inputrec_path[135];

/* Note our PID to let children kill the main dgl process for idling */
dgl_parent = getpid();
Expand Down Expand Up @@ -163,7 +165,12 @@ ttyrec_main (int game, char *username, char *ttyrec_path, char* ttyrec_filename)
if (ancient_encoding == -1)
query_encoding(game, username);

if (inprog_path[strlen(inprog_path)-1] == '/')
snprintf (inputrec_path, 135, "%s%s:%s.in", inprog_path, username, ttyrec_filename);
else
snprintf (inputrec_path, 135, "%s/%s:%s.in", inprog_path, username, ttyrec_filename);
snprintf(last_ttyrec, 512, "%s", dirname);
inputrec_file = strdup(inputrec_path);

atexit(&remove_ipfile);
if ((fscript = fopen (dirname, "w")) == NULL)
Expand Down Expand Up @@ -210,8 +217,18 @@ ttyrec_main (int game, char *username, char *ttyrec_path, char* ttyrec_filename)
perror ("fork2");
fail ();
}
if (!input_child)
doinput ();
if (!input_child) {

if ((inputrec = fopen (inputrec_path, "w")) == NULL)
{
perror (inputrec);
fail ();
}
setbuf (inputrec, NULL);
doinput ();
fclose(inputrec);
unlink(inputrec_path);
}
else
{
while (wait_for_menu)
Expand All @@ -230,8 +247,10 @@ doinput ()
register int cc;
char ibuf[BUFSIZ];

while ((cc = read (0, ibuf, BUFSIZ)) > 0)
while ((cc = read (0, ibuf, BUFSIZ)) > 0) {
(void) write (master, ibuf, cc);
(void) fwrite (ibuf, 1, cc, inputrec);
}
done ();
}

Expand Down Expand Up @@ -592,6 +611,11 @@ remove_ipfile (void)
free(ipfile);
ipfile = NULL;
}
if (inputrec_file) {
unlink(inputrec_file);
free(inputrec_file);
inputrec_file = NULL;
}
signal(SIGALRM, SIG_IGN);
}

Expand Down
2 changes: 1 addition & 1 deletion ttyrec.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern void doshell (int, char *);
extern void finish (int);
extern void remove_ipfile (void);

extern int ttyrec_main (int, char *username, char *ttyrec_path, char* ttyrec_filename);
extern int ttyrec_main (int, char *username, char *ttyrec_path, char *inprog_path, char* ttyrec_filename);

extern pid_t child; /* nethack process */
extern int master, slave;
Expand Down