author | viric@llimona |
Mon, 12 Nov 2007 09:57:46 +0100 | |
changeset 140 | 0b99d94818d1 |
parent 133 | 4306e96f5906 |
child 146 | 5e689cb593aa |
permissions | -rw-r--r-- |
49 | 1 |
/* |
2 |
Task Spooler - a task queue system for the unix user |
|
3 |
Copyright (C) 2007 LluĂs Batlle i Rossell |
|
4 |
||
5 |
Please find the license in the provided COPYING file. |
|
6 |
*/ |
|
44 | 7 |
#include <unistd.h> |
8 |
#include <string.h> |
|
1 | 9 |
#include <sys/types.h> |
10 |
#include <sys/socket.h> |
|
11 |
#include <sys/un.h> |
|
12 |
#include <errno.h> |
|
3 | 13 |
#include <stdlib.h> |
9 | 14 |
#include <stdio.h> |
25 | 15 |
#include <signal.h> |
3 | 16 |
|
17 |
#include "main.h" |
|
1 | 18 |
|
19 |
extern int server_socket; |
|
20 |
||
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
21 |
static char *path; |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
22 |
|
25 | 23 |
static int fork_server(); |
24 |
||
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
25 |
static void create_path() |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
26 |
{ |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
27 |
char *tmpdir; |
119
361b08d33762
$USER is no more used for crating the socket path. Now the UID is used.
viric@llimona
parents:
92
diff
changeset
|
28 |
char userid[20]; |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
29 |
int size; |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
30 |
|
64 | 31 |
/* As a priority, TS_SOCKET mandates over the path creation */ |
32 |
path = getenv("TS_SOCKET"); |
|
33 |
if (path != 0) |
|
34 |
{ |
|
35 |
/* We need this in our memory, for forks and future 'free'. */ |
|
36 |
size = strlen(path) + 1; |
|
37 |
path = (char *) malloc(size); |
|
38 |
strcpy(path, getenv("TS_SOCKET")); |
|
39 |
return; |
|
40 |
} |
|
41 |
||
42 |
/* ... if the $TS_SOCKET doesn't exist ... */ |
|
43 |
||
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
44 |
/* Create the path */ |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
45 |
tmpdir = getenv("TMPDIR"); |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
46 |
if (tmpdir == NULL) |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
47 |
tmpdir = "/tmp"; |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
48 |
|
119
361b08d33762
$USER is no more used for crating the socket path. Now the UID is used.
viric@llimona
parents:
92
diff
changeset
|
49 |
sprintf(userid, "%u", (unsigned int) getuid()); |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
50 |
|
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
51 |
/* Calculate the size */ |
119
361b08d33762
$USER is no more used for crating the socket path. Now the UID is used.
viric@llimona
parents:
92
diff
changeset
|
52 |
size = strlen(tmpdir) + strlen("/socket-ts.") + strlen(userid) + 1; |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
53 |
|
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
54 |
/* Freed after preparing the socket address */ |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
55 |
path = (char *) malloc(size); |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
56 |
|
119
361b08d33762
$USER is no more used for crating the socket path. Now the UID is used.
viric@llimona
parents:
92
diff
changeset
|
57 |
sprintf(path, "%s/socket-ts.%s", tmpdir, userid); |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
58 |
} |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
59 |
|
1 | 60 |
int try_connect(int s) |
61 |
{ |
|
2 | 62 |
struct sockaddr_un addr; |
63 |
int res; |
|
1 | 64 |
|
2 | 65 |
addr.sun_family = AF_UNIX; |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
66 |
strcpy(addr.sun_path, path); |
1 | 67 |
|
2 | 68 |
res = connect(s, (struct sockaddr *) &addr, sizeof(addr)); |
69 |
return res; |
|
1 | 70 |
} |
71 |
||
25 | 72 |
void wait_server_up(int fd) |
1 | 73 |
{ |
25 | 74 |
char a; |
75 |
||
76 |
read(fd, &a, 1); |
|
77 |
close(fd); |
|
1 | 78 |
} |
79 |
||
25 | 80 |
/* Returns the fd where to wait for the parent notification */ |
81 |
static int fork_server() |
|
1 | 82 |
{ |
2 | 83 |
int pid; |
25 | 84 |
int p[2]; |
1 | 85 |
|
2 | 86 |
/* !!! stdin/stdout */ |
25 | 87 |
pipe(p); |
1 | 88 |
|
2 | 89 |
pid = fork(); |
90 |
switch (pid) |
|
91 |
{ |
|
92 |
case 0: /* Child */ |
|
25 | 93 |
close(p[0]); |
3 | 94 |
close(server_socket); |
66
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
95 |
/* Close all std handles for the server */ |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
96 |
close(0); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
97 |
close(1); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
98 |
close(2); |
133 | 99 |
setsid(); |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
100 |
server_main(p[1], path); |
3 | 101 |
exit(0); |
2 | 102 |
break; |
103 |
case -1: /* Error */ |
|
25 | 104 |
return -1; |
2 | 105 |
default: /* Parent */ |
25 | 106 |
close(p[1]); |
2 | 107 |
} |
25 | 108 |
/* Return the read fd */ |
109 |
return p[0]; |
|
110 |
} |
|
111 |
||
112 |
void notify_parent(int fd) |
|
113 |
{ |
|
114 |
char a = 'a'; |
|
115 |
write(fd, &a, 1); |
|
116 |
close(fd); |
|
1 | 117 |
} |
118 |
||
119 |
int ensure_server_up() |
|
120 |
{ |
|
2 | 121 |
int res; |
25 | 122 |
int notify_fd; |
1 | 123 |
|
129
312083fb8899
Change PF_UNIX to AF_UNIX. According to socket(2), this is better.
viric@llimona
parents:
119
diff
changeset
|
124 |
server_socket = socket(AF_UNIX, SOCK_STREAM, 0); |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
125 |
if (server_socket == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
126 |
error("getting the server socket"); |
1 | 127 |
|
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
128 |
create_path(); |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
129 |
|
2 | 130 |
res = try_connect(server_socket); |
1 | 131 |
|
2 | 132 |
/* Good connection */ |
133 |
if (res == 0) |
|
134 |
return 1; |
|
1 | 135 |
|
2 | 136 |
/* If error other than "No one listens on the other end"... */ |
3 | 137 |
if (!(errno == ENOENT || errno == ECONNREFUSED)) |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
138 |
error("c: cannot connect to the server"); |
1 | 139 |
|
3 | 140 |
if (errno == ECONNREFUSED) |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
141 |
unlink(path); |
3 | 142 |
|
2 | 143 |
/* Try starting the server */ |
25 | 144 |
notify_fd = fork_server(); |
145 |
wait_server_up(notify_fd); |
|
2 | 146 |
res = try_connect(server_socket); |
1 | 147 |
|
2 | 148 |
/* The second time didn't work. Abort. */ |
149 |
if (res == -1) |
|
9 | 150 |
{ |
151 |
fprintf(stderr, "The server didn't come up.\n"); |
|
152 |
exit(-1); |
|
153 |
} |
|
1 | 154 |
|
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
155 |
free(path); |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
156 |
|
2 | 157 |
/* Good connection on the second time */ |
158 |
return 1; |
|
1 | 159 |
} |