2 * Copyright 2014 Marios Kogias <marioskogias@gmail.com>
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the following
9 * 1. Redistributions of source code must retain the above
10 * copyright notice, this list of conditions and the following
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.
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.
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <ztracer.hpp>
38 #define SOCK_PATH "socket"
42 struct blkin_trace_info trace_info
;
43 message(int s
) : actual_message(s
) {};
52 Parent() : e("0.0.0.0", 1, "parent")
58 struct sockaddr_un remote
;
60 std::cout
<< "I am parent process : " << getpid() << std::endl
;
62 /* Wait for connection */
64 if ((s2
= accept(s
, (struct sockaddr
*)&remote
, (socklen_t
*)&t
)) == -1) {
65 std::cerr
<< "accept" << std::endl
;
69 std::cerr
<< "Connected" << std::endl
;
71 for (int i
=0;i
<10;i
++) {
73 ZTracer::Trace
tr("parent process", &e
);
80 tr
.event("parent end");
84 void process(ZTracer::Trace
&tr
)
86 struct message
msg(rand());
88 tr
.event("parent start");
89 /*Set trace info to the message*/
90 msg
.trace_info
= *tr
.get_info();
93 send(s2
, &msg
, sizeof(struct message
), 0);
104 /*create and bind socket*/
106 struct sockaddr_un local
;
108 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
109 std::cerr
<< "socket" << std::endl
;
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
;
122 if (listen(s
, 5) == -1) {
123 std::cerr
<< "listen" << std::endl
;
127 std::cout
<< "Waiting for a connection..." << std::endl
;
137 Child() : e("0.0.0.1", 2, "child")
142 /*Connect to the socket*/
145 for (int i
=0;i
<10;i
++)
152 recv(s
, &msg
, sizeof(struct message
), 0);
154 ZTracer::Trace
tr("Child process", &e
, &msg
.trace_info
, true);
155 tr
.event("child start");
158 std::cout
<< "Message received : " << msg
.actual_message
<< ::std::endl
;
159 tr
.event("child end");
168 struct sockaddr_un remote
;
170 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
171 std::cerr
<< "socket" << std::endl
;
175 std::cout
<< "Trying to connect...\n" << std::endl
;
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
;;
185 std::cout
<< "Connected" << std::endl
;
189 int main(int argc
, const char *argv
[])
191 int r
= ZTracer::ztrace_init();
193 std::cout
<< "Error initializing blkin" << std::endl
;
198 std::thread
workerThread1(p
);
199 std::thread
workerThread2(c
);
200 workerThread1
.join();
201 workerThread2
.join();