X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=command.c;h=3932ed2215691ea12fbbf04b7c7de9884c4cce4b;hp=c1bbba9aef86d7fccc936d2230f591ce3eb734d4;hb=faccd246cf9ee662cd8262d0db2a2375c1d4da86;hpb=7d912dbad687503f2a8d4895b0e48b4584b4940b diff --git a/command.c b/command.c index c1bbba9a..3932ed22 100644 --- a/command.c +++ b/command.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1997-2007 Andre Noll + * Copyright (C) 1997-2008 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ @@ -20,12 +20,13 @@ #include "afs.h" #include "server.h" #include "vss.h" +#include "list.h" #include "send.h" #include "rc4.h" #include "net.h" #include "daemon.h" #include "fd.h" -#include "list.h" +#include "ipc.h" #include "user_list.h" #include "server_command_list.h" #include "afs_command_list.h" @@ -37,31 +38,43 @@ static RC4_KEY rc4_recv_key; static RC4_KEY rc4_send_key; static unsigned char rc4_buf[2 * RC4_KEY_LEN]; +extern int mmd_mutex; extern struct misc_meta_data *mmd; extern struct sender senders[]; -static void dummy(__a_unused int s) -{} +static void dummy(int s) +{ + /* + * At least on Solaris, SIGUSR1 is one-shot, i.e. the signal action is + * restored to the default state once the signal handler has been + * called. + */ + if (s == SIGUSR1) + signal(SIGUSR1, dummy); +} static void mmd_dup(struct misc_meta_data *new_mmd) { - mmd_lock(); + mutex_lock(mmd_mutex); *new_mmd = *mmd; - mmd_unlock(); + mutex_unlock(mmd_mutex); } /* - * compute human readable string containing - * vss status for given integer value + * Compute human readable string containing vss status for given integer value. + * + * We don't want to use vss_playing() and friends here because we take a + * snapshot of the mmd struct and use the copy for computing the state of the + * vss. If the real data were used, we would take the mmd lock for a rather + * long time or risk to get an inconsistent view. */ static char *vss_status_tohuman(unsigned int flags) { if (flags & VSS_PLAYING) return para_strdup("playing"); - else if (flags & VSS_NEXT) + if (flags & VSS_NEXT) return para_strdup("stopped"); - else - return para_strdup("paused"); + return para_strdup("paused"); } /* @@ -108,7 +121,7 @@ static char *get_status(struct misc_meta_data *nmmd) flags = vss_get_status_flags(nmmd->vss_status_flags); if (nmmd->size) { /* parent currently has an audio file open */ localtime_r(&nmmd->mtime, &mtime_tm); - strftime(mtime, 29, "%a %b %d %Y", &mtime_tm); + strftime(mtime, 29, "%b %d %Y", &mtime_tm); } gettimeofday(&now, NULL); ret = make_message( @@ -167,12 +180,8 @@ static int check_sender_args(int argc, char * const * argv, struct sender_comman if (!subcmds[i]) return -E_COMMAND_SYNTAX; scd->cmd_num = i; - mmd_lock(); - if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) { - mmd_unlock(); + if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) return -E_SENDER_CMD; - } - mmd_unlock(); switch (scd->cmd_num) { case SENDER_ON: case SENDER_OFF: @@ -234,19 +243,19 @@ int com_sender(int fd, int argc, char * const * argv) if (scd.sender_num < 0) return ret; msg = senders[scd.sender_num].help(); - send_buffer(fd, msg); + ret = send_buffer(fd, msg); free(msg); - return 1; + return ret; } for (i = 0; i < 10; i++) { - mmd_lock(); + mutex_lock(mmd_mutex); if (mmd->sender_cmd_data.cmd_num >= 0) { - mmd_unlock(); + mutex_unlock(mmd_mutex); usleep(100 * 1000); continue; } mmd->sender_cmd_data = scd; - mmd_unlock(); + mutex_unlock(mmd_mutex); break; } return (i < 10)? 1 : -E_LOCK; @@ -261,7 +270,7 @@ int com_si(int fd, int argc, __a_unused char * const * argv) if (argc != 1) return -E_COMMAND_SYNTAX; - mmd_lock(); + mutex_lock(mmd_mutex); for (i = 0; senders[i].name; i++) { char *info = senders[i].info(); sender_info = para_strcat(sender_info, info); @@ -271,14 +280,16 @@ int com_si(int fd, int argc, __a_unused char * const * argv) } ut = uptime_str(); ret = send_va_buffer(fd, "up: %s\nplayed: %u\n" - "pid: %d\n" + "server_pid: %d\n" + "afs_pid: %d\n" "connections (active/accepted/total): %u/%u/%u\n" "current loglevel: %i\n" "supported audio formats: %s\n" "supported senders: %s\n" "%s", ut, mmd->num_played, - getppid(), + (int)getppid(), + (int)mmd->afs_pid, mmd->active_connections, mmd->num_commands, mmd->num_connects, @@ -287,7 +298,7 @@ int com_si(int fd, int argc, __a_unused char * const * argv) sender_list, sender_info ); - mmd_unlock(); + mutex_unlock(mmd_mutex); free(ut); free(sender_list); free(sender_info); @@ -301,7 +312,7 @@ int com_version(int fd, int argc, __a_unused char * const * argv) return -E_COMMAND_SYNTAX; return send_buffer(fd, VERSION_TEXT("server") "built: " BUILD_DATE "\n" - SYSTEM ", " CC_VERSION "\n" + UNAME_RS ", " CC_VERSION "\n" ); } @@ -327,7 +338,7 @@ int com_stat(int fd, int argc, char * const * argv) if (ret < 0) goto out; ret = 1; - if (num == 1) + if (num > 0 && !--num) goto out; sleep(50); if (getppid() == 1) @@ -436,10 +447,10 @@ int com_play(__a_unused int fd, int argc, __a_unused char * const * argv) { if (argc != 1) return -E_COMMAND_SYNTAX; - mmd_lock(); + mutex_lock(mmd_mutex); mmd->new_vss_status_flags |= VSS_PLAYING; mmd->new_vss_status_flags &= ~VSS_NOMORE; - mmd_unlock(); + mutex_unlock(mmd_mutex); return 1; } @@ -449,11 +460,11 @@ int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv) { if (argc != 1) return -E_COMMAND_SYNTAX; - mmd_lock(); + mutex_lock(mmd_mutex); mmd->new_vss_status_flags &= ~VSS_PLAYING; mmd->new_vss_status_flags &= ~VSS_REPOS; mmd->new_vss_status_flags |= VSS_NEXT; - mmd_unlock(); + mutex_unlock(mmd_mutex); return 1; } @@ -462,13 +473,13 @@ int com_pause(__a_unused int fd, int argc, __a_unused char * const * argv) { if (argc != 1) return -E_COMMAND_SYNTAX; - mmd_lock(); + mutex_lock(mmd_mutex); if (!vss_paused() && !vss_stopped()) { mmd->events++; mmd->new_vss_status_flags &= ~VSS_PLAYING; mmd->new_vss_status_flags &= ~VSS_NEXT; } - mmd_unlock(); + mutex_unlock(mmd_mutex); return 1; } @@ -477,10 +488,10 @@ int com_next(__a_unused int fd, int argc, __a_unused char * const * argv) { if (argc != 1) return -E_COMMAND_SYNTAX; - mmd_lock(); + mutex_lock(mmd_mutex); mmd->events++; mmd->new_vss_status_flags |= VSS_NEXT; - mmd_unlock(); + mutex_unlock(mmd_mutex); return 1; } @@ -489,10 +500,10 @@ int com_nomore(__a_unused int fd, int argc, __a_unused char * const * argv) { if (argc != 1) return -E_COMMAND_SYNTAX; - mmd_lock(); + mutex_lock(mmd_mutex); if (vss_playing() || vss_paused()) mmd->new_vss_status_flags |= VSS_NOMORE; - mmd_unlock(); + mutex_unlock(mmd_mutex); return 1; } @@ -510,7 +521,7 @@ int com_ff(__a_unused int fd, int argc, char * const * argv) return -E_COMMAND_SYNTAX; if (ret > 1 && c == '-') backwards = 1; /* jmp backwards */ - mmd_lock(); + mutex_lock(mmd_mutex); ret = -E_NO_AUDIO_FILE; if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total) goto out; @@ -531,7 +542,7 @@ int com_ff(__a_unused int fd, int argc, char * const * argv) mmd->events++; ret = 1; out: - mmd_unlock(); + mutex_unlock(mmd_mutex); return ret; } @@ -545,7 +556,7 @@ int com_jmp(__a_unused int fd, int argc, char * const * argv) return -E_COMMAND_SYNTAX; if (sscanf(argv[1], "%lu", &i) <= 0) return -E_COMMAND_SYNTAX; - mmd_lock(); + mutex_lock(mmd_mutex); ret = -E_NO_AUDIO_FILE; if (!mmd->afd.afhi.chunks_total) goto out; @@ -560,7 +571,7 @@ int com_jmp(__a_unused int fd, int argc, char * const * argv) ret = 1; mmd->events++; out: - mmd_unlock(); + mutex_unlock(mmd_mutex); return ret; } @@ -570,7 +581,7 @@ out: */ static int check_perms(unsigned int perms, struct server_command *cmd_ptr) { - PARA_DEBUG_LOG("%s", "checking permissions\n"); + PARA_DEBUG_LOG("checking permissions\n"); return (cmd_ptr->perms & perms) < cmd_ptr->perms ? -E_PERM : 0; } @@ -652,12 +663,10 @@ out: } /** - * perform user authentication and execute a command - * - * \param fd the file descriptor to send output to - * \param addr socket address info of peer + * Perform user authentication and execute a command. * - * \return EXIT_SUCCESS or EXIT_FAILURE + * \param fd The file descriptor to send output to. + * \param peername Identifies the connecting peer. * * Whenever para_server accepts an incoming tcp connection on * the port it listens on, it forks and the resulting child @@ -680,7 +689,7 @@ out: * * \sa alarm(2), rc4(3), crypt.c, crypt.h */ -int handle_connect(int fd, struct sockaddr_in *addr) +__noreturn void handle_connect(int fd, const char *peername) { int ret, argc, use_rc4 = 0; char buf[4096]; @@ -696,8 +705,11 @@ int handle_connect(int fd, struct sockaddr_in *addr) signal(SIGINT, SIG_DFL); signal(SIGTERM, SIG_DFL); signal(SIGHUP, SIG_DFL); - signal(SIGUSR1, SIG_IGN); + /* we need a blocking fd here as recv() might return EAGAIN otherwise. */ + ret = mark_fd_blocking(fd); + if (ret < 0) + goto err_out; challenge_nr = random(); /* send Welcome message */ ret = send_va_buffer(fd, "This is para_server, version " @@ -737,15 +749,15 @@ int handle_connect(int fd, struct sockaddr_in *addr) /* We can't use send_buffer here since buf may contain null bytes */ ret = send_bin_buffer(fd,(char *) crypt_buf, numbytes); if (ret < 0) - goto err_out; + goto net_err; /* recv decrypted number */ ret = recv_buffer(fd, buf, sizeof(buf)); if (ret < 0) - goto err_out; + goto net_err; numbytes = ret; ret = -E_AUTH; if (!numbytes) - goto err_out; + goto net_err; if (sscanf(buf, CHALLENGE_RESPONSE_MSG "%lu", &chall_response) < 1 || chall_response != challenge_nr) goto err_out; @@ -763,12 +775,14 @@ int handle_connect(int fd, struct sockaddr_in *addr) numbytes = strlen(buf); ret = send_bin_buffer(fd, buf, numbytes); if (ret < 0) - goto err_out; + goto net_err; if (use_rc4) enable_crypt(fd, rc4_recv, rc4_send, NULL); ret = read_command(fd, &command); - if (ret < 0) + if (ret == -E_COMMAND_SYNTAX) goto err_out; + if (ret < 0) + goto net_err; ret = -E_BAD_CMD; cmd = parse_cmd(command); if (!cmd) @@ -780,28 +794,25 @@ int handle_connect(int fd, struct sockaddr_in *addr) /* valid command and sufficient perms */ alarm(0); argc = split_args(command, &argv, "\n"); - mmd_lock(); + mutex_lock(mmd_mutex); mmd->num_commands++; - mmd_unlock(); + mutex_unlock(mmd_mutex); PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name, - inet_ntoa(addr->sin_addr)); + peername); ret = cmd->handler(fd, argc, argv); - if (ret >= 0) { - ret = EXIT_SUCCESS; + if (ret >= 0) goto out; - } err_out: - PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret)); - if (ret != -E_SEND && ret != -E_RECV) - send_va_buffer(fd, "%s\n", PARA_STRERROR(-ret)); - ret = EXIT_FAILURE; + send_va_buffer(fd, "%s\n", para_strerror(-ret)); +net_err: + PARA_NOTICE_LOG("%s\n", para_strerror(-ret)); out: free(command); free(argv); - mmd_lock(); + mutex_lock(mmd_mutex); if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0) mmd->events++; mmd->active_connections--; - mmd_unlock(); - return ret; + mutex_unlock(mmd_mutex); + exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS); }