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 | DBG_WARNING("Cleaning up brl and lock database after unclean "
|
---|
98 | "shutdown\n");
|
---|
99 |
|
---|
100 | message_send_all(msg, MSG_SMB_UNLOCK, NULL, 0, NULL);
|
---|
101 |
|
---|
102 | brl_revalidate(msg, private_data, msg_type, server_id, data);
|
---|
103 | }
|
---|
104 |
|
---|
105 | static void smbd_cleanupd_process_exited(struct messaging_context *msg,
|
---|
106 | void *private_data, uint32_t msg_type,
|
---|
107 | struct server_id server_id,
|
---|
108 | DATA_BLOB *data)
|
---|
109 | {
|
---|
110 | struct tevent_req *req = talloc_get_type_abort(
|
---|
111 | private_data, struct tevent_req);
|
---|
112 | struct smbd_cleanupd_state *state = tevent_req_data(
|
---|
113 | req, struct smbd_cleanupd_state);
|
---|
114 | pid_t pid;
|
---|
115 | struct server_id child_id;
|
---|
116 | bool unclean_shutdown;
|
---|
117 | int ret;
|
---|
118 |
|
---|
119 | if (data->length != (sizeof(pid) + sizeof(unclean_shutdown))) {
|
---|
120 | DBG_WARNING("Got invalid length: %zu\n", data->length);
|
---|
121 | return;
|
---|
122 | }
|
---|
123 |
|
---|
124 | memcpy(&pid, data->data, sizeof(pid));
|
---|
125 | memcpy(&unclean_shutdown, data->data + sizeof(pid),
|
---|
126 | sizeof(unclean_shutdown));
|
---|
127 |
|
---|
128 | DBG_DEBUG("%d exited %sclean\n", (int)pid,
|
---|
129 | unclean_shutdown ? "un" : "");
|
---|
130 |
|
---|
131 | /*
|
---|
132 | * Get child_id before messaging_cleanup which wipes the
|
---|
133 | * unique_id. Not that it really matters here for functionality (the
|
---|
134 | * child should have properly cleaned up :-)) though, but it looks
|
---|
135 | * nicer.
|
---|
136 | */
|
---|
137 | child_id = pid_to_procid(pid);
|
---|
138 |
|
---|
139 | smbprofile_cleanup(pid, state->parent_pid);
|
---|
140 |
|
---|
141 | ret = messaging_cleanup(msg, pid);
|
---|
142 |
|
---|
143 | if ((ret != 0) && (ret != ENOENT)) {
|
---|
144 | DBG_DEBUG("messaging_cleanup returned %s\n", strerror(ret));
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (!serverid_deregister(child_id)) {
|
---|
148 | DEBUG(1, ("Could not remove pid %d from serverid.tdb\n",
|
---|
149 | (int)pid));
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | NTSTATUS smbd_cleanupd_recv(struct tevent_req *req)
|
---|
154 | {
|
---|
155 | return tevent_req_simple_recv_ntstatus(req);
|
---|
156 | }
|
---|