30 #include <sys/socket.h>
35 #include <linux/limits.h>
78 static void *unsock_client(
void *data) {
82 fd_set rd_set, act_set;
91 fdf = fcntl(fd, F_GETFL);
92 fcntl(fd, F_SETFD, fdf | O_NONBLOCK);
94 setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on,
sizeof(on));
102 selfd = select(fd + 1, &act_set, NULL, NULL, &tv);
105 if ((selfd < 0 && errno == EINTR) || (!selfd)) {
107 }
else if (selfd < 0) {
111 if (FD_ISSET(sock->
sock, &act_set) && unsock->
client) {
120 static void unixclient_sockthread_free(
void *data) {
141 if (!(unsock =
objalloc(
sizeof(*unsock), unixclient_sockthread_free))) {
161 static void *unsock_serv(
void *data) {
167 fd_set rd_set, act_set;
180 fdf = fcntl(fd, F_GETFL);
181 fcntl(fd, F_SETFD, fdf | O_NONBLOCK);
184 memset(&adr->
un, 0,
sizeof(adr->
un));
185 adr->
un.sun_family = PF_UNIX;
186 salen =
sizeof(adr->
un);
187 strncpy((
char *)adr->
un.sun_path, unsock->
sockpath,
sizeof(adr->
un.sun_path) -1);
190 setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on,
sizeof(on));
192 if (bind(fd, (
struct sockaddr *)&adr->
un, salen)) {
193 if (errno == EADDRINUSE) {
196 if (bind(fd, (
struct sockaddr *)&adr->
un,
sizeof(
struct sockaddr_un))) {
208 if (unsock->
protocol == SOCK_STREAM) {
209 if (listen(fd, 10)) {
224 selfd = select(fd + 1, &act_set, NULL, NULL, &tv);
227 if ((selfd < 0 && errno == EINTR) || (!selfd)) {
229 }
else if (selfd < 0) {
233 if (FD_ISSET(fd, &act_set)) {
234 if (unsock->
protocol == SOCK_STREAM) {
236 if (!(new_unixclientthread(newsock, NULL, unsock->
read, unsock->
data))) {
240 }
else if (unsock->
read) {
253 static void free_unixserv(
void *data) {
280 if (!(unsock =
objalloc(
sizeof(*unsock), free_unixserv))) {
284 strncpy(unsock->
sockpath, sock, UNIX_PATH_MAX);
319 if (!(fws =
make_socket(PF_UNIX, protocol, 0, NULL))) {
324 if (protocol == SOCK_DGRAM) {
326 omask = umask(S_IXUSR | S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
327 tmpsock = basename((
char*)sock);
328 temp = tempnam(NULL, tmpsock);
338 salen =
sizeof(caddr.
un);
339 memset(&caddr.
un, 0, salen);
340 caddr.
un.sun_family = PF_UNIX;
341 strncpy((
char *)caddr.
un.sun_path, temp,
sizeof(caddr.
un.sun_path) -1);
343 if (bind(fws->
sock, (
struct sockaddr *)&caddr.
un, salen)) {
361 salen =
sizeof(saddr->
un);
362 memset(&saddr->
un, 0, salen);
363 saddr->
un.sun_family = PF_UNIX;
364 strncpy((
char *)saddr->
un.sun_path, sock,
sizeof(saddr->
un.sun_path) -1);
366 if (connect(fws->
sock, (
struct sockaddr *)&saddr->
un, salen)) {
378 if (!(new_unixclientthread(fws, temp, read, data))) {
389 return (
objref(fws)) ? fws : NULL;
int protocol
Socket protocol.
int strlenzero(const char *str)
Check if a string is zero length.
union sockstruct addr
system socket data structure.
char sockpath[UNIX_PATH_MAX+1]
Socket path.
struct sockaddr_un un
Unix sockets.
int objref(void *data)
Reference a object.
Unix socket client data structure.
struct fwsocket * sock
Socket reference.
struct fwsocket * unixsocket_client(const char *sock, int protocol, socketrecv read, void *data)
Create a client thread on the socket.
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
socketrecv client
Client read callback.
const char * endpoint
Client endpoint tmp for SOCK_DGRAM.
struct fwsocket * sock
Socket reference.
void * data
Data reference passed to callback.
DTS Application library API Include file.
void(* socketrecv)(struct fwsocket *, void *)
Callback function to register with a socket that will be called when there is data available...
struct thread_pvt * framework_mkthread(threadfunc, threadcleanup, threadsighandler, void *data, int flags)
create a thread result must be unreferenced
Return reference to thread this must be unreferenced.
Unix socket server data structure.
socketrecv read
Thread to begin on client connect.
void * data
Data reference passed to callback.
struct fwsocket * make_socket(int family, int type, int proto, void *ssl)
Allocate a socket structure and return reference.
int framework_threadok(void)
let threads check there status.
struct fwsocket * unixsocket_server(const char *sock, int protocol, int mask, socketrecv read, void *data)
Create and run UNIX server socket thread.
struct fwsocket * accept_socket(struct fwsocket *sock)
Create and return a socket structure from accept()
Socket union describing all address types.
enum sock_flags flags
Socket control flags.
int objunref(void *data)
Drop reference held.