Simplify handling of /DETACH socket path
init_socketpath() was building a sprintf() format string intended to be used by /DETACH to create the socket file name. This included the actual socket path, plus a %d for the port, plus the sanitised tty name and hostname. Only one caller needed all this though - the /DETACH command - and the other callers (in scr-bx.c) just wanted to truncate it to the actual socket path. The format string also wasn't safe - if the home directory path, hostname or ttyname contained % characters these werent being escaped. It simplifies things to have init_socketpath() just return the actual socket path, after creating the 'screens' directory if necessary. This lets the code in scr-bx.c use it as-is, and removes the need for the global socket_path variable. The code to include the sanitised tty name and hostname in the socket file name can be moved to the create_ipc_socket() function. There's no need to check access() for the socket path before trying to create it - just call mkdir() regardless, since it will fail if the path already exists, which is fine. This commit also adds error handling to the create_ipc_socket() function for the case where creation of the socket file fails, and switches the chmod() and chown() for the opened file to the more appropriate fchmod() and fchown().
This commit is contained in:
@@ -2871,38 +2871,16 @@ char *BX_stripdev(char *ttynam)
|
||||
return ttynam;
|
||||
}
|
||||
|
||||
|
||||
void init_socketpath(void)
|
||||
const char *init_socketpath(void)
|
||||
{
|
||||
#if !defined(__EMX__) && !defined(WINNT)
|
||||
struct stat st;
|
||||
extern char socket_path[], attach_ttyname[];
|
||||
static char socket_path[BIG_BUFFER_SIZE];
|
||||
|
||||
sprintf(socket_path, "%s/.BitchX/screens", my_path);
|
||||
if (access(socket_path, F_OK))
|
||||
{
|
||||
if (mkdir(socket_path, 0700) != -1)
|
||||
(void) chown(socket_path, getuid(), getgid());
|
||||
else
|
||||
return;
|
||||
}
|
||||
if (stat(socket_path, &st) != -1)
|
||||
{
|
||||
char host[BIG_BUFFER_SIZE+1];
|
||||
char *ap;
|
||||
if (!S_ISDIR(st.st_mode))
|
||||
return;
|
||||
gethostname(host, BIG_BUFFER_SIZE);
|
||||
if ((ap = strchr(host, '.')))
|
||||
*ap = 0;
|
||||
ap = &socket_path[strlen(socket_path)];
|
||||
sprintf(ap, "/%%d.%s.%s", stripdev(attach_ttyname), host);
|
||||
ap++;
|
||||
for ( ; *ap; ap++)
|
||||
if (*ap == '/')
|
||||
*ap = '-';
|
||||
}
|
||||
#endif
|
||||
snprintf(socket_path, sizeof socket_path, "%s/.BitchX/screens", my_path);
|
||||
|
||||
if (mkdir(socket_path, 0700) != -1)
|
||||
(void)chown(socket_path, getuid(), getgid());
|
||||
|
||||
return socket_path;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user