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"
29 struct smbd_cleanupd_state
{
33 static void smbd_cleanupd_shutdown(struct messaging_context
*msg
,
34 void *private_data
, uint32_t msg_type
,
35 struct server_id server_id
,
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
,
41 static void smbd_cleanupd_unlock(struct messaging_context
*msg
,
42 void *private_data
, uint32_t msg_type
,
43 struct server_id server_id
,
46 struct tevent_req
*smbd_cleanupd_send(TALLOC_CTX
*mem_ctx
,
47 struct tevent_context
*ev
,
48 struct messaging_context
*msg
,
51 struct tevent_req
*req
;
52 struct smbd_cleanupd_state
*state
;
55 req
= tevent_req_create(mem_ctx
, &state
, struct smbd_cleanupd_state
);
59 state
->parent_pid
= parent_pid
;
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
);
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
);
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
);
82 static void smbd_cleanupd_shutdown(struct messaging_context
*msg
,
83 void *private_data
, uint32_t msg_type
,
84 struct server_id server_id
,
87 struct tevent_req
*req
= talloc_get_type_abort(
88 private_data
, struct tevent_req
);
92 static void smbd_cleanupd_unlock(struct messaging_context
*msg
,
93 void *private_data
, uint32_t msg_type
,
94 struct server_id server_id
,
97 DBG_WARNING("Cleaning up brl and lock database after unclean "
100 message_send_all(msg
, MSG_SMB_UNLOCK
, NULL
, 0, NULL
);
102 brl_revalidate(msg
, private_data
, msg_type
, server_id
, data
);
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
,
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
);
115 struct server_id child_id
;
116 bool unclean_shutdown
;
119 if (data
->length
!= (sizeof(pid
) + sizeof(unclean_shutdown
))) {
120 DBG_WARNING("Got invalid length: %zu\n", data
->length
);
124 memcpy(&pid
, data
->data
, sizeof(pid
));
125 memcpy(&unclean_shutdown
, data
->data
+ sizeof(pid
),
126 sizeof(unclean_shutdown
));
128 DBG_DEBUG("%d exited %sclean\n", (int)pid
,
129 unclean_shutdown
? "un" : "");
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
137 child_id
= pid_to_procid(pid
);
139 smbprofile_cleanup(pid
, state
->parent_pid
);
141 ret
= messaging_cleanup(msg
, pid
);
143 if ((ret
!= 0) && (ret
!= ENOENT
)) {
144 DBG_DEBUG("messaging_cleanup returned %s\n", strerror(ret
));
147 if (!serverid_deregister(child_id
)) {
148 DEBUG(1, ("Could not remove pid %d from serverid.tdb\n",
153 NTSTATUS
smbd_cleanupd_recv(struct tevent_req
*req
)
155 return tevent_req_simple_recv_ntstatus(req
);