2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Volker Lendecke 2015
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.
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.
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/>.
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"
27 #include "locking/proto.h"
28 #include "cleanupdb.h"
30 struct smbd_cleanupd_state
{
34 static void smbd_cleanupd_shutdown(struct messaging_context
*msg
,
35 void *private_data
, uint32_t msg_type
,
36 struct server_id server_id
,
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
,
43 struct tevent_req
*smbd_cleanupd_send(TALLOC_CTX
*mem_ctx
,
44 struct tevent_context
*ev
,
45 struct messaging_context
*msg
,
48 struct tevent_req
*req
;
49 struct smbd_cleanupd_state
*state
;
52 req
= tevent_req_create(mem_ctx
, &state
, struct smbd_cleanupd_state
);
56 state
->parent_pid
= parent_pid
;
58 status
= messaging_register(msg
, req
, MSG_SHUTDOWN
,
59 smbd_cleanupd_shutdown
);
60 if (tevent_req_nterror(req
, status
)) {
61 return tevent_req_post(req
, ev
);
64 status
= messaging_register(msg
, req
, MSG_SMB_NOTIFY_CLEANUP
,
65 smbd_cleanupd_process_exited
);
66 if (tevent_req_nterror(req
, status
)) {
67 return tevent_req_post(req
, ev
);
73 static void smbd_cleanupd_shutdown(struct messaging_context
*msg
,
74 void *private_data
, uint32_t msg_type
,
75 struct server_id server_id
,
78 struct tevent_req
*req
= talloc_get_type_abort(
79 private_data
, struct tevent_req
);
83 struct cleanup_child
{
84 struct cleanup_child
*prev
, *next
;
89 struct cleanupdb_traverse_state
{
92 struct cleanup_child
*children
;
95 static int cleanupdb_traverse_fn(const pid_t pid
,
99 struct cleanupdb_traverse_state
*cleanup_state
=
100 (struct cleanupdb_traverse_state
*)private_data
;
101 struct cleanup_child
*child
= NULL
;
103 child
= talloc_zero(cleanup_state
->mem_ctx
, struct cleanup_child
);
105 DBG_ERR("talloc_zero failed\n");
110 child
->unclean
= unclean
;
111 DLIST_ADD(cleanup_state
->children
, child
);
116 static void smbd_cleanupd_process_exited(struct messaging_context
*msg
,
117 void *private_data
, uint32_t msg_type
,
118 struct server_id server_id
,
121 struct tevent_req
*req
= talloc_get_type_abort(
122 private_data
, struct tevent_req
);
123 struct smbd_cleanupd_state
*state
= tevent_req_data(
124 req
, struct smbd_cleanupd_state
);
126 struct cleanupdb_traverse_state cleanup_state
;
127 TALLOC_CTX
*frame
= talloc_stackframe();
128 struct cleanup_child
*child
= NULL
;
130 cleanup_state
= (struct cleanupdb_traverse_state
) {
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.
141 ret
= cleanupdb_traverse_read(cleanupdb_traverse_fn
, &cleanup_state
);
143 DBG_ERR("cleanupdb_traverse_read failed\n");
153 for (child
= cleanup_state
.children
;
159 ok
= cleanupdb_delete_child(child
->pid
);
161 DBG_ERR("failed to delete pid %d\n", (int)child
->pid
);
164 smbprofile_cleanup(child
->pid
, state
->parent_pid
);
166 ret
= messaging_cleanup(msg
, child
->pid
);
168 if ((ret
!= 0) && (ret
!= ENOENT
)) {
169 DBG_DEBUG("messaging_cleanup returned %s\n",
173 DBG_DEBUG("cleaned up pid %d\n", (int)child
->pid
);
179 NTSTATUS
smbd_cleanupd_recv(struct tevent_req
*req
)
181 return tevent_req_simple_recv_ntstatus(req
);