File: unix.cpp

package info (click to toggle)
libfilezilla 0.52.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,492 kB
  • sloc: cpp: 30,965; sh: 4,241; makefile: 375; xml: 37
file content (230 lines) | stat: -rw-r--r-- 3,780 bytes parent folder | download | duplicates (3)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "../libfilezilla/buffer.hpp"
#include "../libfilezilla/glue/unix.hpp"
#include "../libfilezilla/process.hpp"

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>

namespace fz {

bool set_cloexec(int fd)
{
#ifdef FD_CLOEXEC
	if (fd != -1) {
		int flags = fcntl(fd, F_GETFD);
		if (flags >= 0) {
			return fcntl(fd, F_SETFD, flags | FD_CLOEXEC) >= 0;
		}
	}
#else
	errno = ENOSYS;
#endif
	return false;
}

bool create_pipe(int fds[2])
{
	disable_sigpipe();

	fds[0] = -1;
	fds[1] = -1;

#if HAVE_PIPE2
	int res = pipe2(fds, O_CLOEXEC);
	if (!res) {
		return true;
	}
	else if (errno != ENOSYS) {
		return false;
	}
	else
#endif
	{
		forkblock b;
		if (pipe(fds) != 0) {
			return false;
		}

		set_cloexec(fds[0]);
		set_cloexec(fds[1]);
	}

	return true;
}

void disable_sigpipe()
{
	[[maybe_unused]] static bool const once = []() { signal(SIGPIPE, SIG_IGN); return true; }();
}

bool create_socketpair(int fds[2])
{
	disable_sigpipe();

	int flags = SOCK_STREAM;
#ifdef SOCK_CLOEXEC
	flags |= SOCK_CLOEXEC;
#else
	forkblock b;
#endif
	bool ret = socketpair(AF_UNIX, flags, 0, fds) == 0;
	if (!ret) {
		fds[0] = -1;
		fds[1] = -1;
	}
#ifndef SOCK_CLOEXEC
	if (ret) {
	    set_cloexec(fds[0]);
	    set_cloexec(fds[1]);
	}
#endif

	return ret;
}

int send_fd(int socket, fz::buffer & buf, int fd, int & error)
{
	if (buf.empty()) {
		error = EINVAL;
		return -1;
	}
	if (socket < 0) {
		error = EBADF;
		return -1;
	}

#ifdef MSG_NOSIGNAL
	const int flags = MSG_NOSIGNAL;
#else
	const int flags = 0;
#endif

	struct msghdr msg{};

	struct iovec iov{};
	iov.iov_base = buf.get();
	iov.iov_len = buf.size();
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;

	union {
		char buf[CMSG_SPACE(sizeof(int))];
		struct cmsghdr header;
	} cmsg_u; // For alignment reasons

	if (fd != -1) {
		msg.msg_control = cmsg_u.buf;
		msg.msg_controllen = sizeof(cmsg_u.buf);
		memset(msg.msg_control, 0, msg.msg_controllen);

		struct cmsghdr * cmsg = CMSG_FIRSTHDR(&msg);
		cmsg->cmsg_len = CMSG_LEN(sizeof(int));
		cmsg->cmsg_level = SOL_SOCKET;
		cmsg->cmsg_type = SCM_RIGHTS;
		memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
	}


	int res{};
	do {
		res = sendmsg(socket, &msg, flags);
	}
	while (res == -1 && errno == EINTR);

	if (res > 0) {
		buf.consume(res);
		error = 0;
	}
	else {
		error = errno;
	}

	return res;
}

int read_fd(int socket, fz::buffer & buf, int & fd, int & error)
{
	fd = -1;
	if (socket < 0) {
		error = EBADF;
		return -1;
	}

	int flags{};
#ifdef MSG_NOSIGNAL
	flags |= MSG_NOSIGNAL;
#endif
#ifdef MSG_CMSG_CLOEXEC
	flags |= MSG_CMSG_CLOEXEC;
#else
	forkblock b;
#endif

	struct msghdr msg{};

	struct iovec iov{};
	iov.iov_base = buf.get(16*1024);
	iov.iov_len = 16*1024;
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;

	union {
		char buf[CMSG_SPACE(sizeof(int))];
		struct cmsghdr header;
	} cmsg_u; // For alignment reasons

	msg.msg_control = cmsg_u.buf;
	msg.msg_controllen = sizeof(cmsg_u.buf);

	int res{};
	do {
		res = recvmsg(socket, &msg, flags);
	}
	while (res == -1 && errno == EINTR);
	error = errno;

	if (res >= 0) {
		buf.add(res);
		error = 0;

		struct cmsghdr * cmsg = CMSG_FIRSTHDR(&msg);
		if (cmsg && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
			memcpy(&fd, CMSG_DATA(cmsg), sizeof(int));
#ifndef MSG_CMSG_CLOEXEC
			set_cloexec(fd);
#endif
		};
	}
	else {
		error = errno;
	}

	return res;
}

int set_nonblocking(int fd, bool non_blocking)
{
	int flags = fcntl(fd, F_GETFL);
	if (flags == -1) {
		return errno;
	}
	if (non_blocking) {
		flags |= O_NONBLOCK;
	}
	else {
		flags &= ~O_NONBLOCK;
	}

	int res = fcntl(fd, F_SETFL, flags);
	if (res == -1) {
		return errno;
	}
	return 0;
}

}