author | Lluís Batlle i Rossell <viric@viric.name> |
Mon, 01 Apr 2024 11:06:42 +0200 | |
changeset 379 | 1f8972dab2c1 |
parent 373 | 453b2b0a833c |
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 |
|
369
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
26 |
static int fork_server(int ls); |
25 | 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 |
|
359
3cbf0e10ef48
Quick fix for Bug#975321
Alexander Inyukhin <shurick@sectorb.msk.ru>
parents:
302
diff
changeset
|
74 |
memset(&addr, 0, sizeof(addr)); |
2 | 75 |
addr.sun_family = AF_UNIX; |
359
3cbf0e10ef48
Quick fix for Bug#975321
Alexander Inyukhin <shurick@sectorb.msk.ru>
parents:
302
diff
changeset
|
76 |
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); |
3cbf0e10ef48
Quick fix for Bug#975321
Alexander Inyukhin <shurick@sectorb.msk.ru>
parents:
302
diff
changeset
|
77 |
if (strcmp(socket_path, addr.sun_path)) |
3cbf0e10ef48
Quick fix for Bug#975321
Alexander Inyukhin <shurick@sectorb.msk.ru>
parents:
302
diff
changeset
|
78 |
error("Cannot create the socket '%s'. Probably, the name is too long.", socket_path); |
1 | 79 |
|
2 | 80 |
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
|
81 |
|
2 | 82 |
return res; |
1 | 83 |
} |
84 |
||
302
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
85 |
static void |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
86 |
try_check_ownership() |
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 |
int res; |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
89 |
struct stat socketstat; |
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 |
if (!should_check_owner) |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
92 |
return; |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
93 |
|
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
94 |
res = stat(socket_path, &socketstat); |
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 (res != 0) |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
97 |
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
|
98 |
|
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
99 |
if (socketstat.st_uid != getuid()) |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
100 |
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
|
101 |
} |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
102 |
|
25 | 103 |
void wait_server_up(int fd) |
1 | 104 |
{ |
25 | 105 |
char a; |
106 |
||
107 |
read(fd, &a, 1); |
|
108 |
close(fd); |
|
1 | 109 |
} |
110 |
||
25 | 111 |
/* Returns the fd where to wait for the parent notification */ |
369
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
112 |
static int fork_server(int ls) |
1 | 113 |
{ |
2 | 114 |
int pid; |
25 | 115 |
int p[2]; |
1 | 116 |
|
2 | 117 |
/* !!! stdin/stdout */ |
25 | 118 |
pipe(p); |
1 | 119 |
|
2 | 120 |
pid = fork(); |
121 |
switch (pid) |
|
122 |
{ |
|
123 |
case 0: /* Child */ |
|
25 | 124 |
close(p[0]); |
3 | 125 |
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
|
126 |
/* 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
|
127 |
close(0); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
128 |
close(1); |
f70a27afd92e
Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents:
64
diff
changeset
|
129 |
close(2); |
133 | 130 |
setsid(); |
369
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
131 |
server_main(p[1], socket_path, ls); |
3 | 132 |
exit(0); |
2 | 133 |
break; |
134 |
case -1: /* Error */ |
|
25 | 135 |
return -1; |
2 | 136 |
default: /* Parent */ |
369
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
137 |
close(ls); |
25 | 138 |
close(p[1]); |
2 | 139 |
} |
25 | 140 |
/* Return the read fd */ |
141 |
return p[0]; |
|
142 |
} |
|
143 |
||
144 |
void notify_parent(int fd) |
|
145 |
{ |
|
146 |
char a = 'a'; |
|
147 |
write(fd, &a, 1); |
|
148 |
close(fd); |
|
1 | 149 |
} |
150 |
||
368
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
151 |
static int is_path_unixsocket(const char *path) |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
152 |
{ |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
153 |
int res; |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
154 |
struct stat socketstat; |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
155 |
|
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
156 |
res = stat(path, &socketstat); |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
157 |
|
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
158 |
if (res == 0 && S_ISSOCK(socketstat.st_mode)) |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
159 |
return 1; |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
160 |
|
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
161 |
return 0; |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
162 |
} |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
163 |
|
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
164 |
static int open_server_socket() |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
165 |
{ |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
166 |
int res; |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
167 |
int ls; |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
168 |
struct sockaddr_un addr; |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
169 |
|
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
170 |
ls = socket(AF_UNIX, SOCK_STREAM, 0); |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
171 |
if(ls == -1) |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
172 |
error("cannot create the listen socket in the server"); |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
173 |
|
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
174 |
addr.sun_family = AF_UNIX; |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
175 |
strcpy(addr.sun_path, socket_path); |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
176 |
|
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
177 |
res = bind(ls, (struct sockaddr *) &addr, sizeof(addr)); |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
178 |
if (res == -1) |
369
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
179 |
{ |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
180 |
warning("Error binding."); |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
181 |
close(ls); |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
182 |
return -1; |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
183 |
} |
368
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
184 |
|
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
185 |
res = listen(ls, 0); |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
186 |
if (res == -1) |
369
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
187 |
{ |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
188 |
/* If bind succeeded, can this fail? */ |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
189 |
warning("Error listening."); |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
190 |
close(ls); |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
191 |
return -1; |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
192 |
} |
368
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
193 |
|
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
194 |
return ls; |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
195 |
} |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
196 |
|
1 | 197 |
int ensure_server_up() |
198 |
{ |
|
2 | 199 |
int res; |
25 | 200 |
int notify_fd; |
369
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
201 |
int server_bind_fd = -1; |
1 | 202 |
|
129
312083fb8899
Change PF_UNIX to AF_UNIX. According to socket(2), this is better.
viric@llimona
parents:
119
diff
changeset
|
203 |
server_socket = socket(AF_UNIX, SOCK_STREAM, 0); |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
204 |
if (server_socket == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
66
diff
changeset
|
205 |
error("getting the server socket"); |
1 | 206 |
|
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
207 |
create_socket_path(&socket_path); |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
208 |
|
2 | 209 |
res = try_connect(server_socket); |
1 | 210 |
|
2 | 211 |
/* Good connection */ |
212 |
if (res == 0) |
|
302
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
213 |
{ |
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
214 |
try_check_ownership(); |
2 | 215 |
return 1; |
302
c60e0db23bd7
Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents:
267
diff
changeset
|
216 |
} |
1 | 217 |
|
2 | 218 |
/* If error other than "No one listens on the other end"... */ |
3 | 219 |
if (!(errno == ENOENT || errno == ECONNREFUSED)) |
373
453b2b0a833c
Fix error message about bad ts_socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
369
diff
changeset
|
220 |
error("c: cannot connect to the server at '%s': %s", |
453b2b0a833c
Fix error message about bad ts_socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
369
diff
changeset
|
221 |
socket_path, strerror(errno)); |
1 | 222 |
|
3 | 223 |
if (errno == ECONNREFUSED) |
368
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
224 |
{ |
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
225 |
if (!is_path_unixsocket(socket_path)) |
373
453b2b0a833c
Fix error message about bad ts_socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
369
diff
changeset
|
226 |
error("The socket path exists and it is not a unix socket: %s", |
453b2b0a833c
Fix error message about bad ts_socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
369
diff
changeset
|
227 |
socket_path); |
453b2b0a833c
Fix error message about bad ts_socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
369
diff
changeset
|
228 |
|
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
229 |
unlink(socket_path); |
368
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
230 |
} |
3 | 231 |
|
369
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
232 |
/* Try to start a server. It may fail if multiple clients try to do this at oncei. */ |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
233 |
server_bind_fd = open_server_socket(); |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
234 |
if (server_bind_fd != -1) |
368
8dc7e38d782c
Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents:
359
diff
changeset
|
235 |
{ |
369
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
236 |
/* If it worked, wait for server to start. Otherwise, repeat connect */ |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
237 |
notify_fd = fork_server(server_bind_fd); |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
238 |
wait_server_up(notify_fd); |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
239 |
} |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
240 |
|
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
241 |
/* Connect after either server start or reattempt because of clash at start */ |
2 | 242 |
res = try_connect(server_socket); |
1 | 243 |
|
2 | 244 |
/* The second time didn't work. Abort. */ |
245 |
if (res == -1) |
|
9 | 246 |
{ |
247 |
fprintf(stderr, "The server didn't come up.\n"); |
|
248 |
exit(-1); |
|
249 |
} |
|
1 | 250 |
|
369
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
251 |
try_check_ownership(); |
040912c941e4
Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents:
368
diff
changeset
|
252 |
|
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
253 |
free(socket_path); |
28
107abb4ec98a
Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents:
25
diff
changeset
|
254 |
|
2 | 255 |
/* Good connection on the second time */ |
256 |
return 1; |
|
1 | 257 |
} |