2 * Samba Unix/Linux notifyd client code
3 * Copyright (C) 2015 Volker Lendecke <vl@samba.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "utils/net.h"
21 #include "lib/util/server_id.h"
22 #include "lib/util/tevent_unix.h"
23 #include "lib/util/server_id_db.h"
25 #include "source3/smbd/notifyd/notifyd.h"
27 static void net_notify_got_event(struct messaging_context
*msg
,
30 struct server_id server_id
,
33 struct notify_event_msg
*event_msg
;
35 if (data
->length
< offsetof(struct notify_event_msg
, path
) + 1) {
36 d_fprintf(stderr
, "message too short\n");
39 if (data
->data
[data
->length
-1] != 0) {
40 d_fprintf(stderr
, "path not 0-terminated\n");
44 event_msg
= (struct notify_event_msg
*)data
->data
;
46 d_printf("%u %s\n", (unsigned)event_msg
->action
,
50 static int net_notify_listen(struct net_context
*c
, int argc
,
53 struct messaging_context
*msg_ctx
= c
->msg_ctx
;
54 struct tevent_context
*ev
= messaging_tevent_context(msg_ctx
);
55 struct server_id_db
*names_db
= messaging_names_db(msg_ctx
);
56 struct server_id notifyd
;
57 struct server_id_buf idbuf
;
58 struct notify_rec_change_msg msg
;
64 d_printf("Usage: net notify listen <path> <filter> "
69 ok
= server_id_db_lookup_one(names_db
, "notify-daemon", ¬ifyd
);
71 fprintf(stderr
, "no notify daemon found\n");
75 printf("notify daemon: %s\n", server_id_str_buf(notifyd
, &idbuf
));
77 msg
= (struct notify_rec_change_msg
) {
78 .instance
.filter
= atoi(argv
[1]),
79 .instance
.subdir_filter
= atoi(argv
[2])
81 iov
[0] = (struct iovec
) {
83 .iov_len
= offsetof(struct notify_rec_change_msg
, path
)
85 iov
[1] = (struct iovec
) {
86 .iov_base
= discard_const_p(char, argv
[0]),
87 .iov_len
= strlen(argv
[0])+1
90 status
= messaging_register(c
->msg_ctx
, NULL
, MSG_PVFS_NOTIFY
,
91 net_notify_got_event
);
92 if (!NT_STATUS_IS_OK(status
)) {
93 d_fprintf(stderr
, "messaging_register failed: %s\n",
98 status
= messaging_send_iov(
99 c
->msg_ctx
, notifyd
, MSG_SMB_NOTIFY_REC_CHANGE
,
100 iov
, ARRAY_SIZE(iov
), NULL
, 0);
101 if (!NT_STATUS_IS_OK(status
)) {
102 d_fprintf(stderr
, "Sending rec_change to %s returned %s\n",
103 server_id_str_buf(notifyd
, &idbuf
),
111 ret
= tevent_loop_once(ev
);
113 d_fprintf(stderr
, "tevent_loop_once failed: %s\n",
122 static int net_notify_trigger(struct net_context
*c
, int argc
,
125 struct messaging_context
*msg_ctx
= c
->msg_ctx
;
126 struct server_id_db
*names_db
= messaging_names_db(msg_ctx
);
127 struct server_id notifyd
;
128 struct server_id_buf idbuf
;
129 struct notify_trigger_msg msg
;
135 d_printf("Usage: net notify trigger <path> <action> "
140 ok
= server_id_db_lookup_one(names_db
, "notify-daemon", ¬ifyd
);
142 fprintf(stderr
, "no notify daemon found\n");
146 printf("notify daemon: %s\n", server_id_str_buf(notifyd
, &idbuf
));
148 msg
= (struct notify_trigger_msg
) {
149 .action
= atoi(argv
[1]), .filter
= atoi(argv
[2])
152 iov
[0] = (struct iovec
) {
154 .iov_len
= offsetof(struct notify_trigger_msg
, path
)
156 iov
[1] = (struct iovec
) {
157 .iov_base
= discard_const_p(char, argv
[0]),
158 .iov_len
= strlen(argv
[0])+1
161 status
= messaging_send_iov(
162 c
->msg_ctx
, notifyd
, MSG_SMB_NOTIFY_TRIGGER
,
163 iov
, ARRAY_SIZE(iov
), NULL
, 0);
164 if (!NT_STATUS_IS_OK(status
)) {
165 d_printf("Sending rec_change to %s returned %s\n",
166 server_id_str_buf(notifyd
, &idbuf
),
174 int net_notify(struct net_context
*c
, int argc
, const char **argv
)
176 struct functable func
[] = {
180 N_("Register for a path and listen for changes"),
181 N_("net notify listen <path>")
186 N_("Simulate a trigger action"),
187 N_("net notify trigger <path> <action> <filter>")
189 {NULL
, NULL
, 0, NULL
, NULL
}
192 if (c
->msg_ctx
== NULL
) {
193 d_fprintf(stderr
, "No connection to messaging, need to run "
198 return net_run_function(c
, argc
, argv
, "net notify", func
);