From 451b522dfd85e087ef74205abbcf607a3290e22e Mon Sep 17 00:00:00 2001 From: Ron Pedde Date: Tue, 6 Apr 2004 13:44:26 +0000 Subject: [PATCH] fix db_init after dropping privs --- src/db-gdbm.c | 38 +++++++++++++++++++++++++------------- src/db-memory.c | 15 +++++++++++++-- src/db-memory.h | 3 ++- src/main.c | 11 +++++++++-- 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/db-gdbm.c b/src/db-gdbm.c index 55d17e11..e0c47c92 100644 --- a/src/db-gdbm.c +++ b/src/db-gdbm.c @@ -182,19 +182,40 @@ void db_init_once(void) { pthread_rwlock_init(&db_rwlock,NULL); } +/* + * db_open + * + * Open the database, so we can drop privs + */ +int db_open(char *parameters) { + char db_path[PATH_MAX + 1]; + + if(pthread_once(&db_initlock,db_init_once)) + return -1; + + snprintf(db_path,sizeof(db_path),"%s/%s",parameters,"songs.gdb"); + db_songs=gdbm_open(db_path,0,GDBM_WRCREAT | GDBM_SYNC | GDBM_NOLOCK, + 0600,NULL); + if(!db_songs) { + DPRINTF(ERR_FATAL,"Could not open songs database (%s)\n", + db_path); + return -1; + } + + return 0; +} + + /* * db_init * * Initialize the database. Parameter is the directory * of the database files */ -int db_init(char *parameters) { +int db_init(void) { MP3FILE mp3file; datum tmp_key,tmp_nextkey,song_data; - char db_path[PATH_MAX + 1]; - if(pthread_once(&db_initlock,db_init_once)) - return -1; if((db_removed=rbinit(db_compare_rb_nodes,NULL)) == NULL) { errno=ENOMEM; @@ -203,15 +224,6 @@ int db_init(char *parameters) { pl_register(); - snprintf(db_path,sizeof(db_path),"%s/%s",parameters,"songs.gdb"); - db_songs=gdbm_open(db_path,0,GDBM_WRCREAT | GDBM_SYNC | GDBM_NOLOCK, - 0600,NULL); - if(!db_songs) { - DPRINTF(ERR_FATAL,"Could not open songs database (%s)\n", - gdbm_strerror(errno)); - return -1; - } - db_version_no=1; db_song_count=0; diff --git a/src/db-memory.c b/src/db-memory.c index c034f291..f0c71d9c 100644 --- a/src/db-memory.c +++ b/src/db-memory.c @@ -75,7 +75,8 @@ pthread_once_t db_initlock=PTHREAD_ONCE_INIT; int db_start_initial_update(void); int db_end_initial_update(void); int db_is_empty(void); -int db_init(char *parameters); +int db_open(char *parameters); +int db_init(); int db_deinit(void); int db_version(void); int db_add(MP3FILE *mp3file); @@ -114,13 +115,23 @@ void db_init_once(void) { pthread_rwlock_init(&db_rwlock,NULL); } + +/* + * db_open + * + * We'll wait for db_init + */ +void db_open(char *parameters) { + return 0; +} + /* * db_init * * Initialize the database. For the in-memory db * the parameters are insignificant */ -int db_init(char *parameters) { +int db_init(void) { db_root.next=NULL; db_version_no=1; db_song_count=0; diff --git a/src/db-memory.h b/src/db-memory.h index 678d07b5..3b9f5920 100644 --- a/src/db-memory.h +++ b/src/db-memory.h @@ -29,7 +29,8 @@ typedef void* ENUMHANDLE; extern int db_start_initial_update(void); extern int db_end_initial_update(void); extern int db_is_empty(void); -extern int db_init(char *parameters); +extern int db_open(char *parameters); +extern int db_init(void); extern int db_deinit(void); extern int db_version(void); extern int db_add(MP3FILE *mp3file); diff --git a/src/main.c b/src/main.c index e8f00806..d383e3ed 100644 --- a/src/main.c +++ b/src/main.c @@ -472,13 +472,20 @@ int main(int argc, char *argv[]) { } if((config.use_mdns) && (!parseonly)) { - fprintf(stderr,"Starting rendezvous daemon\n"); + fprintf(stderr,"Starting rendezvous daemon -- indexing " + "mp3 files... wait.\n"); if(rend_init(config.runas)) { perror("rend_init"); exit(EXIT_FAILURE); } } + + if(db_open(config.db_dir)) { + perror("db_open"); + exit(EXIT_FAILURE); + } + // Drop privs here if(drop_privs(config.runas)) { perror("drop_privs"); @@ -501,7 +508,7 @@ int main(int argc, char *argv[]) { } /* Initialize the database before starting */ - if(db_init(config.dbdir)) { + if(db_init()) { perror("db_init"); exit(EXIT_FAILURE); }