/* * daemon.c * * Copyright (C) 2006 Alex deVries * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "afpfs-ng/afp.h" #include "afpfs-ng/dsi.h" #include "afp_server.h" #include "afpfs-ng/utils.h" #include "daemon.h" #include "commands.h" #define MAX_ERROR_LEN 1024 #define STATUS_LEN 1024 #define MAX_CLIENT_RESPONSE 2048 static int debug_mode = 0; static char commandfilename[PATH_MAX]; int get_debug_mode(void) { return debug_mode; } void fuse_forced_ending_hook(void) { struct afp_server * s = get_server_base(); struct afp_volume * volume; int i; for (s=get_server_base();s;s=s->next) { if (s->connect_state==SERVER_STATE_CONNECTED) for (i=0;inum_volumes;i++) { volume=&s->volumes[i]; if (volume->mounted==AFP_VOLUME_MOUNTED) log_for_client(NULL,AFPFSD,LOG_NOTICE, "Unmounting %s\n",volume->mountpoint); afp_unmount_volume(volume); } } } int fuse_unmount_volume(struct afp_volume * volume) { if (volume->priv) { fuse_exit((struct fuse *)volume->priv); pthread_kill(volume->thread, SIGHUP); pthread_join(volume->thread,NULL); } return 0; } static int startup_listener(void) { int command_fd; struct sockaddr_un sa; int len; if ((command_fd=socket(AF_UNIX,SOCK_STREAM,0)) < 0) { goto error; } memset(&sa,0,sizeof(sa)); sa.sun_family = AF_UNIX; strcpy(sa.sun_path,commandfilename); len = sizeof(sa.sun_family) + strlen(sa.sun_path)+1; if (bind(command_fd,(struct sockaddr *)&sa,len) < 0) { perror("binding"); close(command_fd); goto error; } listen(command_fd,5); /* Just one at a time */ return command_fd; error: return -1; } void close_commands(int command_fd) { close(command_fd); unlink(commandfilename); } static void usage(void) { printf("Usage: afpfsd [OPTION]\n" " -l, --logmethod Either 'syslog' or 'stdout'" " -f, --foreground Do not fork\n" " -d, --debug Does not fork, logs to stdout\n" "Version %s\n", AFPFS_VERSION); } static int remove_other_daemon(void) { int sock; struct sockaddr_un servaddr; int len=0, ret; char incoming_buffer[MAX_CLIENT_RESPONSE]; struct timeval tv; fd_set rds; #define OUTGOING_PACKET_LEN 1 char outgoing_buffer[OUTGOING_PACKET_LEN]; if (access(commandfilename,F_OK)!=0) goto doesnotexist; /* file doesn't even exist */ if ((sock=socket(AF_UNIX,SOCK_STREAM,0))<0) { perror("Opening socket"); goto error; } memset(&servaddr,0,sizeof(servaddr)); servaddr.sun_family = AF_UNIX; strcpy(servaddr.sun_path,commandfilename); if ((connect(sock,(struct sockaddr*) &servaddr, sizeof(servaddr.sun_family) + sizeof(servaddr.sun_path))) <0) { goto dead; } /* Try writing to it */ outgoing_buffer[0]=AFP_SERVER_COMMAND_PING; if (write(sock, outgoing_buffer,OUTGOING_PACKET_LEN)