Changeset View
Changeset View
Standalone View
Standalone View
src/ioslaves/file/sharefd.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 <fcntl.h> | ||||
22 | #include <unistd.h> | ||||
23 | #include <sys/socket.h> | ||||
24 | #include <sys/un.h> | ||||
25 | #include "sharefd.h" | ||||
26 | | ||||
27 | //borrowed from klocalsocket.cpp | ||||
28 | class KSockaddrUn | ||||
29 | { | ||||
30 | sockaddr_un addr; | ||||
31 | int addrlen; | ||||
32 | | ||||
33 | public: | ||||
34 | KSockaddrUn(const std::string &path) | ||||
35 | { | ||||
36 | addrlen = sizeof(sockaddr_un); | ||||
37 | memset(&addr, 0, addrlen); | ||||
38 | addr.sun_family = AF_UNIX; | ||||
39 | std::string finalPath = "/tmp/" + path; | ||||
40 | #ifdef __linux__ | ||||
41 | strcpy(&addr.sun_path[1], finalPath.c_str()); | ||||
42 | #else | ||||
43 | strcpy(&addr.sun_path, finalPath.c_str()); | ||||
44 | unlink(finalPath.c_str()); | ||||
45 | #endif | ||||
46 | } | ||||
47 | | ||||
48 | int length() const | ||||
49 | { | ||||
50 | return addrlen; | ||||
51 | } | ||||
52 | const sockaddr *address() | ||||
53 | { | ||||
54 | return reinterpret_cast<sockaddr*>(&addr); | ||||
55 | } | ||||
56 | }; | ||||
57 | | ||||
58 | class KMsgHdr | ||||
59 | { | ||||
60 | char io_buf[2]; | ||||
61 | char cmsg_buf[CMSG_SPACE(sizeof(int))]; | ||||
62 | msghdr msg; | ||||
63 | iovec io; | ||||
64 | | ||||
65 | public: | ||||
66 | KMsgHdr() | ||||
67 | { | ||||
68 | io.iov_base = io_buf; | ||||
69 | io.iov_len = 2; | ||||
70 | msg.msg_name = NULL; | ||||
71 | msg.msg_namelen = 0; | ||||
72 | msg.msg_iov = &io; | ||||
73 | msg.msg_iovlen = 1; | ||||
74 | msg.msg_control = cmsg_buf; | ||||
75 | msg.msg_controllen = sizeof(cmsg_buf); | ||||
76 | } | ||||
77 | | ||||
78 | msghdr *message() | ||||
79 | { | ||||
80 | return &msg; | ||||
81 | } | ||||
82 | | ||||
83 | cmsghdr *cmsgHeader() const | ||||
84 | { | ||||
85 | return CMSG_FIRSTHDR(&msg); | ||||
86 | } | ||||
87 | }; | ||||
88 | | ||||
89 | | ||||
90 | // File descriptor reciever | ||||
91 | FdReceiver::FdReceiver(QObject *parent) | ||||
92 | : QObject(parent) | ||||
93 | , m_socketDes(-1) | ||||
94 | , m_fileDes(-1) | ||||
95 | { | ||||
96 | } | ||||
97 | | ||||
98 | bool FdReceiver::startListening(const QString &path) | ||||
99 | { | ||||
100 | if (path.isEmpty()) | ||||
101 | return false; | ||||
102 | | ||||
103 | m_socketDes = ::socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK, 0); | ||||
104 | if (m_socketDes == -1) | ||||
105 | return false; | ||||
106 | | ||||
107 | KSockaddrUn addr(path.toStdString()); | ||||
108 | bool bound = bind(m_socketDes, addr.address(), addr.length()) != -1; | ||||
109 | bool listening = listen(m_socketDes, 5) != -1; | ||||
110 | | ||||
111 | if (!bound || !listening) { | ||||
112 | ::close(m_socketDes); | ||||
113 | return false; | ||||
114 | } | ||||
115 | | ||||
116 | m_readNotifier = new QSocketNotifier(m_socketDes, QSocketNotifier::Read, this); | ||||
117 | connect(m_readNotifier, &QSocketNotifier::activated, this, &FdReceiver::receiveFileDescriptor); | ||||
118 | return true; | ||||
119 | } | ||||
120 | | ||||
121 | void FdReceiver::receiveFileDescriptor() | ||||
122 | { | ||||
123 | int client = ::accept(m_socketDes, NULL, NULL); | ||||
124 | if (client > 0) { | ||||
125 | KMsgHdr msg; | ||||
126 | if (recvmsg(client, msg.message(), 0) == 2) { | ||||
127 | cmsghdr *cmsg = msg.cmsgHeader(); | ||||
128 | memcpy(&m_fileDes, (int*)CMSG_DATA(cmsg), sizeof(int)); | ||||
129 | m_readNotifier->setEnabled(false); | ||||
130 | } | ||||
131 | ::close(client); | ||||
132 | } | ||||
133 | ::close(m_socketDes); | ||||
134 | } | ||||
135 | | ||||
136 | int FdReceiver::fileDescriptor() const | ||||
137 | { | ||||
138 | return m_fileDes; | ||||
139 | } | ||||
140 | | ||||
141 | | ||||
142 | // File descriptor sender | ||||
143 | FdSender::FdSender() | ||||
144 | : m_socketDes(-1) | ||||
145 | , m_connected(false) | ||||
146 | { | ||||
147 | } | ||||
148 | | ||||
149 | void FdSender::connectToPath(const std::string &path) | ||||
150 | { | ||||
151 | if (path.empty()) | ||||
152 | return; | ||||
153 | | ||||
154 | m_socketDes = ::socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); | ||||
155 | if (m_socketDes == -1) | ||||
156 | return; | ||||
157 | | ||||
158 | KSockaddrUn addr(path); | ||||
159 | bool connected = ::connect(m_socketDes, addr.address(), addr.length()) == 0; | ||||
160 | | ||||
161 | if (!connected) | ||||
162 | close(m_socketDes); | ||||
163 | | ||||
164 | m_connected = true; | ||||
165 | } | ||||
166 | | ||||
167 | bool FdSender::sendFileDescriptor(int fd) | ||||
168 | { | ||||
169 | KMsgHdr msg; | ||||
170 | cmsghdr *cmsg = msg.cmsgHeader(); | ||||
171 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); | ||||
172 | cmsg->cmsg_type = SCM_RIGHTS; | ||||
173 | cmsg->cmsg_level = SOL_SOCKET; | ||||
174 | memcpy((int*)CMSG_DATA(cmsg), &fd, sizeof(int)); | ||||
175 | bool success = sendmsg(m_socketDes, msg.message(), 0) == 2; | ||||
176 | close(m_socketDes); | ||||
177 | return success; | ||||
178 | } | ||||
179 | | ||||
180 | bool FdSender::isConnected() const | ||||
181 | { | ||||
182 | return m_connected; | ||||
183 | } |