]> git.proxmox.com Git - ceph.git/blob - ceph/src/blkin/blkin-lib/tests/test.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / blkin / blkin-lib / tests / test.cc
1 /*
2 * Copyright 2014 Marios Kogias <marioskogias@gmail.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the following
7 * conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30 #include <thread>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 #include <ztracer.hpp>
36 #include <iostream>
37
38 #define SOCK_PATH "socket"
39
40 struct message {
41 int actual_message;
42 struct blkin_trace_info trace_info;
43 message(int s) : actual_message(s) {};
44 message() {}
45 };
46
47 class Parent {
48 private:
49 int s, s2;
50 ZTracer::Endpoint e;
51 public:
52 Parent() : e("0.0.0.0", 1, "parent")
53 {
54 connect();
55 }
56 void operator()()
57 {
58 struct sockaddr_un remote;
59 int t;
60 std::cout << "I am parent process : " << getpid() << std::endl;
61
62 /* Wait for connection */
63 t = sizeof(remote);
64 if ((s2 = accept(s, (struct sockaddr *)&remote, (socklen_t *)&t)) == -1) {
65 std::cerr << "accept" << std::endl;
66 exit(1);
67 }
68
69 std::cerr << "Connected" << std::endl;
70
71 for (int i=0;i<10;i++) {
72 /*Init trace*/
73 ZTracer::Trace tr("parent process", &e);
74
75 process(tr);
76
77 wait_response();
78
79 /*Log received*/
80 tr.event("parent end");
81 }
82 }
83
84 void process(ZTracer::Trace &tr)
85 {
86 struct message msg(rand());
87 /*Annotate*/
88 tr.event("parent start");
89 /*Set trace info to the message*/
90 msg.trace_info = *tr.get_info();
91
92 /*send*/
93 send(s2, &msg, sizeof(struct message), 0);
94 }
95
96 void wait_response()
97 {
98 char ack;
99 recv(s2, &ack, 1, 0);
100 }
101
102 void connect()
103 {
104 /*create and bind socket*/
105 int len;
106 struct sockaddr_un local;
107
108 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
109 std::cerr << "socket" << std::endl;
110 exit(1);
111 }
112
113 local.sun_family = AF_UNIX;
114 strcpy(local.sun_path, SOCK_PATH);
115 unlink(local.sun_path);
116 len = strlen(local.sun_path) + sizeof(local.sun_family);
117 if (bind(s, (struct sockaddr *)&local, len) == -1) {
118 std::cerr << "bind" << std::endl;
119 exit(1);
120 }
121
122 if (listen(s, 5) == -1) {
123 std::cerr << "listen" << std::endl;
124 exit(1);
125 }
126
127 std::cout << "Waiting for a connection..." << std::endl;
128 }
129
130 };
131
132 class Child {
133 private:
134 int s;
135 ZTracer::Endpoint e;
136 public:
137 Child() : e("0.0.0.1", 2, "child")
138 {
139 }
140 void operator()()
141 {
142 /*Connect to the socket*/
143 soc_connect();
144
145 for (int i=0;i<10;i++)
146 process();
147 }
148
149 void process()
150 {
151 struct message msg;
152 recv(s, &msg, sizeof(struct message), 0);
153
154 ZTracer::Trace tr("Child process", &e, &msg.trace_info, true);
155 tr.event("child start");
156
157 usleep(10);
158 std::cout << "Message received : " << msg.actual_message << ::std::endl;
159 tr.event("child end");
160
161 send(s, "*", 1, 0);
162 }
163
164
165 void soc_connect()
166 {
167 int len;
168 struct sockaddr_un remote;
169
170 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
171 std::cerr << "socket" << std::endl;
172 exit(1);
173 }
174
175 std::cout << "Trying to connect...\n" << std::endl;
176
177 remote.sun_family = AF_UNIX;
178 strcpy(remote.sun_path, SOCK_PATH);
179 len = strlen(remote.sun_path) + sizeof(remote.sun_family);
180 if (connect(s, (struct sockaddr *)&remote, len) == -1) {
181 std::cerr << "connect" << std::endl;;
182 exit(1);
183 }
184
185 std::cout << "Connected" << std::endl;
186 }
187
188 };
189 int main(int argc, const char *argv[])
190 {
191 int r = ZTracer::ztrace_init();
192 if (r < 0) {
193 std::cout << "Error initializing blkin" << std::endl;
194 return -1;
195 }
196 Parent p;
197 Child c;
198 std::thread workerThread1(p);
199 std::thread workerThread2(c);
200 workerThread1.join();
201 workerThread2.join();
202
203 return 0;
204 }