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/tevent_unix.h"
22 #include "lib/util/server_id_db.h"
24 #include "source3/smbd/notifyd/notifyd.h"
26 static void net_notify_got_event(struct messaging_context
*msg
,
29 struct server_id server_id
,
32 struct notify_event_msg
*event_msg
;
34 if (data
->length
< offsetof(struct notify_event_msg
, path
) + 1) {
35 d_fprintf(stderr
, "message too short\n");
38 if (data
->data
[data
->length
-1] != 0) {
39 d_fprintf(stderr
, "path not 0-terminated\n");
43 event_msg
= (struct notify_event_msg
*)data
->data
;
45 d_printf("%u %s\n", (unsigned)event_msg
->action
,
49 static int net_notify_listen(struct net_context
*c
, int argc
,
52 struct messaging_context
*msg_ctx
= c
->msg_ctx
;
53 struct tevent_context
*ev
= messaging_tevent_context(msg_ctx
);
54 struct server_id_db
*names_db
= messaging_names_db(msg_ctx
);
55 struct server_id notifyd
;
56 struct server_id_buf idbuf
;
57 struct notify_rec_change_msg msg
;
63 d_printf("Usage: net notify listen <path> <filter> "
68 ok
= server_id_db_lookup_one(names_db
, "notify-daemon", ¬ifyd
);
70 fprintf(stderr
, "no notify daemon found\n");
74 printf("notify daemon: %s\n", server_id_str_buf(notifyd
, &idbuf
));
76 msg
= (struct notify_rec_change_msg
) {
77 .instance
.filter
= atoi(argv
[1]),
78 .instance
.subdir_filter
= atoi(argv
[2])
80 iov
[0] = (struct iovec
) {
82 .iov_len
= offsetof(struct notify_rec_change_msg
, path
)
84 iov
[1] = (struct iovec
) {
85 .iov_base
= discard_const_p(char, argv
[0]),
86 .iov_len
= strlen(argv
[0])+1
89 status
= messaging_register(c
->msg_ctx
, NULL
, MSG_PVFS_NOTIFY
,
90 net_notify_got_event
);
91 if (!NT_STATUS_IS_OK(status
)) {
92 d_fprintf(stderr
, "messaging_register failed: %s\n",
97 status
= messaging_send_iov(
98 c
->msg_ctx
, notifyd
, MSG_SMB_NOTIFY_REC_CHANGE
,
99 iov
, ARRAY_SIZE(iov
), NULL
, 0);
100 if (!NT_STATUS_IS_OK(status
)) {
101 d_fprintf(stderr
, "Sending rec_change to %s returned %s\n",
102 server_id_str_buf(notifyd
, &idbuf
),
110 ret
= tevent_loop_once(ev
);
112 d_fprintf(stderr
, "tevent_loop_once failed: %s\n",
121 static int net_notify_trigger(struct net_context
*c
, int argc
,
124 struct messaging_context
*msg_ctx
= c
->msg_ctx
;
125 struct server_id_db
*names_db
= messaging_names_db(msg_ctx
);
126 struct server_id notifyd
;
127 struct server_id_buf idbuf
;
128 struct notify_trigger_msg msg
;
134 d_printf("Usage: net notify trigger <path> <action> "
139 ok
= server_id_db_lookup_one(names_db
, "notify-daemon", ¬ifyd
);
141 fprintf(stderr
, "no notify daemon found\n");
145 printf("notify daemon: %s\n", server_id_str_buf(notifyd
, &idbuf
));
147 msg
= (struct notify_trigger_msg
) {
148 .action
= atoi(argv
[1]), .filter
= atoi(argv
[2])
151 iov
[0] = (struct iovec
) {
153 .iov_len
= offsetof(struct notify_trigger_msg
, path
)
155 iov
[1] = (struct iovec
) {
156 .iov_base
= discard_const_p(char, argv
[0]),
157 .iov_len
= strlen(argv
[0])+1
160 status
= messaging_send_iov(
161 c
->msg_ctx
, notifyd
, MSG_SMB_NOTIFY_TRIGGER
,
162 iov
, ARRAY_SIZE(iov
), NULL
, 0);
163 if (!NT_STATUS_IS_OK(status
)) {
164 d_printf("Sending rec_change to %s returned %s\n",
165 server_id_str_buf(notifyd
, &idbuf
),
173 int net_notify(struct net_context
*c
, int argc
, const char **argv
)
175 struct functable func
[] = {
179 N_("Register for a path and listen for changes"),
180 N_("net notify listen <path>")
185 N_("Simulate a trigger action"),
186 N_("net notify trigger <path> <action> <filter>")
188 {NULL
, NULL
, 0, NULL
, NULL
}
191 if (c
->msg_ctx
== NULL
) {
192 d_fprintf(stderr
, "No connection to messaging, need to run "
197 return net_run_function(c
, argc
, argv
, "net notify", func
);