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
|
#include "daemon.h"
#include <library/cpp/testing/unittest/registar.h>
#include <util/network/pair.h>
#include <util/network/socket.h>
#include <util/system/pipe.h>
Y_UNIT_TEST_SUITE(TDaemonTest) {
#ifdef _unix_
template <typename Func>
static bool ProcessBuffer(Func&& func, void* bufin, size_t size) {
char* buf = (char*)bufin;
do {
const ssize_t bytesDone = func(buf, size);
if (bytesDone == 0) {
return false;
}
if (bytesDone < 0) {
if (errno == EAGAIN || errno == EINTR) {
continue;
} else {
return false;
}
}
buf += bytesDone;
size -= bytesDone;
} while (size != 0);
return true;
}
const int size = 1024 * 4;
const int pagesSize = sizeof(int) * size;
Y_UNIT_TEST(WaitForMessageSocket) {
using namespace NDaemonMaker;
SOCKET sockets[2];
SocketPair(sockets, false, true);
TSocket sender(sockets[0]);
TSocket receiver(sockets[1]);
int status = -1;
int* pages = new int[size];
memset(pages, 0, pagesSize);
if (MakeMeDaemon(closeStdIoOnly, openDevNull, chdirNone, returnFromParent)) {
sender.Close();
UNIT_ASSERT(ProcessBuffer([&receiver](char* ptr, size_t sz) -> size_t { return receiver.Recv(ptr, sz); }, &status, sizeof(status)));
UNIT_ASSERT(ProcessBuffer([&receiver](char* ptr, size_t sz) -> size_t { return receiver.Recv(ptr, sz); }, pages, pagesSize));
UNIT_ASSERT(memchr(pages, 0, pagesSize) == nullptr);
} else {
receiver.Close();
status = 0;
UNIT_ASSERT(ProcessBuffer([&sender](char* ptr, size_t sz) -> size_t { return sender.Send(ptr, sz); }, &status, sizeof(status)));
memset(pages, 1, pagesSize);
UNIT_ASSERT(ProcessBuffer([&sender](char* ptr, size_t sz) -> size_t { return sender.Send(ptr, sz); }, pages, pagesSize));
exit(0);
}
UNIT_ASSERT(status == 0);
delete[] pages;
}
Y_UNIT_TEST(WaitForMessagePipe) {
using namespace NDaemonMaker;
TPipeHandle sender;
TPipeHandle receiver;
TPipeHandle::Pipe(receiver, sender);
int status = -1;
int* pages = new int[size];
memset(pages, 0, pagesSize);
if (MakeMeDaemon(closeStdIoOnly, openDevNull, chdirNone, returnFromParent)) {
sender.Close();
UNIT_ASSERT(ProcessBuffer([&receiver](char* ptr, size_t sz) -> size_t { return receiver.Read(ptr, sz); }, &status, sizeof(status)));
UNIT_ASSERT(ProcessBuffer([&receiver](char* ptr, size_t sz) -> size_t { return receiver.Read(ptr, sz); }, pages, pagesSize));
UNIT_ASSERT(memchr(pages, 0, pagesSize) == nullptr);
} else {
receiver.Close();
status = 0;
UNIT_ASSERT(ProcessBuffer([&sender](char* ptr, size_t sz) -> size_t { return sender.Write(ptr, sz); }, &status, sizeof(status)));
memset(pages, 1, pagesSize);
UNIT_ASSERT(ProcessBuffer([&sender](char* ptr, size_t sz) -> size_t { return sender.Write(ptr, sz); }, pages, pagesSize));
exit(0);
}
UNIT_ASSERT(status == 0);
delete[] pages;
}
#endif
}
|