1 /* Create an AF_UNIX socket.
3 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
20 #include <sys/types.h>
22 #include <sys/socket.h>
25 #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
27 int main(int argc, char *argv[])
29 struct sockaddr_un sun;
35 fprintf(stderr, "Format: %s <socketpath>\n", argv[0]);
39 max = sizeof(sun.sun_path);
40 len = strlen(argv[1]);
42 fprintf(stderr, "Filename too long (max %zu)\n", max);
46 fd = socket(AF_UNIX, SOCK_DGRAM, 0);
52 memset(&sun, 0, sizeof(sun));
53 sun.sun_family = AF_UNIX;
54 strcpy(sun.sun_path, argv[1]);
55 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
60 if (stat(argv[1], &st)) {
61 fprintf(stderr, "Couldn't stat socket after creation: %m\n");