summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNate Straz <nstraz@redhat.com>2005-09-13 18:43:56 +0000
committerNathan Straz <nstraz@redhat.com>2008-09-23 09:37:44 -0400
commit60fd79294546d08d729074323052b3ea1a56a20e (patch)
treec92cec87d9c82f4f18cc7c1561ea7af35db72759
parent8bd361e185489e2ce2f0582e3c70de9bf6451a08 (diff)
downloadqarsh-60fd79294546d08d729074323052b3ea1a56a20e.zip
qarsh-60fd79294546d08d729074323052b3ea1a56a20e.tar.gz
qarsh-60fd79294546d08d729074323052b3ea1a56a20e.tar.xz
Merge r1727 from sistina-test branch djansa-qarsh
-rw-r--r--btimed.c212
-rw-r--r--btimed.xinetd13
2 files changed, 26 insertions, 199 deletions
diff --git a/btimed.c b/btimed.c
index 259c8aa..82d3c94 100644
--- a/btimed.c
+++ b/btimed.c
@@ -15,124 +15,36 @@
#include "btime_int.h"
-static void (*btimed_error)(int level, const char *fmt, ...);
-
-static void btimed(void);
static unsigned int get_btime(void);
-static int daemon_init(void);
-static void btimed_perror(int level, const char *fmt, ...);
-static void sigchld_hdlr(int sig);
-static void usage(const char *progname);
-
int
main(int argc, char **argv)
{
- struct sigaction sa;
- int debug = 0;
- int c;
-
- while ((c = getopt(argc, argv, "hd")) != -1) {
- switch (c) {
- case 'd':
- debug = 1;
- break;
- case 'h':
- case '?':
- default:
- usage(argv[0]);
- exit(2);
- }
- }
-
- memset(&sa, 0, sizeof(sa));
- sa.sa_handler = sigchld_hdlr;
- sigemptyset(&sa.sa_mask);
- sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
- sigaction(SIGCHLD, &sa, NULL);
-
- if (debug) {
- btimed_error = btimed_perror;
- } else {
- btimed_error = syslog;
- daemon_init();
- }
-
- btimed();
-
- return 0;
-}
-
-
-/*
- *---------------------------------------------------------------------------
- *
- * btimed --
- *
- * The main loop of the deamon. It listens on its well know port for
- * messages, responds to the request via a child process.
- *
- * Returns:
- * Never returns.
- *
- * Side effects:
- * Child processes are created.
- *---------------------------------------------------------------------------
- */
-
-static void
-btimed(void)
-{
int sd;
char msg[BTIME_MSGLEN];
struct sockaddr_in cli_addr;
int cli_addr_len;
- struct sockaddr_in my_addr;
- pid_t pid;
ssize_t nbytes;
- if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- btimed_error(LOG_ERR, "Could not create socket: %s\n",
- strerror(errno));
- exit(1);
- }
-
- memset(&my_addr, 0, sizeof my_addr);
- my_addr.sin_family = AF_INET;
- my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
- my_addr.sin_port = htons(BTIME_PORT);
- if (bind(sd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
- btimed_error(LOG_ERR, "Could not bind to local address: %s\n",
- strerror(errno));
- exit(1);
- }
-
- for (;;) {
- memset(&cli_addr, 0, sizeof cli_addr);
- cli_addr_len = sizeof cli_addr;
- nbytes = recvfrom(sd, &msg, BTIME_MSGLEN, MSG_WAITALL,
- (struct sockaddr *)&cli_addr, &cli_addr_len);
+ openlog("btimed", LOG_PID, LOG_USER);
- if ((pid = fork()) < 0) {
- btimed_error(LOG_ERR, "Could not fork(): %s\n", strerror(errno));
- exit(1);
- }
+ /* Running out of (x)inetd the socket was duped onto stdin. */
+ sd = fileno(stdin);
- if (pid == 0) {
- memset(msg, 0, BTIME_MSGLEN);
- sprintf(msg, "%u\n", get_btime());
- sendto(sd, &msg, BTIME_MSGLEN, MSG_DONTWAIT,
- (struct sockaddr *)&cli_addr, cli_addr_len);
+ memset(&cli_addr, 0, sizeof cli_addr);
+ cli_addr_len = sizeof cli_addr;
+ nbytes = recvfrom(sd, &msg, BTIME_MSGLEN, MSG_WAITALL,
+ (struct sockaddr *)&cli_addr, &cli_addr_len);
- exit(0);
- }
- }
+ memset(msg, 0, BTIME_MSGLEN);
+ sprintf(msg, "%u\n", get_btime());
+ sendto(sd, &msg, BTIME_MSGLEN, MSG_DONTWAIT,
+ (struct sockaddr *)&cli_addr, cli_addr_len);
- close(sd);
+ return 0;
}
-
/*
*---------------------------------------------------------------------------
*
@@ -155,7 +67,7 @@ get_btime(void)
unsigned int btime = 0;
if ((statf = fopen("/proc/stat", "r")) == NULL) {
- btimed_error(LOG_ERR, "/proc/stat open failure: %s\n",
+ syslog(LOG_ERR, "/proc/stat open failure: %s\n",
strerror(errno));
exit(1);
}
@@ -171,101 +83,3 @@ get_btime(void)
return btime;
}
-
-
-/*
- *---------------------------------------------------------------------------
- *
- * daemon_init --
- *
- * Set up a basic daemon environment
- *
- * Returns:
- * 0 on success
- * non-zero on failure.
- *
- * Side effects:
- * All fds are closed and process is now a session leader.
- *
- *---------------------------------------------------------------------------
- */
-
-static int
-daemon_init(void)
-{
- pid_t pid;
- int fd;
- int maxfd = sysconf(_SC_OPEN_MAX);
-
- if ((pid = fork()) < 0) {
- return(-1);
- } else if (pid != 0) {
- exit(0); /* Exit in parent */
- }
-
- /*
- * Child sets up daemon environment
- */
-
- setsid(); /* Become session leader */
- chdir("/");
- umask(0);
- for (fd = 0; fd < maxfd; fd++) {
- close(fd);
- }
- errno = 0; /* probably set to EBADFD from above loop, reset */
-
- openlog("btimed", LOG_PID, LOG_USER);
-
- return 0;
-}
-
-
-/*
- *---------------------------------------------------------------------------
- *
- * btimed_perror --
- *
- * Print error messages to stderr, level is not used, it is there
- * to have the same calling args as syslog(3) so we can use either
- * based on debug level.
- *
- * Returns:
- * NA
- *
- * Side effects:
- * None
- *---------------------------------------------------------------------------
- */
-
-static void
-btimed_perror(int level, const char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
-}
-
-
-static void
-sigchld_hdlr(int sig)
-{
- int status;
-
- wait(&status);
- return;
-}
-
-
-static void
-usage(const char *progname)
-{
- fprintf(stderr, "Usage: %s [OPTION]...\n"
- "-h\tPrint this help and exit.\n"
- "-d\tDon't daemonize, send output to stderr.\n",
- progname);
-
- return;
-}
diff --git a/btimed.xinetd b/btimed.xinetd
new file mode 100644
index 0000000..390e0f7
--- /dev/null
+++ b/btimed.xinetd
@@ -0,0 +1,13 @@
+# default: on
+# description: btimed
+
+service qarshd
+{
+ type = UNLISTED
+ disable = no
+ port = 23456
+ socket_type = dgram
+ wait = yes
+ user = root
+ server = /home/msp/djansa/src/svn/djansa-qarsh/src/btime/btimed
+}