From e38ba51a18f40c28346ece6773cbdbe5c2e05eb2 Mon Sep 17 00:00:00 2001 From: "[CST] Focus" Date: Wed, 23 Apr 2014 17:26:41 +0400 Subject: [PATCH] mdev_helper.cpp: add device hotplug neutrino helper --- .gitignore | 1 + src/Makefile.am | 2 + src/mdev_helper.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 src/mdev_helper.cpp diff --git a/.gitignore b/.gitignore index 8123ac458..6b3da8cc7 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,4 @@ src/rcsim.h src/rcsim src/gui/version.h src/nhttpd/nhttpd.conf +src/mdev_helper diff --git a/src/Makefile.am b/src/Makefile.am index ca20b69f1..2ccc12077 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -153,6 +153,8 @@ bin_PROGRAMS += dt dt_SOURCES = dt.c bin_PROGRAMS += rcsim rcsim_SOURCES = rcsim.c rcsim.h +bin_PROGRAMS += mdev_helper +mdev_helper_SOURCES = mdev_helper.cpp endif if BOXTYPE_TRIPLE diff --git a/src/mdev_helper.cpp b/src/mdev_helper.cpp new file mode 100644 index 000000000..26d35d781 --- /dev/null +++ b/src/mdev_helper.cpp @@ -0,0 +1,98 @@ +/* + Neutrino-GUI - DBoxII-Project + + Copyright (C) 2014 CoolStream International Ltd + + License: GPLv2 + + 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; version 2 of the License. + + 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, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include +#include +#include +#include +#include + +#include "config.h" +#include +#include + +#include + +const char *mdev_env[] = +{ + "ACTION" , + "MDEV" , + "DEVPATH" , + "INTERFACE", +}; + +#define ENV_SIZE (sizeof(mdev_env)/sizeof(char *)) + +int main (int /*argc*/, char **argv) +{ + struct sockaddr_un servaddr; + int clilen, sock_fd; + std::string data; + + memset(&servaddr, 0, sizeof(struct sockaddr_un)); + servaddr.sun_family = AF_UNIX; + strncpy(servaddr.sun_path, NEUTRINO_UDS_NAME, sizeof(servaddr.sun_path) - 1); + clilen = sizeof(servaddr.sun_family) + strlen(servaddr.sun_path); + + if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) + { + perror("error open socket " NEUTRINO_UDS_NAME); + exit(1); + } + + if (connect(sock_fd, (struct sockaddr*) &servaddr, clilen) < 0) + { + perror("error connect socket " NEUTRINO_UDS_NAME); + goto _error; + } + + for (unsigned i = 0; i < ENV_SIZE; i++) { + char * s = getenv(mdev_env[i]); + if (s) + data += std::string(mdev_env[i]) + "=" + s + " "; + } + + if (data.empty()) { + printf("%s: env data empty\n", argv[0]); + goto _error; + } + + CEventServer::eventHead head; + + head.eventID = NeutrinoMessages::EVT_HOTPLUG; + head.initiatorID = CEventServer::INITID_NEUTRINO; + head.dataSize = data.size() + 1; + + if (write(sock_fd, &head, sizeof(head)) != sizeof(head)) { + perror("write event"); + goto _error; + } + + if (write(sock_fd, data.c_str(), head.dataSize) != (ssize_t) head.dataSize) { + perror("write data"); + goto _error; + } + close(sock_fd); + exit(0); +_error: + close(sock_fd); + exit(1); +}