smbd: Move cleanupd revalidate to a separate fn
[obnox/samba/samba-obnox.git] / source3 / smbd / smbd_cleanupd.c
1 /*
2  * Unix SMB/CIFS implementation.
3  *
4  * Copyright (C) Volker Lendecke 2015
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "smbd_cleanupd.h"
22 #include "lib/util_procid.h"
23 #include "lib/util/tevent_ntstatus.h"
24 #include "lib/util/debug.h"
25 #include "smbprofile.h"
26 #include "serverid.h"
27 #include "locking/proto.h"
28
29 struct smbd_cleanupd_state {
30         pid_t parent_pid;
31 };
32
33 static void smbd_cleanupd_shutdown(struct messaging_context *msg,
34                                    void *private_data, uint32_t msg_type,
35                                    struct server_id server_id,
36                                    DATA_BLOB *data);
37 static void smbd_cleanupd_process_exited(struct messaging_context *msg,
38                                          void *private_data, uint32_t msg_type,
39                                          struct server_id server_id,
40                                          DATA_BLOB *data);
41 static void smbd_cleanupd_unlock(struct messaging_context *msg,
42                                  void *private_data, uint32_t msg_type,
43                                  struct server_id server_id,
44                                  DATA_BLOB *data);
45
46 struct tevent_req *smbd_cleanupd_send(TALLOC_CTX *mem_ctx,
47                                       struct tevent_context *ev,
48                                       struct messaging_context *msg,
49                                       pid_t parent_pid)
50 {
51         struct tevent_req *req;
52         struct smbd_cleanupd_state *state;
53         NTSTATUS status;
54
55         req = tevent_req_create(mem_ctx, &state, struct smbd_cleanupd_state);
56         if (req == NULL) {
57                 return NULL;
58         }
59         state->parent_pid = parent_pid;
60
61         status = messaging_register(msg, req, MSG_SHUTDOWN,
62                                     smbd_cleanupd_shutdown);
63         if (tevent_req_nterror(req, status)) {
64                 return tevent_req_post(req, ev);
65         }
66
67         status = messaging_register(msg, req, MSG_SMB_NOTIFY_CLEANUP,
68                                     smbd_cleanupd_process_exited);
69         if (tevent_req_nterror(req, status)) {
70                 return tevent_req_post(req, ev);
71         }
72
73         status = messaging_register(msg, NULL, MSG_SMB_UNLOCK,
74                                     smbd_cleanupd_unlock);
75         if (tevent_req_nterror(req, status)) {
76                 return tevent_req_post(req, ev);
77         }
78
79         return req;
80 }
81
82 static void smbd_cleanupd_shutdown(struct messaging_context *msg,
83                                    void *private_data, uint32_t msg_type,
84                                    struct server_id server_id,
85                                    DATA_BLOB *data)
86 {
87         struct tevent_req *req = talloc_get_type_abort(
88                 private_data, struct tevent_req);
89         tevent_req_done(req);
90 }
91
92 static void smbd_cleanupd_unlock(struct messaging_context *msg,
93                                  void *private_data, uint32_t msg_type,
94                                  struct server_id server_id,
95                                  DATA_BLOB *data)
96 {
97         brl_revalidate(msg, private_data, msg_type, server_id, data);
98 }
99
100 static void smbd_cleanupd_process_exited(struct messaging_context *msg,
101                                          void *private_data, uint32_t msg_type,
102                                          struct server_id server_id,
103                                          DATA_BLOB *data)
104 {
105         struct tevent_req *req = talloc_get_type_abort(
106                 private_data, struct tevent_req);
107         struct smbd_cleanupd_state *state = tevent_req_data(
108                 req, struct smbd_cleanupd_state);
109         pid_t pid;
110         struct server_id child_id;
111         bool unclean_shutdown;
112         int ret;
113
114         if (data->length != (sizeof(pid) + sizeof(unclean_shutdown))) {
115                 DBG_WARNING("Got invalid length: %zu\n", data->length);
116                 return;
117         }
118
119         memcpy(&pid, data->data, sizeof(pid));
120         memcpy(&unclean_shutdown, data->data + sizeof(pid),
121                sizeof(unclean_shutdown));
122
123         DBG_DEBUG("%d exited %sclean\n", (int)pid,
124                   unclean_shutdown ? "un" : "");
125
126         /*
127          * Get child_id before messaging_cleanup which wipes the
128          * unique_id. Not that it really matters here for functionality (the
129          * child should have properly cleaned up :-)) though, but it looks
130          * nicer.
131          */
132         child_id = pid_to_procid(pid);
133
134         smbprofile_cleanup(pid, state->parent_pid);
135
136         ret = messaging_cleanup(msg, pid);
137
138         if ((ret != 0) && (ret != ENOENT)) {
139                 DBG_DEBUG("messaging_cleanup returned %s\n", strerror(ret));
140         }
141
142         if (!serverid_deregister(child_id)) {
143                 DEBUG(1, ("Could not remove pid %d from serverid.tdb\n",
144                           (int)pid));
145         }
146 }
147
148 NTSTATUS smbd_cleanupd_recv(struct tevent_req *req)
149 {
150         return tevent_req_simple_recv_ntstatus(req);
151 }