Skip to content

Commit

Permalink
server: add -S -s -h flags
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Feb 20, 2022
1 parent 24cc0a9 commit b1cefc7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
19 changes: 18 additions & 1 deletion src/rpcserver/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/socket.h>

#include "common.h"

bool g_stdout = false;
bool g_syslog = false;

void trace(const char *prefix, const char *fmt, ...)
{
if (!g_stdout && !g_syslog)
{
return;
}

char line[1022];
char prefixed_line[1024];

Expand All @@ -18,7 +27,15 @@ void trace(const char *prefix, const char *fmt, ...)
va_end(args);

sprintf(prefixed_line, "%s: %s", prefix, line);
puts(prefixed_line);

if (g_stdout)
{
puts(prefixed_line);
}
if (g_syslog)
{
syslog(LOG_DEBUG, "%s", prefixed_line);
}
}

bool recvall(int sockfd, char *buf, size_t len)
Expand Down
3 changes: 3 additions & 0 deletions src/rpcserver/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ typedef signed int s32;
typedef unsigned long u64;
typedef unsigned long s64;

bool g_stdout;
bool g_syslog;

#define TRACE(...) trace(__PRETTY_FUNCTION__, __VA_ARGS__)
#define CHECK(expression) \
if (!(expression)) \
Expand Down
20 changes: 17 additions & 3 deletions src/rpcserver/rpcserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@

#define DEFAULT_PORT ("5910")
#define DEFAULT_SHELL ("/bin/sh")
#define USAGE ("Usage: %s [-p port] [-s shell]")
#define USAGE ("Usage: %s [-p port] [-s] [-S] \n\
-h show this help message \n\
-s log to stdout \n\
-S log to syslog \n")
#define SERVER_MAGIC_VERSION (0x88888800)
#define MAGIC (0x12345678)
#define MAX_CONNECTIONS (1024)
Expand Down Expand Up @@ -690,7 +693,7 @@ int main(int argc, const char *argv[])
int opt;
char port[MAX_OPTION_LEN] = DEFAULT_PORT;

while ((opt = getopt(argc, (char *const *)argv, "p:")) != -1)
while ((opt = getopt(argc, (char *const *)argv, "p:sSh")) != -1)
{
switch (opt)
{
Expand All @@ -699,9 +702,20 @@ int main(int argc, const char *argv[])
strncpy(port, optarg, sizeof(port) - 1);
break;
}
case 's':
{
g_stdout = true;
break;
}
case 'S':
{
g_syslog = true;
break;
}
case 'h':
default: /* '?' */
{
TRACE(USAGE, argv[0]);
printf(USAGE, argv[0]);
exit(EXIT_FAILURE);
}
}
Expand Down

0 comments on commit b1cefc7

Please sign in to comment.