diff options
author | axel <axel@liljencrantz.se> | 2005-09-23 23:10:31 +1000 |
---|---|---|
committer | axel <axel@liljencrantz.se> | 2005-09-23 23:10:31 +1000 |
commit | 77c7a026ee2ca46a114d6886d8b02ad0a96b1e3b (patch) | |
tree | b1d408a3fcb8d48d8eb192d76a4ed55ed5c3d5db /fishd.c | |
parent | f971e02256f903fa58fbc288629009d47fc89712 (diff) |
Fix for various redirection problems
darcs-hash:20050923131031-ac50b-b9e2897e7f20a087260f97d1342deaed65ad7d70.gz
Diffstat (limited to 'fishd.c')
-rw-r--r-- | fishd.c | 137 |
1 files changed, 82 insertions, 55 deletions
@@ -28,20 +28,42 @@ down and save. #include "wutil.h" #include "env_universal_common.h" - +/** + Maximum length of socket filename +*/ #ifndef UNIX_PATH_MAX #define UNIX_PATH_MAX 100 #endif -#define GREETING "#Fish universal variable daemon\n#Lines beginning with '#' are ignored\n#Syntax:\n#SET VARNAME:VALUE\n#or\n#ERASE VARNAME\n#Where VALUE is the escaped value of the variable\n#Backslash escapes and \\xxx hexadecimal style escapes are supported\n" -#define FILE ".fishd" +/** + Small greeting to show that fishd is running +*/ +#define GREETING "#Fish universal variable daemon\n" +/** + The name of the save file. The hostname is appended to this. +*/ +#define FILE ".fishd." +/** + Maximum length of hostname. Longer hostnames are truncated +*/ +#define HOSTNAME_LEN 32 + +/** + The list of connections to clients +*/ static connection_t *conn; -static int sock; +/** + The socket to accept new clients on +*/ +static int sock; -int get_socket() +/** + Connects to the fish socket +*/ +static int get_socket() { int s, len; struct sockaddr_un local; @@ -107,7 +129,10 @@ int get_socket() return s; } -void broadcast( int type, const wchar_t *key, const wchar_t *val ) +/** + Event handler. Broadcasts updates to all clients. +*/ +static void broadcast( int type, const wchar_t *key, const wchar_t *val ) { connection_t *c; message_t *msg; @@ -136,7 +161,10 @@ void broadcast( int type, const wchar_t *key, const wchar_t *val ) } } -void daemonize() +/** + Make program into a creature of the night. +*/ +static void daemonize() { /* Fork, and let parent exit @@ -178,77 +206,75 @@ void daemonize() } - -void load() +/** + Load or save all variables +*/ +void load_or_save( int save) { struct passwd *pw; char *name; char *dir = getenv( "HOME" ); + char hostname[HOSTNAME_LEN]; + connection_t c; + if( !dir ) { pw = getpwuid( getuid() ); dir = pw->pw_dir; } - - name = malloc( strlen(dir)+ strlen(FILE)+ 2 ); + + gethostname( hostname, HOSTNAME_LEN ); + + name = malloc( strlen(dir)+ strlen(FILE)+ strlen(hostname) + 2 ); strcpy( name, dir ); strcat( name, "/" ); strcat( name, FILE ); + strcat( name, hostname ); - debug( 1, L"Open file for loading: '%s'", name ); - - connection_t load; - load.fd = open( name, O_RDONLY); + debug( 1, L"Open file for %s: '%s'", + save?"saving":"loading", + name ); + c.fd = open( name, save?(O_CREAT | O_TRUNC | O_WRONLY):O_RDONLY, 0600); free( name ); - if( load.fd == -1 ) + if( c.fd == -1 ) { - debug( 0, L"Could not open save file. No previous saves?" ); + debug( 1, L"Could not open load/save file. No previous saves?" ); + wperror( L"open" ); + } - debug( 1, L"Load input file on fd %d", load.fd ); - sb_init( &load.input ); - memset (&load.wstate, '\0', sizeof (mbstate_t)); - read_message( &load ); - sb_destroy( &load.input ); - close( load.fd ); + debug( 1, L"File open on fd %d", c.fd ); + + sb_init( &c.input ); + memset (&c.wstate, '\0', sizeof (mbstate_t)); + q_init( &c.unsent ); + + if( save ) + enqueue_all( &c ); + else + read_message( &c ); + + q_destroy( &c.unsent ); + sb_destroy( &c.input ); + close( c.fd ); + } -void save() +static void load() { - struct passwd *pw; - char *name; - char *dir = getenv( "HOME" ); - if( !dir ) - { - pw = getpwuid( getuid() ); - dir = pw->pw_dir; - } - - name = malloc( strlen(dir)+ strlen(FILE)+ 2 ); - strcpy( name, dir ); - strcat( name, "/" ); - strcat( name, FILE ); - - debug( 1, L"Open file for saving: '%s'", name ); - - connection_t save; - save.fd = open( name, O_CREAT | O_TRUNC | O_WRONLY); - free( name ); + load_or_save(0); +} - if( save.fd == -1 ) - { - debug( 0, L"Could not open save file" ); - wperror( L"open" ); - exit(1); - } - debug( 1, L"File open on fd %d'", save.fd ); - q_init( &save.unsent ); - enqueue_all( &save ); - close( save.fd ); - q_destroy( &save.unsent ); + +static void save() +{ + load_or_save(1); } +/** + Do all sorts of boring initialization. +*/ static void init() { program_name=L"fishd"; @@ -268,6 +294,7 @@ static void init() load(); } + int main( int argc, char ** argv ) { int child_socket, t; @@ -362,7 +389,7 @@ int main( int argc, char ** argv ) won't lose everything on a system crash */ update_count++; - if( update_count >= 8 ) + if( update_count >= 64 ) { save(); update_count = 0; |