xref: /plan9/sys/src/ape/lib/bsd/_sock_srv.c (revision 7e125112574862ca44c733e2ecf7b75e72c6ed0e)
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 /* we can't avoid overrunning npath because we don't know how big it is. */
17 void
_sock_srvname(char * npath,char * path)18 _sock_srvname(char *npath, char *path)
19 {
20 	char *p;
21 
22 	strcpy(npath, "/srv/UD.");
23 	p = strrchr(path, '/');
24 	if(p == 0)
25 		p = path;
26 	else
27 		p++;
28 	strcat(npath, p);
29 }
30 
31 int
_sock_srv(char * path,int fd)32 _sock_srv(char *path, int fd)
33 {
34 	int sfd;
35 	char msg[8+256+1];
36 
37 	/* change the path to something in srv */
38 	_sock_srvname(msg, path);
39 
40 	/* remove any previous instance */
41 	unlink(msg);
42 
43 	/* put the fd in /srv and then close it */
44 	sfd = creat(msg, 0666);
45 	if(sfd < 0){
46 		close(fd);
47 		_syserrno();
48 		return -1;
49 	}
50 	snprintf(msg, sizeof msg, "%d", fd);
51 	if(write(sfd, msg, strlen(msg)) < 0){
52 		_syserrno();
53 		close(sfd);
54 		close(fd);
55 		return -1;
56 	}
57 	close(sfd);
58 	close(fd);
59 	return 0;
60 }
61