[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Commit-gnuradio] r9502 - usrp2/trunk/host/lib
From: |
eb |
Subject: |
[Commit-gnuradio] r9502 - usrp2/trunk/host/lib |
Date: |
Fri, 5 Sep 2008 03:10:48 -0600 (MDT) |
Author: eb
Date: 2008-09-05 03:10:47 -0600 (Fri, 05 Sep 2008)
New Revision: 9502
Added:
usrp2/trunk/host/lib/open_usrp2_socket.cc
usrp2/trunk/host/lib/open_usrp2_socket.h
Modified:
usrp2/trunk/host/lib/Makefile.am
usrp2/trunk/host/lib/ethernet.cc
usrp2/trunk/host/lib/usrp2_socket_opener.cc
Log:
usrp2 can now be opened without running as root
Modified: usrp2/trunk/host/lib/Makefile.am
===================================================================
--- usrp2/trunk/host/lib/Makefile.am 2008-09-05 08:38:37 UTC (rev 9501)
+++ usrp2/trunk/host/lib/Makefile.am 2008-09-05 09:10:47 UTC (rev 9502)
@@ -34,6 +34,7 @@
eth_buffer.cc \
ethernet.cc \
find.cc \
+ open_usrp2_socket.cc \
pktfilter.cc \
ring.cc \
rx_nop_handler.cc \
Modified: usrp2/trunk/host/lib/ethernet.cc
===================================================================
--- usrp2/trunk/host/lib/ethernet.cc 2008-09-05 08:38:37 UTC (rev 9501)
+++ usrp2/trunk/host/lib/ethernet.cc 2008-09-05 09:10:47 UTC (rev 9502)
@@ -22,6 +22,7 @@
#include "ethernet.h"
#include "pktfilter.h"
+#include <open_usrp2_socket.h>
#include <iostream>
#include <unistd.h>
@@ -44,10 +45,15 @@
static int
open_packet_socket (std::string ifname, int protocol)
{
+#if 0
if (protocol == 0)
protocol = htons(ETH_P_ALL);
int fd = socket (PF_PACKET, SOCK_RAW, protocol);
+#else
+ int fd = usrp2::open_usrp2_socket();
+#endif
+
if (fd == -1){
fprintf (stderr, "%s: socket: %s\n", ifname.c_str(), strerror (errno));
return -1;
Added: usrp2/trunk/host/lib/open_usrp2_socket.cc
===================================================================
--- usrp2/trunk/host/lib/open_usrp2_socket.cc (rev 0)
+++ usrp2/trunk/host/lib/open_usrp2_socket.cc 2008-09-05 09:10:47 UTC (rev
9502)
@@ -0,0 +1,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);
+}
Property changes on: usrp2/trunk/host/lib/open_usrp2_socket.cc
___________________________________________________________________
Name: svn:eol-style
+ native
Added: usrp2/trunk/host/lib/open_usrp2_socket.h
===================================================================
--- usrp2/trunk/host/lib/open_usrp2_socket.h (rev 0)
+++ usrp2/trunk/host/lib/open_usrp2_socket.h 2008-09-05 09:10:47 UTC (rev
9502)
@@ -0,0 +1,34 @@
+/* -*- 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/>.
+ */
+#ifndef INCLUDED_OPEN_USRP2_SOCKET_H
+#define INCLUDED_OPEN_USRP2_SOCKET_H
+
+namespace usrp2 {
+
+ /*!
+ * Return the result of executing:
+ *
+ * int fd = socket(PF_PACKET, SOCK_RAW, htons(0xBEEF));
+ *
+ * Doing it in a way that we don't need to be running as root.
+ */
+ int open_usrp2_socket();
+};
+
+
+#endif /* INCLUDED_OPEN_USRP2_SOCKET_H */
Property changes on: usrp2/trunk/host/lib/open_usrp2_socket.h
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: usrp2/trunk/host/lib/usrp2_socket_opener.cc
===================================================================
--- usrp2/trunk/host/lib/usrp2_socket_opener.cc 2008-09-05 08:38:37 UTC (rev
9501)
+++ usrp2/trunk/host/lib/usrp2_socket_opener.cc 2008-09-05 09:10:47 UTC (rev
9502)
@@ -78,15 +78,15 @@
}
bool
-reset_eids ()
+reset_eids()
{
- if (setgid (getgid ()) < 0){
- perror ("setguid");
+ if (setgid(getgid()) < 0){
+ perror("setguid");
return false;
}
- if (setuid (getuid ()) < 0){
- perror ("setuid");
+ if (setuid(getuid()) < 0){
+ perror("setuid");
return false;
}
@@ -115,15 +115,15 @@
// FIXME get client credentials from unix_domain_fd using SCM_CREDENTIALS
// open the raw socket
- int socket_fd = socket (PF_PACKET, SOCK_RAW, htons(0xBEEF));
+ int socket_fd = socket(PF_PACKET, SOCK_RAW, htons(0xBEEF));
if (socket_fd == -1){
perror("socket(PF_PACKET, SOCK_RAW, htons(0xBEEF))");
// printf("errno = %d\n", errno);
if (errno == EACCES || errno == ESPIPE){
- fprintf (stderr, "usrp2_socket_opener must be setuid root to open the
socket using SOCK_RAW.\n");
- fprintf (stderr, "Running as root, please execute: \n");
- fprintf (stderr, " # chown root usrp2_socket_opener\n");
- fprintf (stderr, " # chmod u+s usrp2_socket_opener\n");
+ fprintf(stderr, "usrp2_socket_opener must be setuid root to open the
socket using SOCK_RAW.\n");
+ fprintf(stderr, "Running as root, please execute: \n");
+ fprintf(stderr, " # chown root usrp2_socket_opener\n");
+ fprintf(stderr, " # chmod u+s usrp2_socket_opener\n");
}
exit(2);
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Commit-gnuradio] r9502 - usrp2/trunk/host/lib,
eb <=