version 1.11, 2009/09/27 11:51:20 |
version 1.12, 2011/04/19 00:02:29 |
|
|
#include <sys/socket.h> |
#include <sys/socket.h> |
#include <sys/stat.h> |
#include <sys/stat.h> |
#include <sys/un.h> |
#include <sys/un.h> |
|
|
#include <err.h> |
#include <err.h> |
#include <errno.h> |
#include <errno.h> |
#include <fcntl.h> |
#include <fcntl.h> |
|
|
listen_revents |
listen_revents |
}; |
}; |
|
|
struct listen * |
void |
listen_new(struct fileops *ops, char *path) |
listen_new_un(char *path) |
{ |
{ |
int sock, oldumask; |
int sock, oldumask; |
struct sockaddr_un sockname; |
struct sockaddr_un sockname; |
|
|
sock = socket(AF_UNIX, SOCK_STREAM, 0); |
sock = socket(AF_UNIX, SOCK_STREAM, 0); |
if (sock < 0) { |
if (sock < 0) { |
perror("socket"); |
perror("socket"); |
return NULL; |
exit(1); |
} |
} |
if (unlink(path) < 0 && errno != ENOENT) { |
if (unlink(path) < 0 && errno != ENOENT) { |
perror("unlink"); |
perror("unlink"); |
|
|
perror("listen"); |
perror("listen"); |
goto bad_close; |
goto bad_close; |
} |
} |
f = (struct listen *)file_new(ops, path, 1); |
f = (struct listen *)file_new(&listen_ops, path, 1); |
if (f == NULL) |
if (f == NULL) |
goto bad_close; |
goto bad_close; |
f->path = strdup(path); |
f->path = strdup(path); |
|
|
exit(1); |
exit(1); |
} |
} |
f->fd = sock; |
f->fd = sock; |
return f; |
return; |
bad_close: |
bad_close: |
close(sock); |
close(sock); |
return NULL; |
exit(1); |
} |
} |
|
|
int |
int |
|
|
caddrlen = sizeof(caddrlen); |
caddrlen = sizeof(caddrlen); |
sock = accept(f->fd, &caddr, &caddrlen); |
sock = accept(f->fd, &caddr, &caddrlen); |
if (sock < 0) { |
if (sock < 0) { |
|
/* XXX: should we kill the socket here ? */ |
perror("accept"); |
perror("accept"); |
return 0; |
return 0; |
} |
} |
|
|
{ |
{ |
struct listen *f = (struct listen *)file; |
struct listen *f = (struct listen *)file; |
|
|
unlink(f->path); |
if (f->path != NULL) { |
free(f->path); |
unlink(f->path); |
|
free(f->path); |
|
} |
close(f->fd); |
close(f->fd); |
|
} |
|
|
|
void |
|
listen_closeall(void) |
|
{ |
|
struct file *f, *fnext; |
|
|
|
for (f = LIST_FIRST(&file_list); f != NULL; f = fnext) { |
|
fnext = LIST_NEXT(f, entry); |
|
if (f->ops == &listen_ops) |
|
file_close(f); |
|
} |
} |
} |