sockread: cleanup code and allow arguments

* cleanup code style
* allow arguments to be passed from the command line

Signed-off-by: Moritz Warning <moritzwarning@web.de>
This commit is contained in:
Moritz Warning 2023-11-20 20:37:36 +01:00 committed by Tianling Shen
parent 11279e54ed
commit 67ff89f267
2 changed files with 55 additions and 48 deletions

View file

@ -4,8 +4,8 @@
include $(TOPDIR)/rules.mk include $(TOPDIR)/rules.mk
PKG_NAME:=sockread PKG_NAME:=sockread
PKG_VERSION:=1.0 PKG_VERSION:=1.1
PKG_RELEASE:=2 PKG_RELEASE:=1
PKG_LICENSE:=CC0-1.0 PKG_LICENSE:=CC0-1.0
include $(INCLUDE_DIR)/package.mk include $(INCLUDE_DIR)/package.mk
@ -13,13 +13,12 @@ include $(INCLUDE_DIR)/package.mk
define Package/sockread define Package/sockread
SECTION:=utils SECTION:=utils
CATEGORY:=Utilities CATEGORY:=Utilities
TITLE:=sockread TITLE:=Unix domain sockets utility
MAINTAINER:=Moritz Warning <moritzwarning@web.de> MAINTAINER:=Moritz Warning <moritzwarning@web.de>
endef endef
define Package/sockread/description define Package/sockread/description
sockread writes and reads data from a Unix domain socket Command line utility to read and write to Unix domain sockets.
represented as a special file on the file system.
endef endef
define Build/Prepare define Build/Prepare

View file

@ -7,57 +7,65 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
int main(int argc, char *argv[]) { const char *usage =
char buf[1024]; "Write to and read from a Unix domain socket.\n"
ssize_t r; "Add commands to send as arguments or pass by pipe.\n"
"\n"
"Usage: sockread <path> [<commands>]\n";
if (argc != 2) { int main(int argc, char *argv[])
fprintf(stderr, "Write to and read from a Unix domain socket.\n\nUsage: %s <socket>\n", argv[0]); {
return 1; char buffer[1024];
} ssize_t r;
size_t addrlen = strlen(argv[1]); if (argc < 2) {
fprintf(stderr, "%s", usage);
return EXIT_FAILURE;
}
/* Allocate enough space for arbitrary-length paths */ struct sockaddr_un address = {0};
char addrbuf[offsetof(struct sockaddr_un, sun_path) + addrlen + 1]; address.sun_family = AF_UNIX;
memset(addrbuf, 0, sizeof(addrbuf)); strcpy((char*) &address.sun_path, argv[1]);
struct sockaddr_un *addr = (struct sockaddr_un *)addrbuf; int sock = socket(AF_UNIX, SOCK_STREAM, 0);
addr->sun_family = AF_UNIX; if (sock < 0) {
memcpy(addr->sun_path, argv[1], addrlen+1); fprintf(stderr, "socket() %s\n", strerror(errno));
return EXIT_FAILURE;
}
int fd = socket(AF_UNIX, SOCK_STREAM, 0); if (connect(sock, (struct sockaddr*)&address, sizeof(address)) < 0) {
if (fd < 0) { fprintf(stderr, "connect() %s\n", strerror(errno));
fprintf(stderr, "Failed to create socket: %s\n", strerror(errno)); return EXIT_FAILURE;
return 1; }
}
if (connect(fd, (struct sockaddr*)addr, sizeof(addrbuf)) < 0) { /* Check if stdin refers to a terminal */
fprintf(stderr, "Can't connect to `%s': %s\n", argv[1], strerror(errno)); if (!isatty(fileno(stdin))) {
return 1; /* Read from stdin and write to socket */
} while (0 < (r = fread(buffer, 1, sizeof(buffer), stdin))) {
send(sock, buffer, r, 0);
}
} else {
for (size_t i = 2; i < argc; i++) {
if (i > 2) {
send(sock, " ", 1, 0);
}
send(sock, argv[i], strlen(argv[i]), 0);
}
}
/* Check if stdin refers to a terminal */ /* Read from socket and write to stdout */
if (!isatty(fileno(stdin))) { while (1) {
/* Read from stdin and write to socket */ r = recv(sock, buffer, sizeof(buffer), 0);
while (0 < (r = fread(buf, 1, sizeof(buf), stdin))) { if (r < 0) {
send(fd, buf, r, 0); fprintf(stderr, "recv() %s\n", strerror(errno));
} return EXIT_FAILURE;
} }
/* Read from socket and write to stdout */ if (r == 0)
while (1) { break;
r = recv(fd, buf, sizeof(buf), 0);
if (r < 0) {
fprintf(stderr, "read: %s\n", strerror(errno));
return 1;
}
if (r == 0) fwrite(buffer, r, 1, stdout);
return 0; }
fwrite(buf, r, 1, stdout); return EXIT_SUCCESS;
}
return 0;
} }