]> git.ipfire.org Git - thirdparty/glibc.git/blob - manual/examples/mkfsock.c
initial import
[thirdparty/glibc.git] / manual / examples / mkfsock.c
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>
7
8 int
9 make_named_socket (const char *filename)
10 {
11 struct sockaddr_un name;
12 int sock;
13 size_t size;
14
15 /* Create the socket. */
16
17 sock = socket (PF_UNIX, SOCK_DGRAM, 0);
18 if (sock < 0)
19 {
20 perror ("socket");
21 exit (EXIT_FAILURE);
22 }
23
24 /* Bind a name to the socket. */
25
26 name.sun_family = AF_FILE;
27 strcpy (name.sun_path, filename);
28
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);
35
36 if (bind (sock, (struct sockaddr *) &name, size) < 0)
37 {
38 perror ("bind");
39 exit (EXIT_FAILURE);
40 }
41
42 return sock;
43 }