Created
December 20, 2016 17:01
-
-
Save jjelinek/fd4051654d9b4b28b017cd28de3f7844 to your computer and use it in GitHub Desktop.
test code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <strings.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <netdb.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <inttypes.h> | |
#include <stropts.h> | |
#include <sys/select.h> | |
#include <sys/un.h> | |
#include <string.h> | |
void | |
main(int argc, char **argv) | |
{ | |
int i; | |
int enable = 1; | |
int len; | |
int fds[2]; | |
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) < 0) { | |
perror("p: socketpair"); | |
exit(1); | |
} | |
if (setsockopt(fds[0], SOL_SOCKET, SO_PASSCRED, &enable, | |
sizeof (enable)) < 0) { | |
perror("p: setsockopt"); | |
exit(1); | |
} | |
if (fork() == 0) { | |
char buf[80]; | |
struct msghdr msg; | |
struct iovec iov; | |
snprintf(buf, sizeof (buf), "child %d", getpid()); | |
iov.iov_base = buf; | |
iov.iov_len = strlen(buf); | |
msg.msg_name = NULL; | |
msg.msg_namelen = 0; | |
msg.msg_iov = &iov; | |
msg.msg_iovlen = 1; | |
msg.msg_control = NULL; | |
msg.msg_controllen = 0; | |
msg.msg_flags = 0; | |
printf("c1: sendmsg\n"); | |
if (sendmsg(fds[1], &msg, 0) < 0) { | |
perror("c1: sndmsg"); | |
exit(1); | |
} | |
exit(0); | |
} | |
if (fork() == 0) { | |
char buf[80]; | |
struct msghdr msg; | |
struct iovec iov; | |
snprintf(buf, sizeof (buf), "child %d", getpid()); | |
iov.iov_base = buf; | |
iov.iov_len = strlen(buf); | |
msg.msg_name = NULL; | |
msg.msg_namelen = 0; | |
msg.msg_iov = &iov; | |
msg.msg_iovlen = 1; | |
msg.msg_control = NULL; | |
msg.msg_controllen = 0; | |
msg.msg_flags = 0; | |
printf("c2: sendmsg\n"); | |
if (sendmsg(fds[1], &msg, 0) < 0) { | |
perror("c2: sndmsg"); | |
exit(1); | |
} | |
exit(0); | |
} | |
/* parent - server */ | |
for (i = 0; i < 2; i++) { | |
char ctl[256]; | |
char buf[80]; | |
struct msghdr msg; | |
struct iovec iov; | |
iov.iov_base = buf; | |
iov.iov_len = sizeof (buf); | |
msg.msg_name = NULL; | |
msg.msg_namelen = 0; | |
msg.msg_iov = &iov; | |
msg.msg_iovlen = 1; | |
msg.msg_control = &ctl; | |
msg.msg_controllen = sizeof (ctl); | |
msg.msg_flags = 0; | |
printf("p: recvmsg\n"); | |
if ((len = recvmsg(fds[0], &msg, 0)) < 0) { | |
perror("p: Could not read message"); | |
exit(1); | |
} else { | |
printf("p: len %d, %s\n", len, buf); | |
} | |
} | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment