smbd: rename sconn->raw_thread_pool to sconn->pool
[samba.git] / source3 / torture / msg_source.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Send messages once a second
4  *  Copyright (C) Volker Lendecke 2014
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "replace.h"
21 #include "includes.h"
22 #include "lib/util/server_id.h"
23 #include "messages.h"
24 #include "lib/util/tevent_unix.h"
25 #include <stdio.h>
26
27 struct source_state {
28         struct tevent_context *ev;
29         struct messaging_context *msg_ctx;
30         int msg_type;
31         struct timeval interval;
32         struct server_id dst;
33 };
34
35 static void source_waited(struct tevent_req *subreq);
36
37 static struct tevent_req *source_send(TALLOC_CTX *mem_ctx,
38                                       struct tevent_context *ev,
39                                       struct messaging_context *msg_ctx,
40                                       int msg_type,
41                                       struct timeval interval,
42                                       struct server_id dst)
43 {
44         struct tevent_req *req, *subreq;
45         struct source_state *state;
46
47         req = tevent_req_create(mem_ctx, &state, struct source_state);
48         if (req == NULL) {
49                 return NULL;
50         }
51         state->ev = ev;
52         state->msg_ctx = msg_ctx;
53         state->msg_type = msg_type;
54         state->interval = interval;
55         state->dst = dst;
56
57         subreq = tevent_wakeup_send(
58                 state, state->ev,
59                 timeval_current_ofs(state->interval.tv_sec,
60                                     state->interval.tv_usec));
61         if (tevent_req_nomem(subreq, req)) {
62                 return tevent_req_post(req, ev);
63         }
64         tevent_req_set_callback(subreq, source_waited, req);
65         return req;
66 }
67
68 static void source_waited(struct tevent_req *subreq)
69 {
70         struct tevent_req *req = tevent_req_callback_data(
71                 subreq, struct tevent_req);
72         struct source_state *state = tevent_req_data(
73                 req, struct source_state);
74         bool ok;
75         uint8_t buf[200] = { };
76
77         ok = tevent_wakeup_recv(subreq);
78         TALLOC_FREE(subreq);
79         if (!ok) {
80                 tevent_req_error(req, ENOMEM);
81                 return;
82         }
83
84         messaging_send_buf(state->msg_ctx, state->dst, state->msg_type,
85                            buf, sizeof(buf));
86
87         subreq = tevent_wakeup_send(
88                 state, state->ev,
89                 timeval_current_ofs(state->interval.tv_sec,
90                                     state->interval.tv_usec));
91         if (tevent_req_nomem(subreq, req)) {
92                 return;
93         }
94         tevent_req_set_callback(subreq, source_waited, req);
95 }
96
97 static int source_recv(struct tevent_req *req)
98 {
99         int err;
100
101         if (tevent_req_is_unix_error(req, &err)) {
102                 return err;
103         }
104         return 0;
105 }
106
107 int main(int argc, const char *argv[])
108 {
109         TALLOC_CTX *frame = talloc_stackframe();
110         struct tevent_context *ev;
111         struct messaging_context *msg_ctx;
112         struct tevent_req *req;
113         int ret;
114         struct server_id my_id, id;
115
116         if (argc != 2) {
117                 fprintf(stderr, "Usage: %s <dst>\n", argv[0]);
118                 return -1;
119         }
120
121         lp_load_global(get_dyn_CONFIGFILE());
122
123         ev = tevent_context_init(frame);
124         if (ev == NULL) {
125                 perror("tevent_context_init failed");
126                 return -1;
127         }
128
129         msg_ctx = messaging_init(ev, ev);
130         if (msg_ctx == NULL) {
131                 perror("messaging_init failed");
132                 return -1;
133         }
134         my_id = messaging_server_id(msg_ctx);
135
136         id = server_id_from_string(my_id.vnn, argv[1]);
137         if (!procid_valid(&id)) {
138                 fprintf(stderr, "pid %s invalid\n", argv[1]);
139                 return -1;
140         }
141
142         req = source_send(ev, ev, msg_ctx, MSG_SMB_NOTIFY,
143                           timeval_set(0, 10000), id);
144         if (req == NULL) {
145                 perror("source_send failed");
146                 return -1;
147         }
148
149         if (!tevent_req_poll(req, ev)) {
150                 perror("tevent_req_poll failed");
151                 return -1;
152         }
153
154         ret = source_recv(req);
155
156         printf("source_recv returned %d\n", ret);
157
158         return 0;
159 }