Skip to content

Instantly share code, notes, and snippets.

@creachadair
Created December 12, 2018 17:03
Show Gist options
  • Select an option

  • Save creachadair/214e46db4a0b891d3e9b52ff8bd861d5 to your computer and use it in GitHub Desktop.

Select an option

Save creachadair/214e46db4a0b891d3e9b52ff8bd861d5 to your computer and use it in GitHub Desktop.
Closing Unix domain sockets does not clean up the socket path
/*
gcc -std=c99 -o test test.c
./test foobar
*/
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("usage: %s <socket-path>\n", argv[0]);
return 1;
}
int fd = socket(PF_LOCAL, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
return 1;
}
struct sockaddr_un addr;
addr.sun_len = strlen(argv[1]);
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, argv[1]);
if (bind(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) {
perror("bind");
return 1;
}
if (close(fd) < 0) {
perror("close");
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment