forked from intel/lmbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_unix.c
97 lines (88 loc) · 1.63 KB
/
lib_unix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* unix_lib.c - routines for managing UNIX connections.
*
* Positive port/program numbers are RPC ports, negative ones are UNIX ports.
*
* Copyright (c) 1994-1996 Larry McVoy.
*/
#define _LIB /* bench.h needs this */
#include "bench.h"
/*
* Get a UNIX socket, bind it.
*/
int
unix_server(char *path)
{
int sock;
struct sockaddr_un s;
#ifdef LIBUNIX_VERBOSE
fprintf(stderr, "unix_server(%s, %u)\n", prog, rdwr);
#endif
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
bzero((void*)&s, sizeof(s));
s.sun_family = AF_UNIX;
strcpy(s.sun_path, path);
if (bind(sock, (struct sockaddr*)&s, sizeof(s)) < 0) {
perror("bind");
exit(2);
}
if (listen(sock, 100) < 0) {
perror("listen");
exit(4);
}
return (sock);
}
/*
* Unadvertise the socket
*/
int
unix_done(int sock, char *path)
{
close(sock);
unlink(path);
return (0);
}
/*
* Accept a connection and return it
*/
int
unix_accept(int sock)
{
struct sockaddr_un s;
int newsock, namelen;
namelen = sizeof(s);
bzero((void*)&s, namelen);
retry:
if ((newsock = accept(sock, (struct sockaddr*)&s, &namelen)) < 0) {
if (errno == EINTR)
goto retry;
perror("accept");
exit(6);
}
return (newsock);
}
/*
* Connect to the UNIX socket advertised as "path" and
* return the connected socket.
*/
int
unix_connect(char *path)
{
struct sockaddr_un s;
int sock;
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
bzero((void*)&s, sizeof(s));
s.sun_family = AF_UNIX;
strcpy(s.sun_path, path);
if (connect(sock, (struct sockaddr*)&s, sizeof(s)) < 0) {
perror("connect");
exit(4);
}
return (sock);
}