author | viric <viriketo@gmail.com> |
Wed, 19 Oct 2016 22:08:34 +0200 | |
changeset 349 | 3916245a60b5 |
parent 302 | c60e0db23bd7 |
child 359 | 3cbf0e10ef48 |
permissions | -rw-r--r-- |
49 | 1 |
/* |
2 |
Task Spooler - a task queue system for the unix user |
|
267 | 3 |
Copyright (C) 2007-2009 LluĂs Batlle i Rossell |
49 | 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> |
146 | 15 |
#include <sys/time.h> |
302
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
16 |
#include <sys/stat.h> |
25 | 17 |
#include <signal.h> |
3 | 18 |
|
19 |
#include "main.h" |
|
1 | 20 |
|
21 |
extern int server_socket; |
|
22 |
||
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
23 |
static char *socket_path; |
302
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
24 |
static int should_check_owner = 0; |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
25 |
|
25 | 26 |
static int fork_server(); |
27 |
||
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
28 |
void create_socket_path(char **path) |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
29 |
{ |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
30 |
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
|
31 |
char userid[20]; |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
32 |
int size; |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
33 |
|
64 | 34 |
/* As a priority, TS_SOCKET mandates over the path creation */ |
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
35 |
*path = getenv("TS_SOCKET"); |
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
36 |
if (*path != 0) |
64 | 37 |
{ |
38 |
/* We need this in our memory, for forks and future 'free'. */ |
|
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
39 |
size = strlen(*path) + 1; |
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
40 |
*path = (char *) malloc(size); |
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
41 |
strcpy(*path, getenv("TS_SOCKET")); |
302
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
42 |
|
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
43 |
/* We don't want to check ownership of the socket here, |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
44 |
* as the user may have thought of some shared queue */ |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
45 |
should_check_owner = 0; |
64 | 46 |
return; |
47 |
} |
|
48 |
||
49 |
/* ... if the $TS_SOCKET doesn't exist ... */ |
|
50 |
||
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
51 |
/* Create the path */ |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
52 |
tmpdir = getenv("TMPDIR"); |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
53 |
if (tmpdir == NULL) |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
54 |
tmpdir = "/tmp"; |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
55 |
|
119
361b08d33762
$USER is no more used for crating the socket path. Now the UID is used.
viric@llimona
parents:
92
diff
changeset
|
56 |
sprintf(userid, "%u", (unsigned int) getuid()); |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
57 |
|
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
58 |
/* 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
|
59 |
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
|
60 |
|
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
61 |
/* Freed after preparing the socket address */ |
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
62 |
*path = (char *) malloc(size); |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
63 |
|
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
64 |
sprintf(*path, "%s/socket-ts.%s", tmpdir, userid); |
302
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
65 |
|
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
66 |
should_check_owner = 1; |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
67 |
} |
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
68 |
|
1 | 69 |
int try_connect(int s) |
70 |
{ |
|
2 | 71 |
struct sockaddr_un addr; |
72 |
int res; |
|
1 | 73 |
|
2 | 74 |
addr.sun_family = AF_UNIX; |
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
75 |
strcpy(addr.sun_path, socket_path); |
1 | 76 |
|
2 | 77 |
res = connect(s, (struct sockaddr *) &addr, sizeof(addr)); |
302
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
78 |
|
2 | 79 |
return res; |
1 | 80 |
} |
81 |
||
302
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
82 |
static void |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
83 |
try_check_ownership() |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
84 |
{ |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
85 |
int res; |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
86 |
struct stat socketstat; |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
87 |
|
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
88 |
if (!should_check_owner) |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
89 |
return; |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
90 |
|
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
91 |
res = stat(socket_path, &socketstat); |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
92 |
|
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
93 |
if (res != 0) |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
94 |
error("Cannot state the socket %s.", socket_path); |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
95 |
|
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
96 |
if (socketstat.st_uid != getuid()) |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
97 |
error("The uid %i does not own the socket %s.", getuid(), socket_path); |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
98 |
} |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
99 |
|
25 | 100 |
void wait_server_up(int fd) |
1 | 101 |
{ |
25 | 102 |
char a; |
103 |
||
104 |
read(fd, &a, 1); |
|
105 |
close(fd); |
|
1 | 106 |
} |
107 |
||
25 | 108 |
/* Returns the fd where to wait for the parent notification */ |
109 |
static int fork_server() |
|
1 | 110 |
{ |
2 | 111 |
int pid; |
25 | 112 |
int p[2]; |
1 | 113 |
|
2 | 114 |
/* !!! stdin/stdout */ |
25 | 115 |
pipe(p); |
1 | 116 |
|
2 | 117 |
pid = fork(); |
118 |
switch (pid) |
|
119 |
{ |
|
120 |
case 0: /* Child */ |
|
25 | 121 |
close(p[0]); |
3 | 122 |
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
|
123 |
/* 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
|
124 |
close(0); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
125 |
close(1); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
126 |
close(2); |
133 | 127 |
setsid(); |
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
128 |
server_main(p[1], socket_path); |
3 | 129 |
exit(0); |
2 | 130 |
break; |
131 |
case -1: /* Error */ |
|
25 | 132 |
return -1; |
2 | 133 |
default: /* Parent */ |
25 | 134 |
close(p[1]); |
2 | 135 |
} |
25 | 136 |
/* Return the read fd */ |
137 |
return p[0]; |
|
138 |
} |
|
139 |
||
140 |
void notify_parent(int fd) |
|
141 |
{ |
|
142 |
char a = 'a'; |
|
143 |
write(fd, &a, 1); |
|
144 |
close(fd); |
|
1 | 145 |
} |
146 |
||
147 |
int ensure_server_up() |
|
148 |
{ |
|
2 | 149 |
int res; |
25 | 150 |
int notify_fd; |
1 | 151 |
|
129
312083fb8899
Change PF_UNIX to AF_UNIX. According to socket(2), this is better.
viric@llimona
parents:
119
diff
changeset
|
152 |
server_socket = socket(AF_UNIX, SOCK_STREAM, 0); |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
153 |
if (server_socket == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
154 |
error("getting the server socket"); |
1 | 155 |
|
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
156 |
create_socket_path(&socket_path); |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
157 |
|
2 | 158 |
res = try_connect(server_socket); |
1 | 159 |
|
2 | 160 |
/* Good connection */ |
161 |
if (res == 0) |
|
302
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
162 |
{ |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
163 |
try_check_ownership(); |
2 | 164 |
return 1; |
302
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
165 |
} |
1 | 166 |
|
2 | 167 |
/* If error other than "No one listens on the other end"... */ |
3 | 168 |
if (!(errno == ENOENT || errno == ECONNREFUSED)) |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
169 |
error("c: cannot connect to the server"); |
1 | 170 |
|
3 | 171 |
if (errno == ECONNREFUSED) |
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
172 |
unlink(socket_path); |
3 | 173 |
|
2 | 174 |
/* Try starting the server */ |
25 | 175 |
notify_fd = fork_server(); |
176 |
wait_server_up(notify_fd); |
|
2 | 177 |
res = try_connect(server_socket); |
1 | 178 |
|
2 | 179 |
/* The second time didn't work. Abort. */ |
180 |
if (res == -1) |
|
9 | 181 |
{ |
182 |
fprintf(stderr, "The server didn't come up.\n"); |
|
183 |
exit(-1); |
|
184 |
} |
|
1 | 185 |
|
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
186 |
free(socket_path); |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
187 |
|
2 | 188 |
/* Good connection on the second time */ |
189 |
return 1; |
|
1 | 190 |
} |