s3:smbd: fix max_buffer handling of initial notify requests
[samba.git] / source3 / smbd / notify.c
1 /*
2    Unix SMB/CIFS implementation.
3    change notify handling
4    Copyright (C) Andrew Tridgell 2000
5    Copyright (C) Jeremy Allison 1994-1998
6    Copyright (C) Volker Lendecke 2007
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../librpc/gen_ndr/ndr_notify.h"
26 #include "librpc/gen_ndr/ndr_file_id.h"
27
28 struct notify_change_event {
29         struct timespec when;
30         uint32_t action;
31         const char *name;
32 };
33
34 struct notify_change_buf {
35         /*
36          * Filters for reinitializing after notifyd has been restarted
37          */
38         uint32_t filter;
39         uint32_t subdir_filter;
40
41         /*
42          * If no requests are pending, changes are queued here. Simple array,
43          * we only append.
44          */
45
46         uint32_t max_buffer_size;
47
48         /*
49          * num_changes == -1 means that we have got a catch-all change, when
50          * asked we just return NT_STATUS_OK without specific changes.
51          */
52         int num_changes;
53         struct notify_change_event *changes;
54
55         /*
56          * If no changes are around requests are queued here. Using a linked
57          * list, because we have to append at the end and delete from the top.
58          */
59         struct notify_change_request *requests;
60 };
61
62 struct notify_change_request {
63         struct notify_change_request *prev, *next;
64         struct files_struct *fsp;       /* backpointer for cancel by mid */
65         struct smb_request *req;
66         uint32_t filter;
67         uint32_t max_param;
68         void (*reply_fn)(struct smb_request *req,
69                          NTSTATUS error_code,
70                          uint8_t *buf, size_t len);
71         struct notify_mid_map *mid_map;
72         void *backend_data;
73 };
74
75 static void notify_fsp(files_struct *fsp, struct timespec when,
76                        uint32_t action, const char *name);
77
78 bool change_notify_fsp_has_changes(struct files_struct *fsp)
79 {
80         if (fsp == NULL) {
81                 return false;
82         }
83
84         if (fsp->notify == NULL) {
85                 return false;
86         }
87
88         if (fsp->notify->num_changes == 0) {
89                 return false;
90         }
91
92         return true;
93 }
94
95 /*
96  * For NTCancel, we need to find the notify_change_request indexed by
97  * mid. Separate list here.
98  */
99
100 struct notify_mid_map {
101         struct notify_mid_map *prev, *next;
102         struct notify_change_request *req;
103         uint64_t mid;
104 };
105
106 static bool notify_change_record_identical(struct notify_change_event *c1,
107                                            struct notify_change_event *c2)
108 {
109         /* Note this is deliberately case sensitive. */
110         if (c1->action == c2->action &&
111                         strcmp(c1->name, c2->name) == 0) {
112                 return True;
113         }
114         return False;
115 }
116
117 static int compare_notify_change_events(const void *p1, const void *p2)
118 {
119         const struct notify_change_event *e1 = p1;
120         const struct notify_change_event *e2 = p2;
121
122         return timespec_compare(&e1->when, &e2->when);
123 }
124
125 static bool notify_marshall_changes(int num_changes,
126                                 uint32_t max_offset,
127                                 struct notify_change_event *changes,
128                                 DATA_BLOB *final_blob)
129 {
130         int i;
131
132         if (num_changes == -1) {
133                 return false;
134         }
135
136         /*
137          * Sort the notifies by timestamp when the event happened to avoid
138          * coalescing and thus dropping events.
139          */
140
141         qsort(changes, num_changes,
142               sizeof(*changes), compare_notify_change_events);
143
144         for (i=0; i<num_changes; i++) {
145                 enum ndr_err_code ndr_err;
146                 struct notify_change_event *c;
147                 struct FILE_NOTIFY_INFORMATION m;
148                 DATA_BLOB blob;
149                 uint16_t pad = 0;
150
151                 /* Coalesce any identical records. */
152                 while (i+1 < num_changes &&
153                         notify_change_record_identical(&changes[i],
154                                                 &changes[i+1])) {
155                         i++;
156                 }
157
158                 c = &changes[i];
159
160                 m.FileName1 = c->name;
161                 m.FileNameLength = strlen_m(c->name)*2;
162                 m.Action = c->action;
163
164                 m._pad = data_blob_null;
165
166                 /*
167                  * Offset to next entry, only if there is one
168                  */
169
170                 if (i == (num_changes-1)) {
171                         m.NextEntryOffset = 0;
172                 } else {
173                         if ((m.FileNameLength % 4) == 2) {
174                                 m._pad = data_blob_const(&pad, 2);
175                         }
176                         m.NextEntryOffset =
177                                 ndr_size_FILE_NOTIFY_INFORMATION(&m, 0);
178                 }
179
180                 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &m,
181                         (ndr_push_flags_fn_t)ndr_push_FILE_NOTIFY_INFORMATION);
182                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
183                         return false;
184                 }
185
186                 if (DEBUGLEVEL >= 10) {
187                         NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION, &m);
188                 }
189
190                 if (!data_blob_append(talloc_tos(), final_blob,
191                                       blob.data, blob.length)) {
192                         data_blob_free(&blob);
193                         return false;
194                 }
195
196                 data_blob_free(&blob);
197
198                 if (final_blob->length > max_offset) {
199                         /* Too much data for client. */
200                         DEBUG(10, ("Client only wanted %d bytes, trying to "
201                                    "marshall %d bytes\n", (int)max_offset,
202                                    (int)final_blob->length));
203                         return False;
204                 }
205         }
206
207         return True;
208 }
209
210 /****************************************************************************
211  Setup the common parts of the return packet and send it.
212 *****************************************************************************/
213
214 void change_notify_reply(struct smb_request *req,
215                          NTSTATUS error_code,
216                          uint32_t max_param,
217                          struct notify_change_buf *notify_buf,
218                          void (*reply_fn)(struct smb_request *req,
219                                           NTSTATUS error_code,
220                                           uint8_t *buf, size_t len))
221 {
222         DATA_BLOB blob = data_blob_null;
223
224         if (!NT_STATUS_IS_OK(error_code)) {
225                 reply_fn(req, error_code, NULL, 0);
226                 return;
227         }
228
229         if (notify_buf == NULL) {
230                 reply_fn(req, NT_STATUS_OK, NULL, 0);
231                 return;
232         }
233
234         max_param = MIN(max_param, notify_buf->max_buffer_size);
235
236         if (!notify_marshall_changes(notify_buf->num_changes, max_param,
237                                         notify_buf->changes, &blob)) {
238                 /*
239                  * We exceed what the client is willing to accept. Send
240                  * nothing.
241                  */
242                 data_blob_free(&blob);
243         }
244
245         reply_fn(req, NT_STATUS_OK, blob.data, blob.length);
246
247         data_blob_free(&blob);
248
249         TALLOC_FREE(notify_buf->changes);
250         notify_buf->num_changes = 0;
251 }
252
253 struct notify_fsp_state {
254         struct files_struct *notified_fsp;
255         struct timespec when;
256         const struct notify_event *e;
257 };
258
259 static struct files_struct *notify_fsp_cb(struct files_struct *fsp,
260                                           void *private_data)
261 {
262         struct notify_fsp_state *state = private_data;
263
264         if (fsp == state->notified_fsp) {
265                 DBG_DEBUG("notify_callback called for %s\n", fsp_str_dbg(fsp));
266                 notify_fsp(fsp, state->when, state->e->action, state->e->path);
267                 return fsp;
268         }
269
270         return NULL;
271 }
272
273 void notify_callback(struct smbd_server_connection *sconn,
274                      void *private_data, struct timespec when,
275                      const struct notify_event *e)
276 {
277         struct notify_fsp_state state = {
278                 .notified_fsp = private_data, .when = when, .e = e
279         };
280         files_forall(sconn, notify_fsp_cb, &state);
281 }
282
283 NTSTATUS change_notify_create(struct files_struct *fsp,
284                               uint32_t max_buffer_size,
285                               uint32_t filter,
286                               bool recursive)
287 {
288         size_t len = fsp_fullbasepath(fsp, NULL, 0);
289         char fullpath[len+1];
290         NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
291
292         if (fsp->notify != NULL) {
293                 DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
294                           "fname = %s\n", fsp->fsp_name->base_name));
295                 return NT_STATUS_INVALID_PARAMETER;
296         }
297
298         if (!(fsp->notify = talloc_zero(NULL, struct notify_change_buf))) {
299                 DEBUG(0, ("talloc failed\n"));
300                 return NT_STATUS_NO_MEMORY;
301         }
302         fsp->notify->filter = filter;
303         fsp->notify->subdir_filter = recursive ? filter : 0;
304         fsp->notify->max_buffer_size = max_buffer_size;
305
306         fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
307
308         /*
309          * Avoid /. at the end of the path name. notify can't deal with it.
310          */
311         if (len > 1 && fullpath[len-1] == '.' && fullpath[len-2] == '/') {
312                 fullpath[len-2] = '\0';
313         }
314
315         if ((fsp->notify->filter != 0) ||
316             (fsp->notify->subdir_filter != 0)) {
317                 status = notify_add(fsp->conn->sconn->notify_ctx,
318                                     fullpath, fsp->notify->filter,
319                                     fsp->notify->subdir_filter, fsp);
320         }
321
322         return status;
323 }
324
325 NTSTATUS change_notify_add_request(struct smb_request *req,
326                                 uint32_t max_param,
327                                 uint32_t filter, bool recursive,
328                                 struct files_struct *fsp,
329                                 void (*reply_fn)(struct smb_request *req,
330                                         NTSTATUS error_code,
331                                         uint8_t *buf, size_t len))
332 {
333         struct notify_change_request *request = NULL;
334         struct notify_mid_map *map = NULL;
335         struct smbd_server_connection *sconn = req->sconn;
336
337         DEBUG(10, ("change_notify_add_request: Adding request for %s: "
338                    "max_param = %d\n", fsp_str_dbg(fsp), (int)max_param));
339
340         if (!(request = talloc(NULL, struct notify_change_request))
341             || !(map = talloc(request, struct notify_mid_map))) {
342                 TALLOC_FREE(request);
343                 return NT_STATUS_NO_MEMORY;
344         }
345
346         request->mid_map = map;
347         map->req = request;
348
349         request->req = talloc_move(request, &req);
350         request->max_param = max_param;
351         request->filter = filter;
352         request->fsp = fsp;
353         request->reply_fn = reply_fn;
354         request->backend_data = NULL;
355
356         DLIST_ADD_END(fsp->notify->requests, request);
357
358         map->mid = request->req->mid;
359         DLIST_ADD(sconn->smb1.notify_mid_maps, map);
360
361         return NT_STATUS_OK;
362 }
363
364 static void change_notify_remove_request(struct smbd_server_connection *sconn,
365                                          struct notify_change_request *remove_req)
366 {
367         files_struct *fsp;
368         struct notify_change_request *req;
369
370         /*
371          * Paranoia checks, the fsp referenced must must have the request in
372          * its list of pending requests
373          */
374
375         fsp = remove_req->fsp;
376         SMB_ASSERT(fsp->notify != NULL);
377
378         for (req = fsp->notify->requests; req; req = req->next) {
379                 if (req == remove_req) {
380                         break;
381                 }
382         }
383
384         if (req == NULL) {
385                 smb_panic("notify_req not found in fsp's requests");
386         }
387
388         DLIST_REMOVE(fsp->notify->requests, req);
389         DLIST_REMOVE(sconn->smb1.notify_mid_maps, req->mid_map);
390         TALLOC_FREE(req);
391 }
392
393 static void smbd_notify_cancel_by_map(struct notify_mid_map *map)
394 {
395         struct smb_request *smbreq = map->req->req;
396         struct smbd_server_connection *sconn = smbreq->sconn;
397         struct smbd_smb2_request *smb2req = smbreq->smb2req;
398         NTSTATUS notify_status = NT_STATUS_CANCELLED;
399
400         if (smb2req != NULL) {
401                 NTSTATUS sstatus;
402
403                 if (smb2req->session == NULL) {
404                         sstatus = NT_STATUS_USER_SESSION_DELETED;
405                 } else {
406                         sstatus = smb2req->session->status;
407                 }
408
409                 if (NT_STATUS_EQUAL(sstatus, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
410                         sstatus = NT_STATUS_OK;
411                 }
412
413                 if (!NT_STATUS_IS_OK(sstatus)) {
414                         notify_status = STATUS_NOTIFY_CLEANUP;
415                 } else if (smb2req->tcon == NULL) {
416                         notify_status = STATUS_NOTIFY_CLEANUP;
417                 } else if (!NT_STATUS_IS_OK(smb2req->tcon->status)) {
418                         notify_status = STATUS_NOTIFY_CLEANUP;
419                 }
420         }
421
422         change_notify_reply(smbreq, notify_status,
423                             0, NULL, map->req->reply_fn);
424         change_notify_remove_request(sconn, map->req);
425 }
426
427 /****************************************************************************
428  Delete entries by mid from the change notify pending queue. Always send reply.
429 *****************************************************************************/
430
431 void remove_pending_change_notify_requests_by_mid(
432         struct smbd_server_connection *sconn, uint64_t mid)
433 {
434         struct notify_mid_map *map;
435
436         for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
437                 if (map->mid == mid) {
438                         break;
439                 }
440         }
441
442         if (map == NULL) {
443                 return;
444         }
445
446         smbd_notify_cancel_by_map(map);
447 }
448
449 void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
450 {
451         struct smbd_server_connection *sconn = smbreq->sconn;
452         struct notify_mid_map *map;
453
454         for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
455                 if (map->req->req == smbreq) {
456                         break;
457                 }
458         }
459
460         if (map == NULL) {
461                 return;
462         }
463
464         smbd_notify_cancel_by_map(map);
465 }
466
467 static struct files_struct *smbd_notify_cancel_deleted_fn(
468         struct files_struct *fsp, void *private_data)
469 {
470         struct file_id *fid = talloc_get_type_abort(
471                 private_data, struct file_id);
472
473         if (file_id_equal(&fsp->file_id, fid)) {
474                 remove_pending_change_notify_requests_by_fid(
475                         fsp, NT_STATUS_DELETE_PENDING);
476         }
477         return NULL;
478 }
479
480 void smbd_notify_cancel_deleted(struct messaging_context *msg,
481                                 void *private_data, uint32_t msg_type,
482                                 struct server_id server_id, DATA_BLOB *data)
483 {
484         struct smbd_server_connection *sconn = talloc_get_type_abort(
485                 private_data, struct smbd_server_connection);
486         struct file_id *fid;
487         enum ndr_err_code ndr_err;
488
489         fid = talloc(talloc_tos(), struct file_id);
490         if (fid == NULL) {
491                 DEBUG(1, ("talloc failed\n"));
492                 return;
493         }
494
495         ndr_err = ndr_pull_struct_blob_all(
496                 data, fid, fid, (ndr_pull_flags_fn_t)ndr_pull_file_id);
497         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
498                 DEBUG(10, ("%s: ndr_pull_file_id failed: %s\n", __func__,
499                            ndr_errstr(ndr_err)));
500                 goto done;
501         }
502
503         files_forall(sconn, smbd_notify_cancel_deleted_fn, fid);
504
505 done:
506         TALLOC_FREE(fid);
507 }
508
509 static struct files_struct *smbd_notifyd_reregister(struct files_struct *fsp,
510                                                     void *private_data)
511 {
512         DBG_DEBUG("reregister %s\n", fsp->fsp_name->base_name);
513
514         if ((fsp->conn->sconn->notify_ctx != NULL) &&
515             (fsp->notify != NULL) &&
516             ((fsp->notify->filter != 0) ||
517              (fsp->notify->subdir_filter != 0))) {
518                 size_t len = fsp_fullbasepath(fsp, NULL, 0);
519                 char fullpath[len+1];
520
521                 NTSTATUS status;
522
523                 fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
524                 if (len > 1 && fullpath[len-1] == '.' &&
525                     fullpath[len-2] == '/') {
526                         fullpath[len-2] = '\0';
527                 }
528
529                 status = notify_add(fsp->conn->sconn->notify_ctx,
530                                     fullpath, fsp->notify->filter,
531                                     fsp->notify->subdir_filter, fsp);
532                 if (!NT_STATUS_IS_OK(status)) {
533                         DBG_DEBUG("notify_add failed: %s\n",
534                                   nt_errstr(status));
535                 }
536         }
537         return NULL;
538 }
539
540 void smbd_notifyd_restarted(struct messaging_context *msg,
541                             void *private_data, uint32_t msg_type,
542                             struct server_id server_id, DATA_BLOB *data)
543 {
544         struct smbd_server_connection *sconn = talloc_get_type_abort(
545                 private_data, struct smbd_server_connection);
546
547         TALLOC_FREE(sconn->notify_ctx);
548
549         sconn->notify_ctx = notify_init(sconn, sconn->msg_ctx,
550                                         sconn, notify_callback);
551         if (sconn->notify_ctx == NULL) {
552                 DBG_DEBUG("notify_init failed\n");
553                 return;
554         }
555
556         files_forall(sconn, smbd_notifyd_reregister, sconn->notify_ctx);
557 }
558
559 /****************************************************************************
560  Delete entries by fnum from the change notify pending queue.
561 *****************************************************************************/
562
563 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
564                                                   NTSTATUS status)
565 {
566         if (fsp->notify == NULL) {
567                 return;
568         }
569
570         while (fsp->notify->requests != NULL) {
571                 change_notify_reply(fsp->notify->requests->req,
572                                     status, 0, NULL,
573                                     fsp->notify->requests->reply_fn);
574                 change_notify_remove_request(fsp->conn->sconn,
575                                              fsp->notify->requests);
576         }
577 }
578
579 void notify_fname(connection_struct *conn, uint32_t action, uint32_t filter,
580                   const char *path)
581 {
582         struct notify_context *notify_ctx = conn->sconn->notify_ctx;
583
584         if (path[0] == '.' && path[1] == '/') {
585                 path += 2;
586         }
587
588         notify_trigger(notify_ctx, action, filter, conn->connectpath, path);
589 }
590
591 static void notify_fsp(files_struct *fsp, struct timespec when,
592                        uint32_t action, const char *name)
593 {
594         struct notify_change_event *change, *changes;
595         char *tmp;
596
597         if (fsp->notify == NULL) {
598                 /*
599                  * Nobody is waiting, don't queue
600                  */
601                 return;
602         }
603
604         /*
605          * Someone has triggered a notify previously, queue the change for
606          * later.
607          */
608
609         if ((fsp->notify->num_changes > 1000) || (name == NULL)) {
610                 /*
611                  * The real number depends on the client buf, just provide a
612                  * guard against a DoS here.  If name == NULL the CN backend is
613                  * alerting us to a problem.  Possibly dropped events.  Clear
614                  * queued changes and send the catch-all response to the client
615                  * if a request is pending.
616                  */
617                 TALLOC_FREE(fsp->notify->changes);
618                 fsp->notify->num_changes = -1;
619                 if (fsp->notify->requests != NULL) {
620                         change_notify_reply(fsp->notify->requests->req,
621                                             NT_STATUS_OK,
622                                             fsp->notify->requests->max_param,
623                                             fsp->notify,
624                                             fsp->notify->requests->reply_fn);
625                         change_notify_remove_request(fsp->conn->sconn,
626                                                      fsp->notify->requests);
627                 }
628                 return;
629         }
630
631         /* If we've exceeded the server side queue or received a NULL name
632          * from the underlying CN implementation, don't queue up any more
633          * requests until we can send a catch-all response to the client */
634         if (fsp->notify->num_changes == -1) {
635                 return;
636         }
637
638         if (!(changes = talloc_realloc(
639                       fsp->notify, fsp->notify->changes,
640                       struct notify_change_event,
641                       fsp->notify->num_changes+1))) {
642                 DEBUG(0, ("talloc_realloc failed\n"));
643                 return;
644         }
645
646         fsp->notify->changes = changes;
647
648         change = &(fsp->notify->changes[fsp->notify->num_changes]);
649
650         if (!(tmp = talloc_strdup(changes, name))) {
651                 DEBUG(0, ("talloc_strdup failed\n"));
652                 return;
653         }
654
655         string_replace(tmp, '/', '\\');
656         change->name = tmp;     
657
658         change->when = when;
659         change->action = action;
660         fsp->notify->num_changes += 1;
661
662         if (fsp->notify->requests == NULL) {
663                 /*
664                  * Nobody is waiting, so don't send anything. The ot
665                  */
666                 return;
667         }
668
669         if (action == NOTIFY_ACTION_OLD_NAME) {
670                 /*
671                  * We have to send the two rename events in one reply. So hold
672                  * the first part back.
673                  */
674                 return;
675         }
676
677         /*
678          * Someone is waiting for the change, trigger the reply immediately.
679          *
680          * TODO: do we have to walk the lists of requests pending?
681          */
682
683         change_notify_reply(fsp->notify->requests->req,
684                             NT_STATUS_OK,
685                             fsp->notify->requests->max_param,
686                             fsp->notify,
687                             fsp->notify->requests->reply_fn);
688
689         change_notify_remove_request(fsp->conn->sconn, fsp->notify->requests);
690 }
691
692 char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32_t filter)
693 {
694         char *result = NULL;
695
696         result = talloc_strdup(mem_ctx, "");
697
698         if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
699                 result = talloc_asprintf_append(result, "FILE_NAME|");
700         if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
701                 result = talloc_asprintf_append(result, "DIR_NAME|");
702         if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
703                 result = talloc_asprintf_append(result, "ATTRIBUTES|");
704         if (filter & FILE_NOTIFY_CHANGE_SIZE)
705                 result = talloc_asprintf_append(result, "SIZE|");
706         if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
707                 result = talloc_asprintf_append(result, "LAST_WRITE|");
708         if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
709                 result = talloc_asprintf_append(result, "LAST_ACCESS|");
710         if (filter & FILE_NOTIFY_CHANGE_CREATION)
711                 result = talloc_asprintf_append(result, "CREATION|");
712         if (filter & FILE_NOTIFY_CHANGE_EA)
713                 result = talloc_asprintf_append(result, "EA|");
714         if (filter & FILE_NOTIFY_CHANGE_SECURITY)
715                 result = talloc_asprintf_append(result, "SECURITY|");
716         if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME)
717                 result = talloc_asprintf_append(result, "STREAM_NAME|");
718         if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE)
719                 result = talloc_asprintf_append(result, "STREAM_SIZE|");
720         if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE)
721                 result = talloc_asprintf_append(result, "STREAM_WRITE|");
722
723         if (result == NULL) return NULL;
724         if (*result == '\0') return result;
725
726         result[strlen(result)-1] = '\0';
727         return result;
728 }
729
730 struct sys_notify_context *sys_notify_context_create(TALLOC_CTX *mem_ctx,
731                                                      struct tevent_context *ev)
732 {
733         struct sys_notify_context *ctx;
734
735         if (!(ctx = talloc(mem_ctx, struct sys_notify_context))) {
736                 DEBUG(0, ("talloc failed\n"));
737                 return NULL;
738         }
739
740         ctx->ev = ev;
741         ctx->private_data = NULL;
742         return ctx;
743 }