Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions fcgiwrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ static int setup_socket(char *url) {
} sa;

if (!strncmp(p, "unix:", sizeof("unix:") - 1)) {
struct stat st;

p += sizeof("unix:") - 1;

if (strlen(p) >= UNIX_PATH_MAX) {
Expand All @@ -738,6 +740,36 @@ static int setup_socket(char *url) {
sockaddr_size = sizeof sa.sa_un;
sa.sa_un.sun_family = AF_UNIX;
strcpy(sa.sa_un.sun_path, p);

if (stat(p, &st) != -1) {
/* Socket already exists. See if it is still alive. */
struct sockaddr_un server;
int fd;

memset((char *)&server, '\0',
sizeof(struct sockaddr_un));
server.sun_family = AF_UNIX;
strlcpy(server.sun_path, p, sizeof(server.sun_path));

if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) {
if (connect(fd, (struct sockaddr *)&server,
sizeof(struct sockaddr_un)) >= 0) {
close(fd);
fprintf(stderr,
"Socket %s is in use by "
"another process.\n", p);
return -1;
}
close(fd);
}

fprintf(stderr, "Removing stale socket %s.\n", p);
if (unlink(p) == -1) {
fprintf(stderr,
"Could not unlink stale socket %s\n", p);
return -1;
}
}
} else if (!strncmp(p, "tcp:", sizeof("tcp:") - 1)) {
p += sizeof("tcp:") - 1;

Expand Down