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