From f5c0446975d0e1109a1c15f272f35fd7e20dbf9b Mon Sep 17 00:00:00 2001 From: james <> Date: Fri, 15 Feb 2008 19:51:30 +0000 Subject: *** empty log message *** --- src/lockfile.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lockfile.h | 15 +++++++++ src/prototypes.h | 29 ++++++++++++++--- src/serial.c | 38 +++++++++------------- 4 files changed, 151 insertions(+), 28 deletions(-) diff --git a/src/lockfile.c b/src/lockfile.c index 9c7acc0..451901c 100644 --- a/src/lockfile.c +++ b/src/lockfile.c @@ -10,6 +10,9 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.7 2008/02/15 19:51:30 james + * *** empty log message *** + * * Revision 1.6 2008/02/15 19:09:00 james * *** empty log message *** * @@ -426,6 +429,100 @@ lockfile_lock (Filelist * fl) return ret; } +void +lockfile_unlock (Filelist * fl) +{ + + while (fl->head) + { + unlink (fl->head->name); + filelist_remove (fl, fl->head); + } +} + + +/* If we have a passive lock, check noone has an */ +/* active one, returns 1 if he does, 0 if he doesnt */ +int +serial_lock_check (Serial_lock * l) +{ + Filelist_ent *fle; + int locks_found = 0; + struct stat buf; + struct timeval now, dif; + + if (l->mode == SERIAL_LOCK_ACTIVE) + return 0; + + for (fle = l->locks_to_check; fle; fle = fle->next) + { + if (!stat (fle->name, &buf)) + locks_found++; + } + + if (!locks_found) + return 0; + + getimeofday (&now, NULL); + timersub (&now, &l->last_stale_purge, &dif); + + if (dif.tv_sec > STALE_CHECK_INTERVAL) + { + lockfile_remove_stale (l->locks_to_check); + l->last_stale_purge = now; + } + + return 1; +} + +Serial_lock void +serial_lock_free (Serial_lock * l) +{ + if (!l) + return; + + if (l->locks_held) + { + lockfile_unlock (l->locks_held); + filelist_free (l->locks_held); + } + + if (l->locks_to_check) + { + filelist_free (l->locks_to_check); + } + + free (l); +} + + +Serial_lock * +serial_lock_new (char *dev, int mode) +{ + Filelist *fl = lockfile_make_list (dev); + + if (!fl) + return NULL; + + l->mode = mode; + l->locks_to_check = fl; + l->locks_held = NULL; + memset (&l->last_stale_purge, 0, sizeof (l->last_stale_purge)); + + if (mode == SERIAL_LOCK_PASSIVE) + return l; + + l->locks_held = lockfile_lock (l->locks_to_check); + if (!l->locks_held) + { + serial_lock_free (l); + return NULL; + } + + return l; +} + + #if 1 int main (int argc, char *argv[]) diff --git a/src/lockfile.h b/src/lockfile.h index c8b77f2..71cff4a 100644 --- a/src/lockfile.h +++ b/src/lockfile.h @@ -12,6 +12,9 @@ /* * $Log$ + * Revision 1.4 2008/02/15 19:51:30 james + * *** empty log message *** + * * Revision 1.3 2008/02/15 18:16:36 james * *** empty log message *** * @@ -37,4 +40,16 @@ typedef struct { Filelist_ent *head; } Filelist; + +typedef struct +{ + int mode; + int i; + struct timeval last_stale_purge; + Filelist locks_to_check; + Filelist locks_held; +} Serial_lock; + + + #endif /* __LOCKFILE_H__ */ diff --git a/src/prototypes.h b/src/prototypes.h index f528ef2..f818c88 100644 --- a/src/prototypes.h +++ b/src/prototypes.h @@ -119,6 +119,9 @@ extern int ipc_msg_send_vt102(Socket *s, VT102 *v); extern int ipc_msg_send_key(Socket *s, int key); extern int ipc_msg_send_term(Socket *s, void *buf, int len); extern int ipc_msg_send_status(Socket *s, char *buf); +extern int ipc_msg_send_setbaud(Socket *s, int baud); +extern int ipc_msg_send_sendbreak(Socket *s); +extern int ipc_msg_send_setflow(Socket *s, int flow); /* slide.c */ extern void slide_free(Slide *s); extern void slide_consume(Slide *s, int n); @@ -135,11 +138,7 @@ extern void socket_pre_select(Socket *s, fd_set *rfds, fd_set *wfds); extern int socket_post_select(Socket *s, fd_set *rfds, fd_set *wfds); extern int socket_write(Socket *s, void *buf, int len); /* serial.c */ -extern int make_lockfile(char *name); -extern void construct_lock_file_name_by_name(char *ptr); -extern void construct_lock_file_name_by_device(dev_t dev); -extern int construct_possible_lock_files(char *device); -extern TTY *serial_open(char *path); +extern TTY *serial_open(char *path, int lock_mode); /* cmd.c */ extern void cmd_parse(Cmd *c, Context *ctx, char *buf); extern void cmd_show_status(Cmd *c, Context *ctx); @@ -147,3 +146,23 @@ extern int cmd_key(Cmd *c, Context *ctx, int key); extern int cmd_activate(Cmd *c, Context *ctx); extern void cmd_new_status(Cmd *c, Context *ctx, char *msg); extern Cmd *cmd_new(void); +/* lockfile.c */ +extern Filelist *filelist_new(void); +extern void filelist_remove(Filelist *fl, Filelist_ent *fle); +extern void filelist_add(Filelist *fl, char *fn); +extern void filelist_free(Filelist *fl); +extern void filelist_print(Filelist *fl, FILE *f); +extern int lockfile_make(char *name); +extern void lockfile_add_places(Filelist *fl, char *leaf); +extern void lockfile_regularize_and_add(Filelist *fl, char *leaf); +extern void lockfile_add_name_from_path(Filelist *fl, char *file); +extern void lockfile_add_name_from_dev(Filelist *fl, dev_t dev); +extern void lockfile_check_dir_for_dev(Filelist *fl, char *dir, dev_t dev); +extern Filelist *lockfile_make_list(char *device); +extern void lockfile_remove_stale(Filelist *fl); +extern Filelist *lockfile_lock(Filelist *fl); +extern void lockfile_unlock(Filelist *fl); +extern int serial_lock_check(Serial_lock *l); +extern Serial_lock void serial_lock_free(Serial_lock *l); +extern Serial_lock *serial_lock_new(char *dev, int mode); +extern int main(int argc, char *argv[]); diff --git a/src/serial.c b/src/serial.c index dbd8206..2e67d3d 100644 --- a/src/serial.c +++ b/src/serial.c @@ -10,6 +10,9 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.6 2008/02/15 19:51:30 james + * *** empty log message *** + * * Revision 1.5 2008/02/15 19:09:00 james * *** empty log message *** * @@ -60,31 +63,14 @@ static char rcsid[] = "$Id$"; #include - -typedef struct -{ - int mode; - int i; - - struct timeval last_stale_purge; - Filelist locks_to_check; - Filelist locks_held; -} Serial_lock; - typedef struct { TTY_SIGNATURE; - Serial_lock lock; + Serial_lock *lock; int fd; } Serial; -static void -serial_check_lock (Serial * t) -{ -} - - static void serial_close (TTY * _t) { @@ -98,14 +84,14 @@ serial_close (TTY * _t) } - static int serial_read (TTY * _t, void *buf, int len) { Serial *t = (Serial *) _t; int red, done = 0; - serial_check_lock (t); + t->blocked = serial_lock_check (t->lock); + if (t->blocked) return 0; @@ -135,7 +121,7 @@ ptty_write (TTY * _t, void *buf, int len) int writ, done = 0; Serial *t = (Serial *) _t; - serial_check_lock (t); + t->blocked = serial_lock_check (t->lock); if (t->blocked) return 0; @@ -159,7 +145,7 @@ ptty_write (TTY * _t, void *buf, int len) } TTY * -serial_open (char *path) +serial_open (char *path, int lock_mode) { Serial *t; pid_t child; @@ -167,7 +153,11 @@ serial_open (char *path) struct winsize winsize = { 0 }; struct termios termios; int fd; + Serial_lock *l; + l = serial_lock_new (path, lock_mode); + if (!l) + return NULL; default_termios (&termios); @@ -177,6 +167,8 @@ serial_open (char *path) t = (Serial *) malloc (sizeof (Serial)); + t->lock = l; + strncpy (t->name, path, sizeof (t->name)); t->name[sizeof (t->name) - 1] = 0; @@ -188,7 +180,7 @@ serial_open (char *path) t->wfd = t->fd; t->size.x = VT102_COLS; t->size.y = VT102_ROWS; - t->blocked = 0; + t->blocked = serial_lock_check (t->lock); return (TTY *) t; } -- cgit v1.2.3