summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Straz <nstraz@redhat.com>2013-09-19 10:10:16 -0400
committerNathan Straz <nstraz@redhat.com>2013-09-19 10:26:32 -0400
commit8bc29add2abf434390b3e6696ff7b364d658f028 (patch)
treefaa927113db114a7d1ed4c03f9196def61692a95
parent486049502c9156e1309a81651f014f2d4fd4450e (diff)
downloadqarsh-8bc29add2abf434390b3e6696ff7b364d658f028.tar.gz
qarsh-8bc29add2abf434390b3e6696ff7b364d658f028.tar.xz
qarsh-8bc29add2abf434390b3e6696ff7b364d658f028.zip
Creat a thin logging layer
When qarshd is run via xinetd, stderr still goes out the socket and messages from sockutil.c or qarsh_packet.c can interfere with the protocol. Create a thin wrapper which qacp and qarsh can send to stderr and qarshd can send to syslog.
-rw-r--r--qacp.c12
-rw-r--r--qarsh.c15
-rw-r--r--qarsh_packet.c47
-rw-r--r--qarshd.c14
-rw-r--r--sockutil.c11
5 files changed, 70 insertions, 29 deletions
diff --git a/qacp.c b/qacp.c
index b8b006a..4e9c95d 100644
--- a/qacp.c
+++ b/qacp.c
@@ -40,6 +40,7 @@
#include <pwd.h>
#include <sys/sendfile.h>
#include <libgen.h>
+#include <stdarg.h>
#include "sockutil.h"
@@ -53,6 +54,17 @@ unsigned short qarsh_ss_family; /* AF_INET/AF_INET6, set on connect */
short quiet = 0;
void
+lprintf(int priority, const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vfprintf(stderr, format, ap);
+ va_end(ap);
+}
+
+
+void
usage()
{
/* printf("usage: qacp: [-r] [[user@]host1:]file1 [...] " */
diff --git a/qarsh.c b/qarsh.c
index 49210db..51a876b 100644
--- a/qarsh.c
+++ b/qarsh.c
@@ -35,9 +35,9 @@
#include <sys/select.h>
#include <netinet/in.h>
#include <netdb.h>
-#include <syslog.h>
#include <pwd.h>
#include <time.h>
+#include <stdarg.h>
#include "sockutil.h"
#include "qarsh_packet.h"
@@ -58,6 +58,17 @@ int sigs_to_propogate[] = { SIGINT, SIGTERM, SIGHUP, SIGUSR1, SIGUSR2 };
sigset_t pselect_sigmask;
int connection_timeout = 0;
+
+void
+lprintf(int priority, const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vfprintf(stderr, format, ap);
+ va_end(ap);
+}
+
void
usage()
{
@@ -430,8 +441,6 @@ main(int argc, char *argv[])
char *cp;
int max_timeout = 120;
- openlog("qarsh", LOG_PID, LOG_DAEMON);
-
if ((cp = getenv("QARSH_TIMEOUT")) != NULL) {
max_timeout = atoi(cp);
}
diff --git a/qarsh_packet.c b/qarsh_packet.c
index d6a45a1..24f4661 100644
--- a/qarsh_packet.c
+++ b/qarsh_packet.c
@@ -37,6 +37,9 @@
#include "qarsh_packet.h"
+/* Logging provided by qarshd, qarsh, or qacp */
+extern void lprintf(int priority, const char *format, ...);
+
/* Prototypes */
void parse_qp_hello(char *buf, int *buflen, struct qa_packet *qp);
void parse_qp_returncode(char *buf, int *buflen, struct qa_packet *qp);
@@ -205,7 +208,7 @@ fetch_int(char *buf, int *buflen, int *out)
{
int i;
if (*buflen < sizeof i) {
- fprintf(stderr, "Not enough data to unpack a uint32_t, %d\n", *buflen);
+ lprintf(0, "Not enough data to unpack a uint32_t, %d\n", *buflen);
return buf;
}
memcpy(&i, buf, sizeof i);
@@ -219,13 +222,13 @@ fetch_string(char *buf, int *buflen, char **out)
{
int slen;
if (*buflen < sizeof slen) {
- fprintf(stderr, "Not enough data to unpack string length, %d\n", *buflen);
+ lprintf(0, "Not enough data to unpack string length, %d\n", *buflen);
*out = NULL;
return buf;
}
buf = fetch_int(buf, buflen, &slen);
if (slen > *buflen) {
- fprintf(stderr, "String is more than available data, %d > %d\n", slen, *buflen);
+ lprintf(0, "String is more than available data, %d > %d\n", slen, *buflen);
*out = NULL;
return buf;
}
@@ -242,7 +245,7 @@ fetch_off_t(char *buf, int *buflen, off_t *out)
{
off_t i;
if (*buflen < sizeof i) {
- fprintf(stderr, "Not enough data to unpack a off_t, %d\n", *buflen);
+ lprintf(0, "Not enough data to unpack a off_t, %d\n", *buflen);
return buf;
}
memcpy(&i, buf, sizeof i);
@@ -339,7 +342,7 @@ parse_qp_data(char *buf, int *buflen, struct qa_packet *qp)
buf = fetch_off_t(buf, buflen, &(qp->qp_data.qp_offset));
buf = fetch_int(buf, buflen, (int *)&(qp->qp_data.qp_count));
if (qp->qp_data.qp_count > *buflen) {
- fprintf(stderr, "Blob is larger than rest of packet, %d > %d\n",
+ lprintf(0, "Blob is larger than rest of packet, %d > %d\n",
qp->qp_data.qp_count, *buflen);
free(qp);
} else {
@@ -371,11 +374,11 @@ parse_packet(char *buf, int buflen)
if (qa_pi[qp->qp_type].pi_parse)
qa_pi[qp->qp_type].pi_parse(buf, &buflen, qp);
else {
- fprintf(stderr, "Packet type %d not implemented yet\n", qp->qp_type);
+ lprintf(0, "Packet type %d not implemented yet\n", qp->qp_type);
free(qp);
qp = NULL;
}
- if (buflen) { fprintf(stderr, "%d remaining bytes of data in packet\n", buflen); }
+ if (buflen) { lprintf(0, "%d remaining bytes of data in packet\n", buflen); }
return qp;
}
@@ -797,51 +800,51 @@ qpfree(struct qa_packet *qp)
void
dump_qp_ack(struct qa_packet *qp)
{
- fprintf(stderr, "%s #%d", QP_NAME(qp->qp_ack.qp_ack_type),
+ lprintf(0, "%s #%d", QP_NAME(qp->qp_ack.qp_ack_type),
qp->qp_ack.qp_ack_seq);
}
void
dump_qp_runcmd(struct qa_packet *qp)
{
- fprintf(stderr, "cmdline: %s", qp->qp_runcmd.qp_cmdline);
+ lprintf(0, "cmdline: %s", qp->qp_runcmd.qp_cmdline);
}
void
dump_qp_returncode(struct qa_packet *qp)
{
- fprintf(stderr, "rc: %d", qp->qp_returncode.qp_rc);
+ lprintf(0, "rc: %d", qp->qp_returncode.qp_rc);
}
void
dump_qp_cmdexit(struct qa_packet *qp)
{
if (WIFEXITED(qp->qp_cmdexit.qp_status)) {
- fprintf(stderr, "exited: %d", WEXITSTATUS(qp->qp_cmdexit.qp_status));
+ lprintf(0, "exited: %d", WEXITSTATUS(qp->qp_cmdexit.qp_status));
} else if (WIFSIGNALED(qp->qp_cmdexit.qp_status)) {
- fprintf(stderr, "signaled: %d", WTERMSIG(qp->qp_cmdexit.qp_status));
+ lprintf(0, "signaled: %d", WTERMSIG(qp->qp_cmdexit.qp_status));
} else {
- fprintf(stderr, "status: %d", qp->qp_cmdexit.qp_status);
+ lprintf(0, "status: %d", qp->qp_cmdexit.qp_status);
}
}
void
dump_qp_setuser(struct qa_packet *qp)
{
- fprintf(stderr, "user: %s group: %s",
+ lprintf(0, "user: %s group: %s",
qp->qp_setuser.qp_user, qp->qp_setuser.qp_group);
}
void
dump_qp_kill(struct qa_packet *qp)
{
- fprintf(stderr, "sig: %d", qp->qp_kill.qp_sig);
+ lprintf(0, "sig: %d", qp->qp_kill.qp_sig);
}
void
dump_qp_recvfile(struct qa_packet *qp)
{
- fprintf(stderr, "path: %s mode: %o count: %lld",
+ lprintf(0, "path: %s mode: %o count: %lld",
qp->qp_recvfile.qp_path, qp->qp_recvfile.qp_mode,
(long long int)qp->qp_recvfile.qp_count);
}
@@ -849,14 +852,14 @@ dump_qp_recvfile(struct qa_packet *qp)
void
dump_qp_sendfile(struct qa_packet *qp)
{
- fprintf(stderr, "path: %s",
+ lprintf(0, "path: %s",
qp->qp_sendfile.qp_path);
}
void
dump_qp_rstat(struct qa_packet *qp)
{
- fprintf(stderr, "path: %s st_mode: %o st_uid: %d st_gid: %d st_size: %lld",
+ lprintf(0, "path: %s st_mode: %o st_uid: %d st_gid: %d st_size: %lld",
qp->qp_rstat.qp_path, qp->qp_rstat.qp_st_mode,
qp->qp_rstat.qp_st_uid, qp->qp_rstat.qp_st_gid,
(long long int)qp->qp_rstat.qp_st_size);
@@ -865,24 +868,24 @@ dump_qp_rstat(struct qa_packet *qp)
void
dump_qp_data(struct qa_packet *qp)
{
- fprintf(stderr, "remfd: %d offset: %lld count: %d",
+ lprintf(0, "remfd: %d offset: %lld count: %d",
qp->qp_data.qp_remfd, (long long int)qp->qp_data.qp_offset, qp->qp_data.qp_count);
}
void
dump_qp_data_allow(struct qa_packet *qp)
{
- fprintf(stderr, "remfd: %d count: %d",
+ lprintf(0, "remfd: %d count: %d",
qp->qp_dallow.qp_remfd, qp->qp_dallow.qp_count);
}
void
dump_qp(struct qa_packet *qp)
{
- fprintf(stderr, "#%d %s ", qp->qp_seq, QP_NAME(qp->qp_type));
+ lprintf(0, "#%d %s ", qp->qp_seq, QP_NAME(qp->qp_type));
if (qa_pi[qp->qp_type].pi_dump) {
qa_pi[qp->qp_type].pi_dump(qp);
}
- fprintf(stderr, "\n");
+ lprintf(0, "\n");
fflush(stderr);
}
diff --git a/qarshd.c b/qarshd.c
index daead85..ef0fc15 100644
--- a/qarshd.c
+++ b/qarshd.c
@@ -36,6 +36,7 @@
#include <netdb.h>
#include <pwd.h>
#include <grp.h>
+#include <stdarg.h>
#include "sockutil.h"
@@ -64,6 +65,19 @@ off_t received = 0;
char *saved_path = NULL;
struct stat saved_stat;
+void
+lprintf(int priority, const char *format, ...)
+{
+ char buf[4096];
+ va_list ap;
+
+ va_start(ap, format);
+ vsnprintf(buf, 4096, format, ap);
+ va_end(ap);
+
+ syslog(priority, "%s", buf);
+}
+
int
setup_user(char *user, char *group)
{
diff --git a/sockutil.c b/sockutil.c
index 4020ed4..9d2da1d 100644
--- a/sockutil.c
+++ b/sockutil.c
@@ -32,6 +32,9 @@
#include "qarsh_packet.h"
+/* Logging provided by qarshd, qarsh, or qacp */
+extern void lprintf(int priority, const char *format, ...);
+
static int packet_seq = 1;
/* Some generic socket related functions to make things easier */
@@ -53,7 +56,7 @@ connect_to_host(char *hostname, int port, unsigned short *ss_family)
snprintf(portstr, NI_MAXSERV, "%d", port);
if ((err = getaddrinfo(hostname, portstr, &hints, &ail)) != 0) {
- fprintf(stderr, "Could not resolve hostname %s: %s\n",
+ lprintf(0, "Could not resolve hostname %s: %s\n",
hostname, gai_strerror(err));
return -1;
}
@@ -112,7 +115,7 @@ recv_packet(int fd)
do {
if ((ret = read(fd, (char *)psbuf+bufused, sizeof packetsize - bufused)) < 0) {
- fprintf(stderr, "Read error while reading packet size: %s\n", strerror(errno));
+ lprintf(0, "Read error while reading packet size: %s\n", strerror(errno));
return NULL;
} else if (ret == 0) {
return NULL;
@@ -122,14 +125,14 @@ recv_packet(int fd)
packetsize = ntohl(packetsize);
if (packetsize > QARSH_MAX_PACKET_SIZE) {
- fprintf(stderr, "Packet size too large, %d > %d\n", packetsize, QARSH_MAX_PACKET_SIZE);
+ lprintf(0, "Packet size too large, %d > %d\n", packetsize, QARSH_MAX_PACKET_SIZE);
return NULL;
}
/* Keep reading until we get the whole packet and nothing but the packet, so help me socket */
bufused = 0;
do {
if ((ret = read(fd, buf+bufused, packetsize-bufused)) < 0) {
- fprintf(stderr, "Read error while reading packet data: %s\n", strerror(errno));
+ lprintf(0, "Read error while reading packet data: %s\n", strerror(errno));
return NULL;
}
bufused += ret;