From db1b78014f5c0fe956b4d4b218dc63c8782fc88e Mon Sep 17 00:00:00 2001 From: dcp1990 Date: Sun, 19 Jun 2005 04:46:15 +0000 Subject: [PATCH] Just went through an hour-long debugging session only to find that it was a fucking lock that should have been unlock in db.c Many arch. changes, probably for better, probably for worse. Who knows. --- phoned/db.c | 4 ++-- phoned/init.c | 19 ++++++++++--------- phoned/main.c | 16 +++++++++++++++- phoned/modem.c | 22 ++++++++++++++++++---- phoned/notify.c | 3 ++- phoned/signals.c | 12 +++++++----- phoned/socket.c | 19 ++++++------------- 7 files changed, 60 insertions(+), 35 deletions(-) diff --git a/phoned/db.c b/phoned/db.c index f23aede..f5c2a74 100644 --- a/phoned/db.c +++ b/phoned/db.c @@ -27,7 +27,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ -/* $Amigan: phoned/phoned/db.c,v 1.4 2005/06/19 01:17:55 dcp1990 Exp $ */ +/* $Amigan: phoned/phoned/db.c,v 1.5 2005/06/19 04:46:15 dcp1990 Exp $ */ #include #include #include @@ -160,6 +160,6 @@ short db_add_call(c, t) return 0; } sqlite3_free(sql); - pthread_mutex_lock(&dbmx); + pthread_mutex_unlock(&dbmx); return 1; } diff --git a/phoned/init.c b/phoned/init.c index 3cf93db..e5fca02 100644 --- a/phoned/init.c +++ b/phoned/init.c @@ -17,6 +17,9 @@ extern pthread_mutex_t logfmx; extern struct conf cf; extern pthread_mutex_t cfmx; extern cond_t* topcond; +extern pthread_t networkth, modemth; +extern pthread_cond_t maincond; + #define WD_LOGS 0x1 #define WD_HANDLERS 0x2 #define WD_CONFIG 0x4 @@ -26,19 +29,17 @@ extern cond_t* topcond; void shutd(whatdone) int whatdone; { - lprintf(fatal, "phoned shutting down...\n"); - pthread_mutex_lock(&cfmx); - lprintf(info, "got cf lock"); - if(whatdone & WD_MODEM) close_modem(cf.modemdev); - pthread_mutex_unlock(&cfmx); + lprintf(fatal, "phoned shutting down (th %s)...\n", pthread_equal(pthread_self(), networkth) ? "network" : (pthread_equal(pthread_self(), modemth) ? "modem" : "other/main")); + awaken_sel(); + unlink(SOCKETFILE); + if(whatdone & WD_MODEM) modem_wake(); flush_lists(); free_condition(topcond, 0x1); - pthread_mutex_lock(&logfmx); - pthread_mutex_unlock(&logfmx); if(whatdone & WD_DBINIT) db_destroy(); - lprintf(info, "got log lock"); + pthread_mutex_lock(&logfmx); if(whatdone & WD_LOGS) fclose(logf); - unlink(SOCKETFILE); + pthread_mutex_unlock(&logfmx); + pthread_cond_signal(&maincond); } int open_logs(void) diff --git a/phoned/main.c b/phoned/main.c index d3b96e0..13e61b1 100644 --- a/phoned/main.c +++ b/phoned/main.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include @@ -15,12 +17,23 @@ extern struct conf cf; extern pthread_mutex_t cfmx; extern pthread_t networkth; extern pthread_t modemth; +pthread_t mainth; +pthread_cond_t maincond = PTHREAD_COND_INITIALIZER; +pthread_mutex_t mainmx = PTHREAD_MUTEX_INITIALIZER; void usage(argv) const char* argv; { fprintf(stderr, "%s: usage: %s [-hd] [-c config] [-l log]\n", argv, argv); } - +void *mainthf(v) + void *v; +{ + if(v == 0) v = 0; + pthread_mutex_lock(&mainmx); + pthread_cond_wait(&maincond, &mainmx); + pthread_mutex_unlock(&mainmx); + exit(0); +} int main(argc, argv) int argc; char* argv[]; @@ -55,6 +68,7 @@ int main(argc, argv) initialize(); pthread_create(&networkth, NULL, network, NULL); pthread_create(&modemth, NULL, modem_io, NULL); + pthread_create(&mainth, NULL, mainthf, NULL); pthread_exit(NULL); return 0; } diff --git a/phoned/modem.c b/phoned/modem.c index 527b487..b396979 100644 --- a/phoned/modem.c +++ b/phoned/modem.c @@ -70,7 +70,7 @@ extern modem_t rockwell; void stmod(str) const char* str; { - if(pthread_mutex_trylock(&modemmx) != 0 && pthread_mutex_trylock(&miomx) == 0) { + if(pthread_mutex_trylock(&modemmx) != 0 && pthread_mutex_trylock(&miomx) != 0) { pthread_mutex_lock(&mpipemx); write(modempipes[1], "G", 1); pthread_mutex_unlock(&mpipemx); @@ -86,9 +86,15 @@ void stmod(str) } void modem_wake(void) { - pthread_mutex_lock(&mpipemx); - write(modempipes[1], "D", 1); - pthread_mutex_unlock(&mpipemx); + if(pthread_mutex_trylock(&miomx) != 0) { + pthread_mutex_lock(&mpipemx); + write(modempipes[1], "D", 1); + pthread_mutex_unlock(&mpipemx); + } else { + pthread_mutex_lock(&cfmx); + close_modem(cf.modemdev); + pthread_mutex_unlock(&cfmx); + } } void give_me_modem(str) /* warning: deprecated! */ char *str; @@ -193,6 +199,7 @@ void *modem_io(k) char cbuf[2]; if(k == 0) k = 0; *cbuf = '\0'; cbuf[1] = '\0'; + fillset(); pthread_mutex_lock(&modemmx); pthread_mutex_lock(&miomx); for(;;) { @@ -227,11 +234,18 @@ void *modem_io(k) if(FD_ISSET(modempipes[0], &fds) != 0) { read(modempipes[0], cbuf, 1); if(*cbuf == 'G') pthread_cond_wait(&mpcond, &modemmx); else { + pthread_mutex_unlock(&miomx); + pthread_mutex_unlock(&modemmx); + pthread_mutex_lock(&cfmx); + close_modem(cf.modemdev); + pthread_mutex_unlock(&cfmx); + pthread_exit(NULL); break; } } } } + if(*cbuf == 'D') break; } /* NOTREACHED */ pthread_mutex_unlock(&miomx); diff --git a/phoned/notify.c b/phoned/notify.c index ef3b23d..75dd33b 100644 --- a/phoned/notify.c +++ b/phoned/notify.c @@ -27,7 +27,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ -/* $Amigan: phoned/phoned/notify.c,v 1.6 2005/06/19 02:47:45 dcp1990 Exp $ */ +/* $Amigan: phoned/phoned/notify.c,v 1.7 2005/06/19 04:46:15 dcp1990 Exp $ */ #include #include #include @@ -79,6 +79,7 @@ void flush_lists(void) { pthread_mutex_lock(&addrmx); freeaddrl(top); + top = 0; pthread_mutex_unlock(&addrmx); } int cid_notify(cid_t* c) diff --git a/phoned/signals.c b/phoned/signals.c index aa198fb..9c38298 100644 --- a/phoned/signals.c +++ b/phoned/signals.c @@ -39,7 +39,6 @@ void handsig(sig) int sig; { if(pthread_equal(pthread_self(), networkth)) { - lprintf(info, "siamo spesa"); return; } signal(sig, handsig); @@ -47,11 +46,8 @@ void handsig(sig) case SIGINT: case SIGTERM: lprintf(fatal, "Received signal %d, cleaning up...\n", sig); - awaken_sel(); - lprintf(info, "woke up sel"); - modem_wake(); - lprintf(info, "woke up mod"); shutd(0x1 | 0x2 | 0x4 | 0x10 | 0x20); + lprintf(info, "returned from shutd()"); exit(0); break; case SIGHUP: @@ -69,3 +65,9 @@ void install_handlers(void) signal(SIGHUP, handsig); signal(SIGTERM, handsig); } +void fillset(void) +{ + sigset_t sset; + sigfillset(&sset); + pthread_sigmask(SIG_BLOCK, &sset, NULL); +} diff --git a/phoned/socket.c b/phoned/socket.c index 63c17fd..f89e614 100644 --- a/phoned/socket.c +++ b/phoned/socket.c @@ -60,9 +60,7 @@ void *handclient(k) tf = fdopen(sk, "r+"); begin_dialogue(tf, sk); fclose(tf); - lprintf(info, "here"); pthread_exit(NULL); - lprintf(info, "there"); return 0; } #if 0 @@ -88,6 +86,7 @@ void *network(b) pthread_t thr; char cbuf[2]; struct sockaddr_un it; + fillset(); cbuf[0] = '\0'; cbuf[1] = '\0'; if(b == 0) b = 0; if((s = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1) { @@ -117,10 +116,8 @@ void *network(b) FD_SET(selpipes[0], &fds); switch(select(selpipes[0] + 1, &fds, NULL, NULL, NULL)) { /* this had better be a cancellation point... */ case -1: - lprintf(error, "select: %s\n", strerror(errno)); + lprintf(error, "network select: %s\n", strerror(errno)); pthread_exit(NULL); - return (void*)0; - lprintf(error, "selet: \n"); break; case 0: /* NOTREACHED */ @@ -138,20 +135,16 @@ void *network(b) exit(-3); } pthread_create(&thr, NULL, handclient, (void*)sn); - lprintf(info, "Incoming connection\n" - ,ilen); - + lprintf(info, "Incoming connection\n"); } if(FD_ISSET(selpipes[0], &fds) != 0) { - char tbuf[2]; - lprintf(info, "woken\n"); - read(selpipes[0], tbuf, 1); - break; + read(selpipes[0], cbuf, 1); + pthread_exit(NULL); } } } + if(*cbuf != 0) break; } - lprintf(info, "still alive!"); close(s); unlink(SOCKETFILE); pthread_exit(NULL);