Skip to content
Snippets Groups Projects
Commit 15a717d9 authored by Christian Dietrich's avatar Christian Dietrich
Browse files

Dec 21 -- Let the Children Speak up!

Article:  https://ibr.cs.tu-bs.de/advent/21-sendfd/
Workload: ~155 source-code lines
parent 2f7dc334
No related branches found
No related tags found
No related merge requests found
PROG=server
all: ${PROG} client
%: %.c
gcc $< -o $@ -Wall -g
run: ${PROG}
./${PROG}
strace: ${PROG}
strace ./${PROG}
clean:
rm -f ./${PROG} client
#include <sys/socket.h>
#include <sys/signalfd.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <signal.h>
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while(0)
// Receive a message with an attached file descriptor from a
// connected UNIX domain socket. The message is stored in the buffer,
// and the file descriptor in *fd. The function returns the length of
// the message.
//
// sock_fd: Connected UNIX domain socket
// buf, bufsize: Buffer for the received message
// fd: Where to store the file descriptor
int recvfd(int sock_fd, char *buf, size_t bufsize, int *fd) {
return 0;
}
int main(int argc, char *argv[]) {
// FIXME: Create a socket(2) with AF_UNIX, SOCK_SEQPACKET
// FIXME: Connect to the domain socket "./socket"
// FIXME: Receive the file descriptor with recvfd()
// FIXME: Add a read/write loop that transfers data from your stdin to the received file descriptor (like cat)
}
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <limits.h>
#include <sys/un.h>
#include <sys/socket.h>
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while(0)
// Send a file descriptor over an connected UNIX domain socket. The
// file descriptor is send as auxiliary data attached to a regular
// buffer. With Linux, we have to send at least one byte to transfer a
// file descriptor.
//
// sockfd: connected UNIX domain socket
// buf, buflen: Message to send, arbitrary data
// fd: file descriptor to transfer
void sendfd(int sockfd, void *buf, size_t buflen, int fd) {
// FIXME: Prepare a struct msghdr with an msg_control buffer
// FIXME: Attach the file descriptor as cmsg (see cmsg(3), unix(7))
// FIXME: Use sendmsg(2) to send the file descriptor and the message to the other process.
}
int main() {
// FIXME: Create an socket with AF_UNIX and SOCK_SEQPACKET
// FIXME: Bind it to a filename and listen
// FIXME: Accept clients, send your STDOUT, and directly close the connection again
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment