From e5c39cc9997baccb4a66792e245326c9aadd92be Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Tue, 13 Jan 2015 21:49:44 +0100 Subject: [PATCH 1/2] sockread: add new package sockread reads data from a Unix domain socket Signed-off-by: Moritz Warning --- utils/sockread/Makefile | 35 +++++++++++++++++++++++ utils/sockread/src/Makefile | 11 ++++++++ utils/sockread/src/main.c | 55 +++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 utils/sockread/Makefile create mode 100644 utils/sockread/src/Makefile create mode 100644 utils/sockread/src/main.c diff --git a/utils/sockread/Makefile b/utils/sockread/Makefile new file mode 100644 index 000000000..78beee248 --- /dev/null +++ b/utils/sockread/Makefile @@ -0,0 +1,35 @@ +# +# This software is licensed under the CC0-1.0 license. +# +include $(TOPDIR)/rules.mk + +PKG_NAME:=sockread +PKG_VERSION:=1.0 +PKG_RELEASE:=1 +PKG_LICENSE:=CC0-1.0 + +include $(INCLUDE_DIR)/package.mk + +define Package/sockread + SECTION:=utils + CATEGORY:=Utilities + TITLE:=sockread + MAINTAINER:=Moritz Warning +endef + +define Package/sockread/description + sockread reads data from a Unix domain socket + represented as a special file on the file system. +endef + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) + $(CP) ./src/* $(PKG_BUILD_DIR)/ +endef + +define Package/sockread/install + $(INSTALL_DIR) $(1)/usr/bin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/sockread $(1)/usr/bin/ +endef + +$(eval $(call BuildPackage,sockread)) diff --git a/utils/sockread/src/Makefile b/utils/sockread/src/Makefile new file mode 100644 index 000000000..fe4f8a7c2 --- /dev/null +++ b/utils/sockread/src/Makefile @@ -0,0 +1,11 @@ +CC ?= gcc +CFLAGS ?= -O2 -Wall -pedantic +CFLAGS += -std=gnu99 + +.PHONY: clean + +sockread: + $(CC) $(CFLAGS) main.c -o sockread + +clean: + rm -f sockread diff --git a/utils/sockread/src/main.c b/utils/sockread/src/main.c new file mode 100644 index 000000000..3343f2a45 --- /dev/null +++ b/utils/sockread/src/main.c @@ -0,0 +1,55 @@ + +#include +#include +#include +#include +#include +#include +#include +#include + + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "Usage: %s \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); + } + + return 0; +} From 04a518498c6bdcbe24c9e9f92ed525f3551e0431 Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Sun, 18 Jan 2015 02:01:14 +0100 Subject: [PATCH 2/2] sockread: add support for reading data from a pipe --- utils/sockread/Makefile | 2 +- utils/sockread/src/main.c | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/utils/sockread/Makefile b/utils/sockread/Makefile index 78beee248..d3c15d468 100644 --- a/utils/sockread/Makefile +++ b/utils/sockread/Makefile @@ -18,7 +18,7 @@ define Package/sockread endef define Package/sockread/description - sockread reads data from a Unix domain socket + sockread writes and reads data from a Unix domain socket represented as a special file on the file system. endef diff --git a/utils/sockread/src/main.c b/utils/sockread/src/main.c index 3343f2a45..06c21def4 100644 --- a/utils/sockread/src/main.c +++ b/utils/sockread/src/main.c @@ -3,15 +3,16 @@ #include #include #include -#include -#include +#include #include #include - int main(int argc, char *argv[]) { + char buf[1024]; + ssize_t r; + if (argc != 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); + fprintf(stderr, "Write to and read from a Unix domain socket.\n\nUsage: %s \n", argv[0]); return 1; } @@ -36,8 +37,15 @@ int main(int argc, char *argv[]) { return 1; } - char buf[1024]; - ssize_t r; + /* Check if stdin refers to a terminal */ + if (!isatty(fileno(stdin))) { + /* Read from stdin and write to socket */ + while (0 < (r = fread(buf, 1, sizeof(buf), stdin))) { + send(fd, buf, r, 0); + } + } + + /* Read from socket and write to stdout */ while (1) { r = recv(fd, buf, sizeof(buf), 0); if (r < 0) {