41
41
42
42
#include "su.h"
43
43
44
- //extern char* _mktemp(char*); /* mktemp doesn't link right. Don't ask me why. */
45
-
46
44
/* Still lazt, will fix this */
47
- static char * socket_path = NULL ;
45
+ static char socket_path [ PATH_MAX ] ;
48
46
49
47
static struct su_initiator su_from = {
50
48
.pid = -1 ,
@@ -139,11 +137,9 @@ static void cleanup_signal(int sig)
139
137
exit (sig );
140
138
}
141
139
142
- static int socket_create_temp (void )
140
+ static int socket_create_temp (char * path , size_t len )
143
141
{
144
- static char buf [PATH_MAX ];
145
142
int fd ;
146
-
147
143
struct sockaddr_un sun ;
148
144
149
145
fd = socket (AF_LOCAL , SOCK_STREAM , 0 );
@@ -152,29 +148,32 @@ static int socket_create_temp(void)
152
148
return -1 ;
153
149
}
154
150
155
- for (;;) {
156
- memset ( & sun , 0 , sizeof ( sun )) ;
157
- sun . sun_family = AF_LOCAL ;
158
- strcpy ( buf , SOCKET_PATH_TEMPLATE );
159
- socket_path = mktemp ( buf );
160
- snprintf ( sun . sun_path , sizeof ( sun . sun_path ), "%s" , socket_path );
161
-
162
- if ( bind ( fd , ( struct sockaddr * ) & sun , sizeof ( sun )) < 0 ) {
163
- if ( errno != EADDRINUSE ) {
164
- PLOGE ( "bind" );
165
- return -1 ;
166
- }
167
- } else {
168
- break ;
169
- }
151
+ memset ( & sun , 0 , sizeof ( sun ));
152
+ sun . sun_family = AF_LOCAL ;
153
+ snprintf ( path , len , "%s/.socket%d" , REQUESTOR_CACHE_PATH , getpid ()) ;
154
+ snprintf ( sun . sun_path , sizeof ( sun . sun_path ), "%s" , path );
155
+
156
+ /*
157
+ * Delete the socket to protect from situations when
158
+ * something bad occured previously and the kernel reused pid from that process.
159
+ * Small probability, isn't it.
160
+ */
161
+ unlink ( sun . sun_path ) ;
162
+
163
+ if ( bind ( fd , ( struct sockaddr * ) & sun , sizeof ( sun )) < 0 ) {
164
+ PLOGE ( "bind" ) ;
165
+ goto err ;
170
166
}
171
167
172
168
if (listen (fd , 1 ) < 0 ) {
173
169
PLOGE ("listen" );
174
- return -1 ;
170
+ goto err ;
175
171
}
176
172
177
173
return fd ;
174
+ err :
175
+ close (fd );
176
+ return -1 ;
178
177
}
179
178
180
179
static int socket_accept (int serv_fd )
@@ -397,7 +396,7 @@ int main(int argc, char *argv[])
397
396
default : deny ();
398
397
}
399
398
400
- socket_serv_fd = socket_create_temp ();
399
+ socket_serv_fd = socket_create_temp (socket_path , sizeof ( socket_path ) );
401
400
if (socket_serv_fd < 0 ) {
402
401
deny ();
403
402
}
0 commit comments