summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2015-01-13 00:39:10 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2015-01-13 00:39:10 +0100
commit7815904f1136339d47329a3871d9939eac34f830 (patch)
treec9eb3d29cb97f97edd88e29b8ec0c5a311720a15
parentbc28467b00e48c958bd1313245953ef81a75c293 (diff)
downloadfastd-7815904f1136339d47329a3871d9939eac34f830.tar
fastd-7815904f1136339d47329a3871d9939eac34f830.zip
doc: add example C code to read status socket
-rw-r--r--doc/examples/status.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/doc/examples/status.c b/doc/examples/status.c
new file mode 100644
index 0000000..7f0e46e
--- /dev/null
+++ b/doc/examples/status.c
@@ -0,0 +1,51 @@
+#include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <sys/socket.h>
+#include <sys/un.h>
+
+
+int main(int argc, char *argv[]) {
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <socket>\n", argv[0]);
+ return 1;
+ }
+
+ size_t addrlen = strlen(argv[1]);
+
+ /* Allocate enough space for arbitrary-length paths */
+ char addrbuf[offsetof(struct sockaddr_un, sun_path) + addrlen + 1];
+ memset(addrbuf, 0, sizeof(addrbuf));
+
+ struct sockaddr_un *addr = (struct sockaddr_un *)addrbuf;
+ addr->sun_family = AF_UNIX;
+ memcpy(addr->sun_path, argv[1], addrlen+1);
+
+ int fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (fd < 0) {
+ fprintf(stderr, "Failed to create socket: %s\n", strerror(errno));
+ return 1;
+ }
+
+ if (connect(fd, (struct sockaddr*)addr, sizeof(addrbuf)) < 0) {
+ fprintf(stderr, "Can't connect to `%s': %s\n", argv[1], strerror(errno));
+ return 1;
+ }
+
+ char buf[1024];
+ ssize_t r;
+ while (1) {
+ r = recv(fd, buf, sizeof(buf), 0);
+ if (r < 0) {
+ fprintf(stderr, "read: %s\n", strerror(errno));
+ return 1;
+ }
+
+ if (r == 0)
+ return 0;
+
+ fwrite(buf, r, 1, stdout);
+ }
+}