Line data Source code
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 : #include "cleanupdb.h"
29 :
30 : struct smbd_cleanupd_state {
31 : pid_t parent_pid;
32 : };
33 :
34 : static void smbd_cleanupd_shutdown(struct messaging_context *msg,
35 : void *private_data, uint32_t msg_type,
36 : struct server_id server_id,
37 : DATA_BLOB *data);
38 : static void smbd_cleanupd_process_exited(struct messaging_context *msg,
39 : void *private_data, uint32_t msg_type,
40 : struct server_id server_id,
41 : DATA_BLOB *data);
42 :
43 0 : struct tevent_req *smbd_cleanupd_send(TALLOC_CTX *mem_ctx,
44 : struct tevent_context *ev,
45 : struct messaging_context *msg,
46 : pid_t parent_pid)
47 : {
48 0 : struct tevent_req *req;
49 0 : struct smbd_cleanupd_state *state;
50 0 : NTSTATUS status;
51 :
52 0 : req = tevent_req_create(mem_ctx, &state, struct smbd_cleanupd_state);
53 0 : if (req == NULL) {
54 0 : return NULL;
55 : }
56 0 : state->parent_pid = parent_pid;
57 :
58 0 : status = messaging_register(msg, req, MSG_SHUTDOWN,
59 : smbd_cleanupd_shutdown);
60 0 : if (tevent_req_nterror(req, status)) {
61 0 : return tevent_req_post(req, ev);
62 : }
63 :
64 0 : status = messaging_register(msg, req, MSG_SMB_NOTIFY_CLEANUP,
65 : smbd_cleanupd_process_exited);
66 0 : if (tevent_req_nterror(req, status)) {
67 0 : return tevent_req_post(req, ev);
68 : }
69 :
70 0 : return req;
71 : }
72 :
73 0 : static void smbd_cleanupd_shutdown(struct messaging_context *msg,
74 : void *private_data, uint32_t msg_type,
75 : struct server_id server_id,
76 : DATA_BLOB *data)
77 : {
78 0 : struct tevent_req *req = talloc_get_type_abort(
79 : private_data, struct tevent_req);
80 0 : tevent_req_done(req);
81 0 : }
82 :
83 : struct cleanup_child {
84 : struct cleanup_child *prev, *next;
85 : pid_t pid;
86 : bool unclean;
87 : };
88 :
89 : struct cleanupdb_traverse_state {
90 : TALLOC_CTX *mem_ctx;
91 : bool ok;
92 : struct cleanup_child *children;
93 : };
94 :
95 0 : static int cleanupdb_traverse_fn(const pid_t pid,
96 : const bool unclean,
97 : void *private_data)
98 : {
99 0 : struct cleanupdb_traverse_state *cleanup_state =
100 : (struct cleanupdb_traverse_state *)private_data;
101 0 : struct cleanup_child *child = NULL;
102 :
103 0 : child = talloc_zero(cleanup_state->mem_ctx, struct cleanup_child);
104 0 : if (child == NULL) {
105 0 : DBG_ERR("talloc_zero failed\n");
106 0 : return -1;
107 : }
108 :
109 0 : child->pid = pid;
110 0 : child->unclean = unclean;
111 0 : DLIST_ADD(cleanup_state->children, child);
112 :
113 0 : return 0;
114 : }
115 :
116 0 : static void smbd_cleanupd_process_exited(struct messaging_context *msg,
117 : void *private_data, uint32_t msg_type,
118 : struct server_id server_id,
119 : DATA_BLOB *data)
120 : {
121 0 : struct tevent_req *req = talloc_get_type_abort(
122 : private_data, struct tevent_req);
123 0 : struct smbd_cleanupd_state *state = tevent_req_data(
124 : req, struct smbd_cleanupd_state);
125 0 : int ret;
126 0 : struct cleanupdb_traverse_state cleanup_state;
127 0 : TALLOC_CTX *frame = talloc_stackframe();
128 0 : struct cleanup_child *child = NULL;
129 :
130 0 : cleanup_state = (struct cleanupdb_traverse_state) {
131 : .mem_ctx = frame
132 : };
133 :
134 : /*
135 : * This merely collect children in a list, whatever we're
136 : * supposed to cleanup for every child, it has to take place
137 : * *after* the db traverse in a list loop. This is to minimize
138 : * locking interaction between the traverse and writers (i.e.
139 : * the parent smbd).
140 : */
141 0 : ret = cleanupdb_traverse_read(cleanupdb_traverse_fn, &cleanup_state);
142 0 : if (ret < 0) {
143 0 : DBG_ERR("cleanupdb_traverse_read failed\n");
144 0 : TALLOC_FREE(frame);
145 0 : return;
146 : }
147 :
148 0 : if (ret == 0) {
149 0 : TALLOC_FREE(frame);
150 0 : return;
151 : }
152 :
153 0 : for (child = cleanup_state.children;
154 0 : child != NULL;
155 0 : child = child->next)
156 : {
157 0 : bool ok;
158 :
159 0 : ok = cleanupdb_delete_child(child->pid);
160 0 : if (!ok) {
161 0 : DBG_ERR("failed to delete pid %d\n", (int)child->pid);
162 : }
163 :
164 0 : smbprofile_cleanup(child->pid, state->parent_pid);
165 :
166 0 : ret = messaging_cleanup(msg, child->pid);
167 :
168 0 : if ((ret != 0) && (ret != ENOENT)) {
169 0 : DBG_DEBUG("messaging_cleanup returned %s\n",
170 : strerror(ret));
171 : }
172 :
173 0 : DBG_DEBUG("cleaned up pid %d\n", (int)child->pid);
174 : }
175 :
176 0 : TALLOC_FREE(frame);
177 : }
178 :
179 0 : NTSTATUS smbd_cleanupd_recv(struct tevent_req *req)
180 : {
181 0 : return tevent_req_simple_recv_ntstatus(req);
182 : }
|