Update.
[glibc.git] / manual / examples / mkfsock.c
blobd3750ec150a47cf8a06a89d92e7f977a237795f1
1 #include <stddef.h>
2 #include <stdio.h>
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <sys/socket.h>
6 #include <sys/un.h>
8 int
9 make_named_socket (const char *filename)
11 struct sockaddr_un name;
12 int sock;
13 size_t size;
15 /* Create the socket. */
17 sock = socket (PF_UNIX, SOCK_DGRAM, 0);
18 if (sock < 0)
20 perror ("socket");
21 exit (EXIT_FAILURE);
24 /* Bind a name to the socket. */
26 name.sun_family = AF_FILE;
27 strcpy (name.sun_path, filename);
29 /* The size of the address is
30 the offset of the start of the filename,
31 plus its length,
32 plus one for the terminating null byte. */
33 size = (offsetof (struct sockaddr_un, sun_path)
34 + strlen (name.sun_path) + 1);
36 if (bind (sock, (struct sockaddr *) &name, size) < 0)
38 perror ("bind");
39 exit (EXIT_FAILURE);
42 return sock;