author | viric@llimona |
Fri, 14 Sep 2007 22:23:04 +0200 | |
changeset 11 | b93316813d7a |
parent 6 | 4fe857c0b12a |
child 12 | 6a372f8b4b8a |
permissions | -rw-r--r-- |
0 | 1 |
#include <stdlib.h> |
2 |
#include <sys/un.h> |
|
3 |
#include <sys/socket.h> |
|
4 |
||
3 | 5 |
#include "main.h" |
0 | 6 |
|
7 |
static char socket_path[256]; |
|
6 | 8 |
static char default_path_prefix[] = "/tmp/socket-sm."; |
0 | 9 |
|
10 |
static void get_path() |
|
11 |
{ |
|
12 |
char *new; |
|
13 |
||
6 | 14 |
new = getenv("SM_SOCKET"); |
0 | 15 |
if (new == 0) |
16 |
{ |
|
17 |
char num[20]; |
|
18 |
snprintf(socket_path, sizeof(socket_path), "%s%i", |
|
19 |
default_path_prefix, (int) getuid()); |
|
20 |
} else |
|
21 |
strncpy(socket_path, new, sizeof(socket_path)); |
|
22 |
} |
|
23 |
||
24 |
int serve_socket() |
|
25 |
{ |
|
26 |
int ls; |
|
27 |
struct sockaddr_un addr; |
|
28 |
int res; |
|
29 |
||
30 |
get_path(); |
|
31 |
||
32 |
ls = socket(AF_UNIX, SOCK_STREAM, 0); |
|
33 |
if (ls == -1) |
|
4
b5b369483ec0
Checked rename, error messages without ending dot.
viric@llimona
parents:
3
diff
changeset
|
34 |
error("Cannot create the unix listen socket in the server"); |
0 | 35 |
|
36 |
addr.sun_family = AF_UNIX; |
|
37 |
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path)); |
|
38 |
||
39 |
res = bind(ls, (struct sockaddr *) & addr, sizeof(addr)); |
|
40 |
if (res == -1) |
|
4
b5b369483ec0
Checked rename, error messages without ending dot.
viric@llimona
parents:
3
diff
changeset
|
41 |
error("Error binding"); |
0 | 42 |
|
43 |
res = listen(ls, 10); |
|
44 |
if (res == -1) |
|
4
b5b369483ec0
Checked rename, error messages without ending dot.
viric@llimona
parents:
3
diff
changeset
|
45 |
error("Error listening"); |
0 | 46 |
|
47 |
return ls; |
|
48 |
} |
|
49 |
||
1 | 50 |
int accept_connection(int ls) |
51 |
{ |
|
52 |
int cs; |
|
53 |
cs = accept(ls, 0, 0); |
|
54 |
return cs; |
|
55 |
} |
|
56 |
||
57 |
int connect_socket() |
|
58 |
{ |
|
59 |
struct sockaddr_un addr; |
|
60 |
int res; |
|
61 |
int cs; |
|
62 |
||
63 |
get_path(); |
|
64 |
||
65 |
cs = socket(AF_UNIX, SOCK_STREAM, 0); |
|
66 |
if (cs == -1) |
|
4
b5b369483ec0
Checked rename, error messages without ending dot.
viric@llimona
parents:
3
diff
changeset
|
67 |
error("Cannot create the unix connect socket in the client"); |
1 | 68 |
|
69 |
addr.sun_family = AF_UNIX; |
|
70 |
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path)); |
|
71 |
||
72 |
res = connect(cs, (struct sockaddr *) &addr, sizeof(addr)); |
|
73 |
if (res == -1) |
|
4
b5b369483ec0
Checked rename, error messages without ending dot.
viric@llimona
parents:
3
diff
changeset
|
74 |
error("Cannot connect"); |
1 | 75 |
return cs; |
76 |
} |
|
77 |
||
0 | 78 |
void remove_socket(int ls) |
79 |
{ |
|
80 |
close(ls); |
|
81 |
unlink(socket_path); |
|
82 |
} |