1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
/*
** Copyright 2001 Double Precision, Inc. See COPYING for
** distribution information.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "pcp.h"
#if HAVE_DIRENT_H
#include <dirent.h>
#else
#define dirent direct
#if HAVE_SYS_NDIR_H
#include <sys/ndir.h>
#endif
#if HAVE_SYS_DIR_H
#include <sys/dir.h>
#endif
#if HAVE_NDIR_H
#include <ndir.h>
#endif
#endif
#ifndef SOMAXCONN
#define SOMAXCONN 5
#endif
int pcp_mksocket(const char *dir, const char *sockname)
{
int fd;
const char *priority;
DIR *dirp;
struct dirent *de;
char *p;
struct sockaddr_un skun;
p=malloc(strlen(sockname)+sizeof("_PRIORITY"));
if (!p)
{
perror("malloc");
return (-1);
}
strcat(strcpy(p, sockname), "_PRIORITY");
priority=getenv(p);
free(p);
if (!priority || !*priority)
priority="50";
/* Delete the previous socket */
dirp=opendir(dir);
while (dirp && (de=readdir(dirp)))
{
const char *n=de->d_name;
while (*n && isdigit((int)(unsigned char)*n))
++n;
if (strcmp(n, sockname))
continue;
p=malloc(strlen(dir)+strlen(de->d_name)+2);
if (!p)
{
perror("malloc");
closedir(dirp);
return (-1);
}
strcat(strcat(strcpy(p, dir), "/"), de->d_name);
unlink(p);
free(p);
}
if (dirp)
closedir(dirp);
fd=socket(PF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
{
fprintf(stderr, "ALERT: socket failed: %s\n", strerror(errno));
return (-1);
}
skun.sun_family=AF_UNIX;
strcpy(skun.sun_path, dir);
strcat(skun.sun_path, "/");
strcat(skun.sun_path, priority);
strcat(skun.sun_path, sockname);
if (bind(fd, (const struct sockaddr *)&skun, sizeof(skun)) ||
listen(fd, SOMAXCONN) ||
chmod(skun.sun_path, 0777) ||
fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
{
fprintf(stderr, "ALERT: %s\n", skun.sun_path);
close(fd);
return (-1);
}
return (fd);
}
|