xref: /plan9/sys/src/ape/lib/bsd/_sock_srv.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 /* posix */
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 
10 /* socket extensions */
11 #include <sys/uio.h>
12 #include <sys/socket.h>
13 
14 #include "priv.h"
15 
16 void
17 _sock_srvname(char *npath, char *path)
18 {
19 	char *p;
20 
21 	strcpy(npath, "/srv/UD.");
22 	p = strrchr(path, '/');
23 	if(p == 0)
24 		p = path;
25 	else
26 		p++;
27 	strcat(npath, p);
28 }
29 
30 int
31 _sock_srv(char *path, int fd)
32 {
33 	int sfd;
34 	char msg[128];
35 
36 	/* change the path to something in srv */
37 	_sock_srvname(msg, path);
38 
39 	/* remove any previous instance */
40 	unlink(msg);
41 
42 	/* put the fd in /srv and then close it */
43 	sfd = creat(msg, 0666);
44 	if(sfd < 0){
45 		close(fd);
46 		_syserrno();
47 		return -1;
48 	}
49 	sprintf(msg, "%d", fd);
50 	if(write(sfd, msg, strlen(msg)) < 0){
51 		_syserrno();
52 		close(sfd);
53 		close(fd);
54 		return -1;
55 	}
56 	close(sfd);
57 	close(fd);
58 	return 0;
59 }
60