#include "signal.h"
#include "version.h"
+struct server_command afs_cmds[] = {DEFINE_AFS_CMD_ARRAY};
+struct server_command server_cmds[] = {DEFINE_SERVER_CMD_ARRAY};
+
/** Commands including options must be shorter than this. */
#define MAX_COMMAND_LEN 32768
return send_sb(scc, msg, ret, band, false);
}
+/**
+ * Send an error message to a client.
+ *
+ * \param cc Client info.
+ * \param err The (positive) error code.
+ *
+ * \return The return value of the underlying call to send_sb_va().
+ */
int send_strerror(struct command_context *cc, int err)
{
- return sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(err));
+ return cc->use_sideband?
+ send_sb_va(&cc->scc, SBD_ERROR_LOG, "%s\n", para_strerror(err))
+ :
+ sc_send_va_buffer(&cc->scc, "%s\n", para_strerror(err));
}
/**
return ret;
}
-int com_sender(struct command_context *cc)
+static int com_sender(struct command_context *cc)
{
- int i, ret;
+ int i, ret = 0;
char *msg = NULL;
struct sender_command_data scd;
if (cc->argc < 2) {
for (i = 0; senders[i].name; i++) {
- char *tmp = make_message("%s%s\n",
- msg? msg : "", senders[i].name);
+ char *tmp;
+ ret = xasprintf(&tmp, "%s%s\n", msg? msg : "",
+ senders[i].name);
free(msg);
msg = tmp;
}
+ if (cc->use_sideband)
+ return send_sb(&cc->scc, msg, ret, SBD_OUTPUT, false);
ret = sc_send_buffer(&cc->scc, msg);
free(msg);
return ret;
if (scd.sender_num < 0)
return ret;
msg = senders[scd.sender_num].help();
+ if (cc->use_sideband)
+ return send_sb(&cc->scc, msg, strlen(msg), SBD_OUTPUT,
+ false);
ret = sc_send_buffer(&cc->scc, msg);
free(msg);
return ret;
}
/* server info */
-int com_si(struct command_context *cc)
+static int com_si(struct command_context *cc)
{
int i, ret;
- char *ut;
- char *sender_info = NULL;
+ char *msg, *ut, *sender_info = NULL;
if (cc->argc != 1)
return -E_COMMAND_SYNTAX;
free(info);
}
ut = get_server_uptime_str(now);
- ret = sc_send_va_buffer(&cc->scc, "version: " GIT_VERSION "\n"
+ ret = xasprintf(&msg, "version: " GIT_VERSION "\n"
"up: %s\nplayed: %u\n"
"server_pid: %d\n"
"afs_pid: %d\n"
mutex_unlock(mmd_mutex);
free(ut);
free(sender_info);
+ if (cc->use_sideband)
+ return send_sb(&cc->scc, msg, ret, SBD_OUTPUT, false);
+ ret = sc_send_bin_buffer(&cc->scc, msg, ret);
+ free(msg);
return ret;
}
/* version */
-int com_version(struct command_context *cc)
+static int com_version(struct command_context *cc)
{
+ char *msg;
+ size_t len;
+
if (cc->argc != 1)
return -E_COMMAND_SYNTAX;
- return sc_send_buffer(&cc->scc, VERSION_TEXT("server")
- "built: " BUILD_DATE "\n"
- UNAME_RS ", " CC_VERSION "\n"
- );
+ msg = VERSION_TEXT("server") "built: " BUILD_DATE "\n" UNAME_RS
+ ", " CC_VERSION "\n";
+ len = strlen(msg);
+ if (cc->use_sideband)
+ return send_sb(&cc->scc, msg, len, SBD_OUTPUT, true);
+ return sc_send_bin_buffer(&cc->scc, msg, len);
}
#define EMPTY_STATUS_ITEMS \
*
* This is used by vss when currently no audio file is open.
*/
-static char *empty_status_items(int parser_friendly)
+static unsigned empty_status_items(int parser_friendly, char **result)
{
+ static char *esi;
+ static unsigned len;
+
+ if (esi)
+ goto out;
+
if (parser_friendly)
- return make_message(
+ len = xasprintf(&esi,
#define ITEM(x) "0004 %02x:\n"
EMPTY_STATUS_ITEMS
#undef ITEM
EMPTY_STATUS_ITEMS
#undef ITEM
);
- return make_message(
- #define ITEM(x) "%s:\n"
- EMPTY_STATUS_ITEMS
- #undef ITEM
- #define ITEM(x) ,status_item_list[SI_ ## x]
- EMPTY_STATUS_ITEMS
- #undef ITEM
- );
+ else
+ len = xasprintf(&esi,
+ #define ITEM(x) "%s:\n"
+ EMPTY_STATUS_ITEMS
+ #undef ITEM
+ #define ITEM(x) ,status_item_list[SI_ ## x]
+ EMPTY_STATUS_ITEMS
+ #undef ITEM
+ );
+out:
+ *result = esi;
+ return len;
}
#undef EMPTY_STATUS_ITEMS
/* stat */
-int com_stat(struct command_context *cc)
+static int com_stat(struct command_context *cc)
{
int i, ret;
struct misc_meta_data tmp, *nmmd = &tmp;
- char *s;
+ char *s, *esi = NULL;
int32_t num = 0;
int parser_friendly = 0;
for (;;) {
mmd_dup(nmmd);
ret = get_status(nmmd, parser_friendly, &s);
- ret = sc_send_bin_buffer(&cc->scc, s, ret);
- free(s);
+ if (cc->use_sideband)
+ ret = send_sb(&cc->scc, s, ret, SBD_OUTPUT, false);
+ else {
+ ret = sc_send_bin_buffer(&cc->scc, s, ret);
+ free(s);
+ }
if (ret < 0)
goto out;
if (nmmd->vss_status_flags & VSS_NEXT) {
- static char *esi;
- if (!esi)
- esi = empty_status_items(parser_friendly);
- ret = sc_send_buffer(&cc->scc, esi);
+ ret = empty_status_items(parser_friendly, &esi);
+ if (cc->use_sideband)
+ ret = send_sb(&cc->scc, esi, ret, SBD_OUTPUT,
+ true);
+ else
+ ret = sc_send_bin_buffer(&cc->scc, esi, ret);
if (ret < 0)
goto out;
} else
if (num > 0 && !--num)
goto out;
sleep(50);
+ ret = -E_SERVER_CRASH;
if (getppid() == 1)
- return -E_SERVER_CRASH;
+ goto out;
}
out:
+ free(esi);
return ret;
}
-static int send_list_of_commands(struct stream_cipher_context *scc, struct server_command *cmd,
+static int send_list_of_commands(struct command_context *cc, struct server_command *cmd,
const char *handler)
{
- int ret, i;
+ int ret;
+ char *msg = NULL;
- for (i = 1; cmd->name; cmd++, i++) {
- char *perms = cmd_perms_itohuman(cmd->perms);
- ret = sc_send_va_buffer(scc, "%s\t%s\t%s\t%s\n", cmd->name,
- handler,
- perms,
- cmd->description);
+ for (; cmd->name; cmd++) {
+ char *tmp, *perms = cmd_perms_itohuman(cmd->perms);
+ tmp = make_message("%s\t%s\t%s\t%s\n", cmd->name, handler,
+ perms, cmd->description);
free(perms);
- if (ret < 0)
- return ret;
+ msg = para_strcat(msg, tmp);
+ free(tmp);
}
- return 1;
+ if (cc->use_sideband)
+ return send_sb(&cc->scc, msg, strlen(msg), SBD_OUTPUT, false);
+ ret = sc_send_buffer(&cc->scc, msg);
+ free(msg);
+ return ret;
}
/* returns string that must be freed by the caller */
}
/* help */
-int com_help(struct command_context *cc)
+static int com_help(struct command_context *cc)
{
struct server_command *cmd;
- char *perms, *handler;
+ char *perms, *handler, *buf;
int ret;
if (cc->argc < 2) {
/* no argument given, print list of commands */
- if ((ret = send_list_of_commands(&cc->scc, server_cmds, "server")) < 0)
+ if ((ret = send_list_of_commands(cc, server_cmds, "server")) < 0)
return ret;
- return send_list_of_commands(&cc->scc, afs_cmds, "afs");
+ return send_list_of_commands(cc, afs_cmds, "afs");
}
/* argument given for help */
cmd = get_cmd_ptr(cc->argv[1], &handler);
if (!cmd)
return -E_BAD_CMD;
perms = cmd_perms_itohuman(cmd->perms);
- ret = sc_send_va_buffer(&cc->scc,
- "%s - %s\n\n"
+ ret = xasprintf(&buf, "%s - %s\n\n"
"handler: %s\n"
"permissions: %s\n"
"usage: %s\n\n"
);
free(perms);
free(handler);
+ if (cc->use_sideband)
+ return send_sb(&cc->scc, buf, ret, SBD_OUTPUT, false);
+ ret = sc_send_buffer(&cc->scc, buf);
+ free(buf);
return ret;
}
/* hup */
-int com_hup(struct command_context *cc)
+static int com_hup(struct command_context *cc)
{
if (cc->argc != 1)
return -E_COMMAND_SYNTAX;
}
/* term */
-int com_term(struct command_context *cc)
+static int com_term(struct command_context *cc)
{
if (cc->argc != 1)
return -E_COMMAND_SYNTAX;
return 1;
}
-int com_play(struct command_context *cc)
+static int com_play(struct command_context *cc)
{
if (cc->argc != 1)
return -E_COMMAND_SYNTAX;
}
/* stop */
-int com_stop(struct command_context *cc)
+static int com_stop(struct command_context *cc)
{
if (cc->argc != 1)
return -E_COMMAND_SYNTAX;
}
/* pause */
-int com_pause(struct command_context *cc)
+static int com_pause(struct command_context *cc)
{
if (cc->argc != 1)
return -E_COMMAND_SYNTAX;
}
/* next */
-int com_next(struct command_context *cc)
+static int com_next(struct command_context *cc)
{
if (cc->argc != 1)
return -E_COMMAND_SYNTAX;
}
/* nomore */
-int com_nomore(struct command_context *cc)
+static int com_nomore(struct command_context *cc)
{
if (cc->argc != 1)
return -E_COMMAND_SYNTAX;
}
/* ff */
-int com_ff(struct command_context *cc)
+static int com_ff(struct command_context *cc)
{
long promille;
int ret, backwards = 0;
}
/* jmp */
-int com_jmp(struct command_context *cc)
+static int com_jmp(struct command_context *cc)
{
long unsigned int i;
int ret;
/* send Welcome message */
ret = write_va_buffer(fd, "This is para_server, version "
PACKAGE_VERSION ".\n"
- "Features: sideband,foo\n"
+ "Features: sideband\n"
);
if (ret < 0)
goto net_err;
if (ret >= 0)
goto out;
err_out:
- send_strerror(cc, -ret);
+ if (send_strerror(cc, -ret) >= 0 && cc->use_sideband)
+ send_sb(&cc->scc, NULL, 0, SBD_EXIT__FAILURE, true);
net_err:
PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
out:
free(buf);
free(command);
- sc_free(cc->scc.recv);
- sc_free(cc->scc.send);
mutex_lock(mmd_mutex);
if (cc->cmd && (cc->cmd->perms & AFS_WRITE) && ret >= 0)
mmd->events++;
mmd->active_connections--;
mutex_unlock(mmd_mutex);
+ if (ret >= 0 && cc->use_sideband) {
+ ret = send_sb(&cc->scc, NULL, 0, SBD_EXIT__SUCCESS, true);
+ if (ret < 0)
+ PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
+ }
+ sc_free(cc->scc.recv);
+ sc_free(cc->scc.send);
exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
}