admin管理员组文章数量:1244294
As part of a larger program, I would like to copy everything from standard input to a UNIX domain socket. I figured that splice(2) would likely be appropriate but I'm having trouble using it.
Minimal Example
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <fcntl.h>
int main(void) {
int sock;
struct sockaddr_un addr;
const char *socket_path = "/tmp/test.sock";
int exit_status = EXIT_SUCCESS;
if (socket_path == NULL) {
exit(EXIT_FAILURE);
}
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
perror("socket");
exit_status = EXIT_FAILURE;
goto cleanup;
}
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(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
perror("connect");
exit_status = EXIT_FAILURE;
goto cleanup;
}
ssize_t bytes_spliced;
/* I'm setting the offsets to NULL because they don't make sense in pipes or sockets */
while ((bytes_spliced = splice(STDIN_FILENO, NULL, sock, NULL, 16384, SPLICE_F_MORE)) > 0) {
}
if (bytes_spliced == -1) {
perror("splice");
exit_status = EXIT_FAILURE;
}
cleanup:
close(sock);
return exit_status;
}
All I get is:
splice: Invalid argument
As part of a larger program, I would like to copy everything from standard input to a UNIX domain socket. I figured that splice(2) would likely be appropriate but I'm having trouble using it.
Minimal Example
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <fcntl.h>
int main(void) {
int sock;
struct sockaddr_un addr;
const char *socket_path = "/tmp/test.sock";
int exit_status = EXIT_SUCCESS;
if (socket_path == NULL) {
exit(EXIT_FAILURE);
}
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
perror("socket");
exit_status = EXIT_FAILURE;
goto cleanup;
}
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(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
perror("connect");
exit_status = EXIT_FAILURE;
goto cleanup;
}
ssize_t bytes_spliced;
/* I'm setting the offsets to NULL because they don't make sense in pipes or sockets */
while ((bytes_spliced = splice(STDIN_FILENO, NULL, sock, NULL, 16384, SPLICE_F_MORE)) > 0) {
}
if (bytes_spliced == -1) {
perror("splice");
exit_status = EXIT_FAILURE;
}
cleanup:
close(sock);
return exit_status;
}
All I get is:
splice: Invalid argument
Share
Improve this question
asked Feb 16 at 17:27
Runxi YuRunxi Yu
3432 silver badges9 bronze badges
1 Answer
Reset to default 2The problem is that standard input is not always a pipe, and splice(2) requires at least one side to be a pipe.
$ ./test < test.c
splice: Invalid argument
(exit: 1)
$ cat test.c | ./test
$ ./test
(exit: 1)
In the first case, standard input is a file, which cannot be spliced. In the second case, it is a pipe. In the third case, it's a character device i.e. my terminal.
本文标签: cSplicing standard input to a socketStack Overflow
版权声明:本文标题:c - Splicing standard input to a socket - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740197743a2239635.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论