segfault/host/unix-socket-client.c

49 lines
1.2 KiB
C
Raw Normal View History

2023-10-23 16:47:34 +00:00
// Problem: Alpine's nc does not support connecting to unix sockets, this was needed for logpipe(see tools/logpipe).
// This utility connects to logpipe's socket, writes whatever it recieves on stdin and then exits.
//
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
2024-04-13 19:29:03 +00:00
#include <string.h>
2023-10-23 16:47:34 +00:00
2023-11-01 11:03:49 +00:00
#define SOCKET_PATH "/sf/run/logpipe/logPipe.sock"
2023-10-23 16:47:34 +00:00
int main(int argc, char *argv[]) {
int sockfd;
struct sockaddr_un addr;
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path) - 1);
if (connect(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
perror("connect");
exit(EXIT_FAILURE);
}
char buf[1024];
ssize_t nread;
while ((nread = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
if (write(sockfd, buf, nread) != nread) {
perror("write");
exit(EXIT_FAILURE);
}
}
if (nread == -1) {
perror("read");
exit(EXIT_FAILURE);
}
close(sockfd);
return EXIT_SUCCESS;
2023-11-01 11:03:49 +00:00
}