Line data Source code
1 : /*
2 : * Samba Unix/Linux notifyd client code
3 : * Copyright (C) 2015 Volker Lendecke <vl@samba.org>
4 : *
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.
9 : *
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.
14 : *
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/>.
17 : */
18 :
19 : #include "includes.h"
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"
24 : #include "messages.h"
25 : #include "source3/smbd/notifyd/notifyd.h"
26 :
27 0 : static void net_notify_got_event(struct messaging_context *msg,
28 : void *private_data,
29 : uint32_t msg_type,
30 : struct server_id server_id,
31 : DATA_BLOB *data)
32 : {
33 0 : struct notify_event_msg *event_msg;
34 :
35 0 : if (data->length < offsetof(struct notify_event_msg, path) + 1) {
36 0 : d_fprintf(stderr, "message too short\n");
37 0 : return;
38 : }
39 0 : if (data->data[data->length-1] != 0) {
40 0 : d_fprintf(stderr, "path not 0-terminated\n");
41 0 : return;
42 : }
43 :
44 0 : event_msg = (struct notify_event_msg *)data->data;
45 :
46 0 : d_printf("%u %s\n", (unsigned)event_msg->action,
47 0 : event_msg->path);
48 : }
49 :
50 0 : static int net_notify_listen(struct net_context *c, int argc,
51 : const char **argv)
52 : {
53 0 : struct messaging_context *msg_ctx = c->msg_ctx;
54 0 : struct tevent_context *ev = messaging_tevent_context(msg_ctx);
55 0 : struct server_id_db *names_db = messaging_names_db(msg_ctx);
56 0 : struct server_id notifyd;
57 0 : struct server_id_buf idbuf;
58 0 : struct notify_rec_change_msg msg;
59 0 : struct iovec iov[2];
60 0 : NTSTATUS status;
61 0 : bool ok;
62 :
63 0 : if (argc != 3) {
64 0 : d_printf("Usage: net notify listen <path> <filter> "
65 : "<subdir-filter>\n");
66 0 : return -1;
67 : }
68 :
69 0 : ok = server_id_db_lookup_one(names_db, "notify-daemon", ¬ifyd);
70 0 : if (!ok) {
71 0 : fprintf(stderr, "no notify daemon found\n");
72 0 : return -1;
73 : }
74 :
75 0 : printf("notify daemon: %s\n", server_id_str_buf(notifyd, &idbuf));
76 :
77 0 : msg = (struct notify_rec_change_msg) {
78 0 : .instance.filter = atoi(argv[1]),
79 0 : .instance.subdir_filter = atoi(argv[2])
80 : };
81 0 : iov[0] = (struct iovec) {
82 : .iov_base = &msg,
83 : .iov_len = offsetof(struct notify_rec_change_msg, path)
84 : };
85 0 : iov[1] = (struct iovec) {
86 0 : .iov_base = discard_const_p(char, argv[0]),
87 0 : .iov_len = strlen(argv[0])+1
88 : };
89 :
90 0 : status = messaging_register(c->msg_ctx, NULL, MSG_PVFS_NOTIFY,
91 : net_notify_got_event);
92 0 : if (!NT_STATUS_IS_OK(status)) {
93 0 : d_fprintf(stderr, "messaging_register failed: %s\n",
94 : nt_errstr(status));
95 0 : return -1;
96 : }
97 :
98 0 : status = messaging_send_iov(
99 : c->msg_ctx, notifyd, MSG_SMB_NOTIFY_REC_CHANGE,
100 : iov, ARRAY_SIZE(iov), NULL, 0);
101 0 : if (!NT_STATUS_IS_OK(status)) {
102 0 : d_fprintf(stderr, "Sending rec_change to %s returned %s\n",
103 : server_id_str_buf(notifyd, &idbuf),
104 : nt_errstr(status));
105 0 : return -1;
106 : }
107 :
108 0 : while (true) {
109 0 : int ret;
110 :
111 0 : ret = tevent_loop_once(ev);
112 0 : if (ret != 0) {
113 0 : d_fprintf(stderr, "tevent_loop_once failed: %s\n",
114 0 : strerror(errno));
115 0 : break;
116 : }
117 : }
118 :
119 0 : return 0;
120 : }
121 :
122 0 : static int net_notify_trigger(struct net_context *c, int argc,
123 : const char **argv)
124 : {
125 0 : struct messaging_context *msg_ctx = c->msg_ctx;
126 0 : struct server_id_db *names_db = messaging_names_db(msg_ctx);
127 0 : struct server_id notifyd;
128 0 : struct server_id_buf idbuf;
129 0 : struct notify_trigger_msg msg;
130 0 : struct iovec iov[2];
131 0 : NTSTATUS status;
132 0 : bool ok;
133 :
134 0 : if (argc != 3) {
135 0 : d_printf("Usage: net notify trigger <path> <action> "
136 : "<filter>\n");
137 0 : return -1;
138 : }
139 :
140 0 : ok = server_id_db_lookup_one(names_db, "notify-daemon", ¬ifyd);
141 0 : if (!ok) {
142 0 : fprintf(stderr, "no notify daemon found\n");
143 0 : return -1;
144 : }
145 :
146 0 : printf("notify daemon: %s\n", server_id_str_buf(notifyd, &idbuf));
147 :
148 0 : msg = (struct notify_trigger_msg) {
149 0 : .action = atoi(argv[1]), .filter = atoi(argv[2])
150 : };
151 :
152 0 : iov[0] = (struct iovec) {
153 : .iov_base = &msg,
154 : .iov_len = offsetof(struct notify_trigger_msg, path)
155 : };
156 0 : iov[1] = (struct iovec) {
157 0 : .iov_base = discard_const_p(char, argv[0]),
158 0 : .iov_len = strlen(argv[0])+1
159 : };
160 :
161 0 : status = messaging_send_iov(
162 : c->msg_ctx, notifyd, MSG_SMB_NOTIFY_TRIGGER,
163 : iov, ARRAY_SIZE(iov), NULL, 0);
164 0 : if (!NT_STATUS_IS_OK(status)) {
165 0 : d_printf("Sending rec_change to %s returned %s\n",
166 : server_id_str_buf(notifyd, &idbuf),
167 : nt_errstr(status));
168 0 : return -1;
169 : }
170 :
171 0 : return 0;
172 : }
173 :
174 0 : int net_notify(struct net_context *c, int argc, const char **argv)
175 : {
176 0 : struct functable func[] = {
177 : { "listen",
178 : net_notify_listen,
179 : NET_TRANSPORT_LOCAL,
180 : N_("Register for a path and listen for changes"),
181 : N_("net notify listen <path>")
182 : },
183 : { "trigger",
184 : net_notify_trigger,
185 : NET_TRANSPORT_LOCAL,
186 : N_("Simulate a trigger action"),
187 : N_("net notify trigger <path> <action> <filter>")
188 : },
189 : {NULL, NULL, 0, NULL, NULL}
190 : };
191 :
192 0 : if (c->msg_ctx == NULL) {
193 0 : d_fprintf(stderr, "No connection to messaging, need to run "
194 : "as root\n");
195 0 : return -1;
196 : }
197 :
198 0 : return net_run_function(c, argc, argv, "net notify", func);
199 : }
|