Changeset View
Changeset View
Standalone View
Standalone View
src/ioslaves/file/fdreceiver.cpp
- This file was added.
1 | /*** | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2017 by Chinmoy Ranjan Pradhan <chinmoyrp65@gmail.com> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | ***/ | ||||
20 | | ||||
21 | #include <unistd.h> | ||||
22 | #include <sys/socket.h> | ||||
23 | | ||||
24 | #include <QSocketNotifier> | ||||
25 | | ||||
26 | #include "sharefd_p.h" | ||||
27 | #include "fdreceiver.h" | ||||
28 | | ||||
29 | FdReceiver::FdReceiver(const QString &path, QObject *parent) | ||||
30 | : QObject(parent) | ||||
31 | , m_readNotifier(nullptr) | ||||
32 | , m_socketDes(-1) | ||||
33 | , m_fileDes(-1) | ||||
34 | { | ||||
35 | m_socketDes = ::socket(AF_LOCAL, SOCK_STREAM|SOCK_NONBLOCK, 0); | ||||
36 | if (m_socketDes == -1) { | ||||
37 | std::cerr << "socket error:" << strerror(errno) << std::endl; | ||||
38 | return; | ||||
39 | } | ||||
40 | | ||||
41 | const SocketAddress addr(path.toStdString()); | ||||
42 | if (bind(m_socketDes, addr.address(), addr.length()) != 0 || listen(m_socketDes, 5) != 0) { | ||||
43 | std::cerr << "bind/listen error:" << strerror(errno) << std::endl; | ||||
44 | ::close(m_socketDes); | ||||
45 | m_socketDes = -1; | ||||
46 | return; | ||||
47 | } | ||||
48 | | ||||
49 | m_readNotifier = new QSocketNotifier(m_socketDes, QSocketNotifier::Read, this); | ||||
50 | connect(m_readNotifier, &QSocketNotifier::activated, this, &FdReceiver::receiveFileDescriptor); | ||||
51 | } | ||||
52 | | ||||
53 | FdReceiver::~FdReceiver() | ||||
54 | { | ||||
55 | if (m_socketDes >= 0) { | ||||
56 | ::close(m_socketDes); | ||||
57 | } | ||||
58 | } | ||||
59 | | ||||
60 | bool FdReceiver::isListening() const | ||||
61 | { | ||||
62 | return m_socketDes >= 0 && m_readNotifier; | ||||
63 | } | ||||
64 | | ||||
65 | void FdReceiver::receiveFileDescriptor() | ||||
66 | { | ||||
67 | int client = ::accept(m_socketDes, NULL, NULL); | ||||
68 | if (client > 0) { | ||||
69 | FDMessageHeader msg; | ||||
70 | if (::recvmsg(client, msg.message(), 0) == 2) { | ||||
71 | ::memcpy(&m_fileDes, CMSG_DATA(msg.cmsgHeader()), sizeof m_fileDes); | ||||
72 | } | ||||
73 | ::close(client); | ||||
74 | } | ||||
75 | } | ||||
76 | | ||||
77 | int FdReceiver::fileDescriptor() const | ||||
78 | { | ||||
79 | return m_fileDes; | ||||
80 | } | ||||
81 | |