python/tests: fix typo to use correct var
[Samba.git] / source3 / utils / net_notify.c
blob7138bcb2add75443a8ee238448e97dae54d23658
1 /*
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/>.
19 #include "includes.h"
20 #include "utils/net.h"
21 #include "lib/util/tevent_unix.h"
22 #include "lib/util/server_id_db.h"
23 #include "messages.h"
24 #include "source3/smbd/notifyd/notifyd.h"
26 static void net_notify_got_event(struct messaging_context *msg,
27 void *private_data,
28 uint32_t msg_type,
29 struct server_id server_id,
30 DATA_BLOB *data)
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");
36 return;
38 if (data->data[data->length-1] != 0) {
39 d_fprintf(stderr, "path not 0-terminated\n");
40 return;
43 event_msg = (struct notify_event_msg *)data->data;
45 d_printf("%u %s\n", (unsigned)event_msg->action,
46 event_msg->path);
49 static int net_notify_listen(struct net_context *c, int argc,
50 const char **argv)
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;
58 struct iovec iov[2];
59 NTSTATUS status;
60 bool ok;
62 if (argc != 3) {
63 d_printf("Usage: net notify listen <path> <filter> "
64 "<subdir-filter>\n");
65 return -1;
68 ok = server_id_db_lookup_one(names_db, "notify-daemon", &notifyd);
69 if (!ok) {
70 fprintf(stderr, "no notify daemon found\n");
71 return -1;
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) {
81 .iov_base = &msg,
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",
93 nt_errstr(status));
94 return -1;
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),
103 nt_errstr(status));
104 return -1;
107 while (true) {
108 int ret;
110 ret = tevent_loop_once(ev);
111 if (ret != 0) {
112 d_fprintf(stderr, "tevent_loop_once failed: %s\n",
113 strerror(errno));
114 break;
118 return 0;
121 static int net_notify_trigger(struct net_context *c, int argc,
122 const char **argv)
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;
129 struct iovec iov[2];
130 NTSTATUS status;
131 bool ok;
133 if (argc != 3) {
134 d_printf("Usage: net notify trigger <path> <action> "
135 "<filter>\n");
136 return -1;
139 ok = server_id_db_lookup_one(names_db, "notify-daemon", &notifyd);
140 if (!ok) {
141 fprintf(stderr, "no notify daemon found\n");
142 return -1;
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) {
152 .iov_base = &msg,
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),
166 nt_errstr(status));
167 return -1;
170 return 0;
173 int net_notify(struct net_context *c, int argc, const char **argv)
175 struct functable func[] = {
176 { "listen",
177 net_notify_listen,
178 NET_TRANSPORT_LOCAL,
179 N_("Register for a path and listen for changes"),
180 N_("net notify listen <path>")
182 { "trigger",
183 net_notify_trigger,
184 NET_TRANSPORT_LOCAL,
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 "
193 "as root\n");
194 return -1;
197 return net_run_function(c, argc, argv, "net notify", func);