29#include <sys/socket.h>
52#define YAZ_SOCKLEN_T int
63 int (*check_ip)(
void *cd,
const char *a,
int len,
int type),
72#define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
73 + strlen ((ptr)->sun_path))
118 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
133 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
136 signal (SIGPIPE, SIG_IGN);
180 add->sun_family = AF_UNIX;
181 strncpy(add->sun_path, str,
sizeof(add->sun_path)-1);
182 add->sun_path[
sizeof(add->sun_path)-1] = 0;
183 cp = strchr (add->sun_path,
':');
193 const char * file = NULL;
202 if ((eol = strchr(s,
',')))
204 if (sp->
uid == -1 && strncmp(s,
"user=", 5) == 0)
207 if (strspn(arg,
"0123456789") == strlen(arg))
213 struct passwd * pw = getpwnam(arg);
220 sp->
uid = pw->pw_uid;
223 else if (sp->
gid == -1 && strncmp(s,
"group=", 6) == 0)
226 if (strspn(arg,
"0123456789") == strlen(arg))
232 struct group * gr = getgrnam(arg);
239 sp->
gid = gr->gr_gid;
242 else if (sp->
umask == -1 && strncmp(s,
"umask=", 6) == 0)
247 sp->
umask = strtol(arg, &end, 8);
248 if (errno == EINVAL ||
256 else if (file == NULL && strncmp(s,
"file=", 5) == 0)
264 "invalid or double argument %s", s);
296 static struct sockaddr_un add;
319 struct sockaddr_un *add = (
struct sockaddr_un *)address;
330 for (i = 0; i<3; i++)
332 r = connect(h->
iofile, (
struct sockaddr *) add,
SUN_LEN(add));
338 v.tv_nsec = i*10000000L + 1000000L;
388 struct sockaddr *addr = (
struct sockaddr *)address;
389 const char * path = ((
struct sockaddr_un *)addr)->sun_path;
390 struct stat stat_buf;
394 if (stat(path, &stat_buf) != -1) {
395 struct sockaddr_un socket_unix;
398 if (!S_ISSOCK(stat_buf.st_mode)) {
403 if ((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
407 socket_unix.sun_family = AF_UNIX;
408 strncpy(socket_unix.sun_path, path,
sizeof(socket_unix.sun_path)-1);
409 socket_unix.sun_path[
sizeof(socket_unix.sun_path)-1] = 0;
410 if (connect(socket_out, (
struct sockaddr *) &socket_unix,
SUN_LEN(&socket_unix)) < 0) {
426 if (bind(h->
iofile, (
struct sockaddr *) addr,
SUN_LEN((
struct sockaddr_un *)addr)))
431 if (chown(path, sp->
uid, sp->
gid))
436 if (chmod(path, sp->
umask != -1 ? sp->
umask : 0666))
452 int (*check_ip)(
void *cd,
const char *a,
int len,
int t),
455 struct sockaddr_un addr;
464 h->
newfd = accept(h->
iofile, (
struct sockaddr*)&addr, &len);
470#
if EAGAIN != EWOULDBLOCK
480 if (addrlen && (
size_t) (*addrlen) >=
sizeof(
struct sockaddr_un))
481 memcpy(raddr, &addr, *addrlen =
sizeof(
struct sockaddr_un));
503 memcpy(cnew, h,
sizeof(*h));
518 (fcntl(cnew->
iofile, F_SETFL, O_NONBLOCK) < 0)
557#define CS_UNIX_BUFCHUNK 4096
567 int tmpi, berlen, rest, req, tomove;
568 int hasread = 0, res;
585 while (!(berlen = (*sp->
complete)(*buf, hasread)))
593 if (!(*buf =(
char *)
xrealloc(*buf, *bufsize *= 2)))
601#
if EAGAIN != EWOULDBLOCK
623 if (hasread > berlen)
625 tomove = req = hasread - berlen;
641 *(*buf + berlen) =
'\0';
642 return berlen ? berlen : 1;
665 else if (state->
towrite != size)
685#
if EAGAIN != EWOULDBLOCK
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)
struct comstack * COMSTACK
#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.
int(* f_bind)(COMSTACK handle, void *address, int mode)
COMSTACK(* f_accept)(COMSTACK handle)
void(* f_close)(COMSTACK handle)
void *(* f_straddr)(COMSTACK handle, const char *str)
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)
int(* f_more)(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)
const char *(* f_addrstr)(COMSTACK handle)
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)
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.