29#include <sys/socket.h>
53#define YAZ_SOCKLEN_T int
64 int (*check_ip)(
void *cd,
const char *a,
int len,
int type),
73#define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
74 + strlen ((ptr)->sun_path))
98 static int log_level_set = 0;
124 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
139 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
142 signal (SIGPIPE, SIG_IGN);
188 add->sun_family = AF_UNIX;
189 strncpy(add->sun_path, str,
sizeof(add->sun_path)-1);
190 add->sun_path[
sizeof(add->sun_path)-1] = 0;
191 cp = strchr (add->sun_path,
':');
201 const char * file = NULL;
210 if ((eol = strchr(s,
',')))
212 if (sp->
uid == -1 && strncmp(s,
"user=", 5) == 0)
215 if (strspn(arg,
"0123456789") == strlen(arg))
221 struct passwd * pw = getpwnam(arg);
228 sp->
uid = pw->pw_uid;
231 else if (sp->
gid == -1 && strncmp(s,
"group=", 6) == 0)
234 if (strspn(arg,
"0123456789") == strlen(arg))
240 struct group * gr = getgrnam(arg);
247 sp->
gid = gr->gr_gid;
250 else if (sp->
umask == -1 && strncmp(s,
"umask=", 6) == 0)
255 sp->
umask = strtol(arg, &end, 8);
256 if (errno == EINVAL ||
264 else if (file == NULL && strncmp(s,
"file=", 5) == 0)
272 "invalid or double argument %s", s);
304 static struct sockaddr_un add;
339 struct sockaddr_un *add = (
struct sockaddr_un *)address;
350 for (i = 0; i<3; i++)
352 r = connect(h->
iofile, (
struct sockaddr *) add,
SUN_LEN(add));
358 v.tv_nsec = i*10000000L + 1000000L;
408 struct sockaddr *addr = (
struct sockaddr *)address;
409 const char * path = ((
struct sockaddr_un *)addr)->sun_path;
410 struct stat stat_buf;
414 if (stat(path, &stat_buf) != -1)
416 struct sockaddr_un socket_unix;
419 if (!S_ISSOCK(stat_buf.st_mode))
425 if ((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
430 socket_unix.sun_family = AF_UNIX;
431 strncpy(socket_unix.sun_path, path,
sizeof(socket_unix.sun_path)-1);
432 socket_unix.sun_path[
sizeof(socket_unix.sun_path)-1] = 0;
433 if (connect(socket_out, (
struct sockaddr *) &socket_unix,
SUN_LEN(&socket_unix)) < 0)
453 if (bind(h->
iofile, (
struct sockaddr *) addr,
SUN_LEN((
struct sockaddr_un *)addr)))
458 if (chown(path, sp->
uid, sp->
gid))
463 if (chmod(path, sp->
umask != -1 ? sp->
umask : 0666))
479 int (*check_ip)(
void *cd,
const char *a,
int len,
int t),
482 struct sockaddr_un addr;
491 h->
newfd = accept(h->
iofile, (
struct sockaddr*)&addr, &len);
497#
if EAGAIN != EWOULDBLOCK
507 if (addrlen && (
size_t) (*addrlen) >=
sizeof(
struct sockaddr_un))
508 memcpy(raddr, &addr, *addrlen =
sizeof(
struct sockaddr_un));
530 memcpy(cnew, h,
sizeof(*h));
546 (fcntl(cnew->
iofile, F_SETFL, O_NONBLOCK) < 0)
585#define CS_UNIX_BUFCHUNK 4096
595 int tmpi, berlen, rest, req, tomove;
596 int hasread = 0, res;
613 while ((berlen = (*sp->
complete)(*buf, hasread)) == 0)
625 *bufsize = *bufsize * 2;
632 if (!(*buf = (
char *)
xrealloc(*buf, *bufsize)))
644#
if EAGAIN != EWOULDBLOCK
676 if (hasread > berlen)
678 tomove = req = hasread - berlen;
694 *(*buf + berlen) =
'\0';
695 return berlen ? berlen : 1;
718 else if (state->
towrite != size)
738#
if EAGAIN != EWOULDBLOCK
786 if (p->
flags == flags)
788 flag = fcntl(p->
iofile, F_GETFL, 0);
790 flag = flag & ~O_NONBLOCK;
792 flag = flag | O_NONBLOCK;
793 if (fcntl(p->
iofile, F_SETFL, flag) < 0)
Private Header for COMSTACK.
int yaz_unix_set_head_only(COMSTACK cs, int head_only)
int cs_complete_auto(const char *buf, int len)
Finds the length of a complete BER or HTTP message.
int cs_complete_auto_head(const char *buf, int len)
Finds the length of a BER message or HTTP headers.
struct comstack * COMSTACK
Communication endpoint; release with cs_close.
#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
void yaz_snprintf(char *buf, size_t size, const char *fmt,...)
Header for config file reading utilities.
Public endpoint state and transport dispatch table.
COMSTACK(*) f_accept(COMSTACK handle)
const char *(*) f_addrstr(COMSTACK handle)
int(*) f_rcvconnect(COMSTACK handle)
int(*) f_bind(COMSTACK handle, void *address, int mode)
int(*) f_put(COMSTACK handle, char *buf, int size)
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_connect(COMSTACK handle, void *address)
int(*) f_more(COMSTACK handle)
void *(*) f_straddr(COMSTACK handle, const char *str)
int(*) f_get(COMSTACK handle, char **buf, int *bufsize)
int(*) f_set_blocking(COMSTACK handle, int blocking)
void(*) f_close(COMSTACK handle)
int(*) complete(const char *buf, int len)
static void cs_set_error(COMSTACK cs, int error, const char *details)
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)
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 void * unix_straddr1(COMSTACK h, const char *str, char *f)
static int unix_more(COMSTACK h)
static const char * unix_addrstr(COMSTACK h)
static int unix_set_blocking(COMSTACK p, int blocking)
struct sockaddr_un * unix_strtoaddr(const char *str)
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.