1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
/* -*- c++ -*- */
/*
* Copyright 2008 Free Software Foundation, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <open_usrp2_socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <stdio.h>
#include <errno.h>
#include <string>
static const char *helper = "usrp2_socket_opener";
static ssize_t
read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
{
struct msghdr msg;
struct iovec iov[1];
ssize_t n;
#ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
union {
struct cmsghdr cm;
char control[CMSG_SPACE(sizeof (int))];
} control_un;
struct cmsghdr *cmptr;
msg.msg_control = control_un.control;
msg.msg_controllen = sizeof(control_un.control);
#else
int newfd;
msg.msg_accrights = (char *) &newfd;
msg.msg_accrightslen = sizeof(int);
#endif
msg.msg_name = NULL;
msg.msg_namelen = 0;
iov[0].iov_base = ptr;
iov[0].iov_len = nbytes;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
if ((n = recvmsg(fd, &msg, 0)) <= 0)
return n;
#ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL
&& cmptr->cmsg_len == CMSG_LEN(sizeof(int))){
if (cmptr->cmsg_level != SOL_SOCKET){
fprintf(stderr, "read_fd: control level != SOL_SOCKET\n");
return -1;
}
if (cmptr->cmsg_type != SCM_RIGHTS){
fprintf(stderr, "read_fd: control type != SCM_RIGHTS\n");
return -1;
}
*recvfd = *((int *) CMSG_DATA(cmptr));
} else
*recvfd = -1; /* descriptor was not passed */
#else
if (msg.msg_accrightslen == sizeof(int))
*recvfd = newfd;
else
*recvfd = -1; /* descriptor was not passed */
#endif
return n;
}
int
usrp2::open_usrp2_socket()
{
int fd = -1, sockfd[2], status;
pid_t childpid;
char c, argsockfd[10];
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd) != 0){
perror("socketpair");
return -1;
}
if ((childpid = fork()) == 0) { /* child process */
close(sockfd[0]);
snprintf(argsockfd, sizeof(argsockfd), "%d", sockfd[1]);
execlp(helper, helper, argsockfd, (char *) NULL);
std::string msg("execlp: couldn't exec " + std::string(helper));
perror(msg.c_str());
close(sockfd[0]);
close(sockfd[1]);
return -1;
}
/* parent process - wait for the child to terminate */
close(sockfd[1]); /* close the end we don't use */
waitpid(childpid, &status, 0);
if (!WIFEXITED(status)){
fprintf(stderr, "child did not terminate\n");
return -1;
}
if ((status = WEXITSTATUS(status)) == 0)
read_fd(sockfd[0], &c, 1, &fd);
else {
errno = status; /* bogus: set errno value from child's status */
fd = -1;
}
close(sockfd[0]);
return (fd);
}
|