23 #include <sys/types.h>
29 #include <sys/socket.h>
51 #define YAZ_SOCKLEN_T int
62 int (*check_ip)(
void *cd,
const char *a,
int len,
int type),
71 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
72 + strlen ((ptr)->sun_path))
85 struct sockaddr_un
addr;
117 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
132 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
135 signal (SIGPIPE, SIG_IGN);
179 add->sun_family = AF_UNIX;
180 strncpy(add->sun_path, str,
sizeof(add->sun_path)-1);
181 add->sun_path[
sizeof(add->sun_path)-1] = 0;
182 cp = strchr (add->sun_path,
':');
192 const char * file = NULL;
201 if ((eol = strchr(s,
',')))
203 if (sp->
uid == -1 && strncmp(s,
"user=", 5) == 0)
206 if (strspn(arg,
"0123456789") == strlen(arg))
212 struct passwd * pw = getpwnam(arg);
219 sp->
uid = pw->pw_uid;
222 else if (sp->
gid == -1 && strncmp(s,
"group=", 6) == 0)
225 if (strspn(arg,
"0123456789") == strlen(arg))
231 struct group * gr = getgrnam(arg);
238 sp->
gid = gr->gr_gid;
241 else if (sp->
umask == -1 && strncmp(s,
"umask=", 6) == 0)
246 sp->
umask = strtol(arg, &end, 8);
247 if (errno == EINVAL ||
255 else if (file == NULL && strncmp(s,
"file=", 5) == 0)
263 "invalid or double argument %s", s);
295 static struct sockaddr_un add;
318 struct sockaddr_un *add = (
struct sockaddr_un *)address;
329 for (i = 0; i<3; i++)
331 r = connect(h->
iofile, (
struct sockaddr *) add,
SUN_LEN(add));
337 v.tv_nsec = i*10000000L + 1000000L;
387 struct sockaddr *addr = (
struct sockaddr *)address;
388 const char * path = ((
struct sockaddr_un *)addr)->sun_path;
389 struct stat stat_buf;
393 if (stat(path, &stat_buf) != -1) {
394 struct sockaddr_un socket_unix;
397 if (!S_ISSOCK(stat_buf.st_mode)) {
402 if ((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
406 socket_unix.sun_family = AF_UNIX;
407 strncpy(socket_unix.sun_path, path,
sizeof(socket_unix.sun_path)-1);
408 socket_unix.sun_path[
sizeof(socket_unix.sun_path)-1] = 0;
409 if (connect(socket_out, (
struct sockaddr *) &socket_unix,
SUN_LEN(&socket_unix)) < 0) {
425 if (bind(h->
iofile, (
struct sockaddr *) addr,
SUN_LEN((
struct sockaddr_un *)addr)))
430 if (chown(path, sp->
uid, sp->
gid))
435 if (chmod(path, sp->
umask != -1 ? sp->
umask : 0666))
451 int (*check_ip)(
void *cd,
const char *a,
int len,
int t),
454 struct sockaddr_un addr;
463 h->
newfd = accept(h->
iofile, (
struct sockaddr*)&addr, &len);
469 #
if EAGAIN != EWOULDBLOCK
479 if (addrlen && (
size_t) (*addrlen) >=
sizeof(
struct sockaddr_un))
480 memcpy(raddr, &addr, *addrlen =
sizeof(
struct sockaddr_un));
502 memcpy(cnew, h,
sizeof(*h));
517 (fcntl(cnew->
iofile, F_SETFL, O_NONBLOCK) < 0)
556 #define CS_UNIX_BUFCHUNK 4096
566 int tmpi, berlen, rest, req, tomove;
567 int hasread = 0, res;
584 while (!(berlen = (*sp->
complete)(*buf, hasread)))
592 if (!(*buf =(
char *)
xrealloc(*buf, *bufsize *= 2)))
600 #
if EAGAIN != EWOULDBLOCK
622 if (hasread > berlen)
624 tomove = req = hasread - berlen;
640 *(*buf + berlen) =
'\0';
641 return berlen ? berlen : 1;
664 else if (state->
towrite != size)
684 #if EAGAIN != EWOULDBLOCK
725 sprintf(
buf,
"unix:%s", sp->
addr.sun_path);
733 if (p->
flags == flags)
735 flag = fcntl(p->
iofile, F_GETFL, 0);
737 flag = flag & ~O_NONBLOCK;
739 flag = flag | O_NONBLOCK;
740 if (fcntl(p->
iofile, F_SETFL, flag) < 0)
int cs_complete_auto(const char *buf, int len)
#define CS_FLAGS_BLOCKING
int yaz_errno(void)
returns errno
void yaz_set_errno(int v)
sets errno to value
Header for errno utilities.
void yaz_log(int level, const char *fmt,...)
Writes log message.
int yaz_log_module_level(const char *name)
returns level for module
int(* f_bind)(COMSTACK handle, void *address, int mode)
COMSTACK(* f_accept)(COMSTACK handle)
void(* f_close)(COMSTACK handle)
int(* f_listen)(COMSTACK h, char *raddr, int *addrlen, int(*check_ip)(void *cd, const char *a, int len, int type), void *cd)
int(* f_put)(COMSTACK handle, char *buf, int size)
void *(* f_straddr)(COMSTACK handle, const char *str)
int(* f_more)(COMSTACK handle)
const char *(* f_addrstr)(COMSTACK handle)
int(* f_set_blocking)(COMSTACK handle, int blocking)
int(* f_rcvconnect)(COMSTACK handle)
int(* f_connect)(COMSTACK handle, void *address)
int(* f_get)(COMSTACK handle, char **buf, int *bufsize)
int(* complete)(const char *buf, int len)
static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
static int unix_rcvconnect(COMSTACK h)
static int unix_bind(COMSTACK h, void *address, int mode)
static int unix_put(COMSTACK h, char *buf, int size)
static void unix_close(COMSTACK h)
static int unix_get(COMSTACK h, char **buf, int *bufsize)
struct unix_state unix_state
static COMSTACK unix_accept(COMSTACK h)
static void * unix_straddr(COMSTACK h, const char *str)
static int unix_listen(COMSTACK h, char *raddr, int *addrlen, int(*check_ip)(void *cd, const char *a, int len, int type), void *cd)
static int unix_connect(COMSTACK h, void *address)
static int unix_more(COMSTACK h)
struct sockaddr_un * unix_strtoaddr(const char *str)
static void * unix_straddr1(COMSTACK h, const char *str, char *f)
static const char * unix_addrstr(COMSTACK h)
static int unix_set_blocking(COMSTACK p, int blocking)
static void unix_init(void)
COMSTACK unix_type(int s, int flags, int protocol, void *vp)
Header for UNIX domain socket COMSTACK.
#define xstrdup(s)
utility macro which calls xstrdup_f
#define xfree(x)
utility macro which calls xfree_f
#define xrealloc(o, x)
utility macro which calls xrealloc_f
#define xmalloc(x)
utility macro which calls malloc_f
Header with fundamental macros.