diff options
author | Eric Andersen <andersen@codepoet.org> | 1999-11-24 12:04:33 +0300 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 1999-11-24 12:04:33 +0300 |
commit | b99df0fd65abe3245fa2d04115326100847f865e (patch) | |
tree | 7a75d52f89ec1891ec65c54d5f3ababae84c3c46 | |
parent | 394f764e7d059e979f72ba88c31f4fa94a252ebf (diff) |
First draft
-rw-r--r-- | sysklogd/syslogd.c | 86 | ||||
-rw-r--r-- | syslogd.c | 86 |
2 files changed, 172 insertions, 0 deletions
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c new file mode 100644 index 000000000..03fea9c76 --- /dev/null +++ b/sysklogd/syslogd.c @@ -0,0 +1,86 @@ +/* userver.c - simple server for Unix domain sockets */ + +/* Waits for a connection on the ./sample-socket Unix domain + socket. Once a connection has been established, copy data + from the socket to stdout until the other end closes the + connection, and then wait for another connection to the socket. */ + +#include <stdio.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <unistd.h> + +#define _PATH_LOG "/dev/log" + + +/* issue an error message via perror() and terminate the program */ +void die(char * message) { + perror(message); + exit(1); +} + +/* Copies data from file descriptor 'from' to file descriptor 'to' + until nothing is left to be copied. Exits if an error occurs. */ +void copyData(int from, int to) { + char buf[1024]; + int amount; + + while ((amount = read(from, buf, sizeof(buf))) > 0) { + if (write(to, buf, amount) != amount) { + die("write"); + return; + } + } + if (amount < 0) + die("read"); +} + + +int main(void) { + struct sockaddr_un address; + int fd, conn; + size_t addrLength; + + /* Remove any preexisting socket (or other file) */ + unlink(_PATH_LOG); + + memset(&address, 0, sizeof(address)); + address.sun_family = AF_UNIX; /* Unix domain socket */ + strncpy(address.sun_path, _PATH_LOG, sizeof(address.sun_path)); + + if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) + die("socket"); + + + /* The total length of the address includes the sun_family + element */ + addrLength = sizeof(address.sun_family) + + strlen(address.sun_path); + + if (bind(fd, (struct sockaddr *) &address, addrLength)) + die("bind"); + + if (chmod(_PATH_LOG, 0666) < 0) + die("chmod"); + + if (listen(fd, 5)) + die("listen"); + + + while ((conn = accept(fd, (struct sockaddr *) &address, + &addrLength)) >= 0) { + printf("---- getting data\n"); + copyData(conn, fileno(stdout)); + printf("---- done\n"); + close(conn); + } + + if (conn < 0) + die("accept"); + + close(fd); + return 0; +} + + + diff --git a/syslogd.c b/syslogd.c new file mode 100644 index 000000000..03fea9c76 --- /dev/null +++ b/syslogd.c @@ -0,0 +1,86 @@ +/* userver.c - simple server for Unix domain sockets */ + +/* Waits for a connection on the ./sample-socket Unix domain + socket. Once a connection has been established, copy data + from the socket to stdout until the other end closes the + connection, and then wait for another connection to the socket. */ + +#include <stdio.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <unistd.h> + +#define _PATH_LOG "/dev/log" + + +/* issue an error message via perror() and terminate the program */ +void die(char * message) { + perror(message); + exit(1); +} + +/* Copies data from file descriptor 'from' to file descriptor 'to' + until nothing is left to be copied. Exits if an error occurs. */ +void copyData(int from, int to) { + char buf[1024]; + int amount; + + while ((amount = read(from, buf, sizeof(buf))) > 0) { + if (write(to, buf, amount) != amount) { + die("write"); + return; + } + } + if (amount < 0) + die("read"); +} + + +int main(void) { + struct sockaddr_un address; + int fd, conn; + size_t addrLength; + + /* Remove any preexisting socket (or other file) */ + unlink(_PATH_LOG); + + memset(&address, 0, sizeof(address)); + address.sun_family = AF_UNIX; /* Unix domain socket */ + strncpy(address.sun_path, _PATH_LOG, sizeof(address.sun_path)); + + if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) + die("socket"); + + + /* The total length of the address includes the sun_family + element */ + addrLength = sizeof(address.sun_family) + + strlen(address.sun_path); + + if (bind(fd, (struct sockaddr *) &address, addrLength)) + die("bind"); + + if (chmod(_PATH_LOG, 0666) < 0) + die("chmod"); + + if (listen(fd, 5)) + die("listen"); + + + while ((conn = accept(fd, (struct sockaddr *) &address, + &addrLength)) >= 0) { + printf("---- getting data\n"); + copyData(conn, fileno(stdout)); + printf("---- done\n"); + close(conn); + } + + if (conn < 0) + die("accept"); + + close(fd); + return 0; +} + + + |