sockread: add new package
sockread reads data from a Unix domain socket Signed-off-by: Moritz Warning <moritzwarning@web.de>
This commit is contained in:
parent
1c40fc1022
commit
e5c39cc999
3 changed files with 101 additions and 0 deletions
35
utils/sockread/Makefile
Normal file
35
utils/sockread/Makefile
Normal file
|
@ -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 <moritzwarning@web.de>
|
||||||
|
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))
|
11
utils/sockread/src/Makefile
Normal file
11
utils/sockread/src/Makefile
Normal file
|
@ -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
|
55
utils/sockread/src/main.c
Normal file
55
utils/sockread/src/main.c
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in a new issue