1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/*
* Printing background queue helper
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "replace.h"
#include "system/filesys.h"
#include "lib/util/server_id.h"
#include "source3/locking/share_mode_lock.h"
#include "source3/param/loadparm.h"
#include "source3/param/param_proto.h"
#include "lib/cmdline/cmdline.h"
#include "lib/cmdline/closefrom_except.h"
#include "lib/util/talloc_stack.h"
#include "lib/util/debug.h"
#include "lib/util/signal.h"
#include "lib/util/fault.h"
#include "lib/util/become_daemon.h"
#include "lib/util/charset/charset.h"
#include "lib/util/samba_util.h"
#include "lib/util/sys_rw.h"
#include "lib/util/pidfile.h"
#include "lib/async_req/async_sock.h"
#include "dynconfig/dynconfig.h"
#include "source3/lib/global_contexts.h"
#include "messages.h"
#include "nsswitch/winbind_client.h"
#include "source3/include/auth.h"
#include "source3/lib/util_procid.h"
#include "source3/auth/proto.h"
#include "source3/printing/queue_process.h"
#include "source3/lib/substitute.h"
static void watch_handler(struct tevent_req *req)
{
bool *pdone = tevent_req_callback_data_void(req);
*pdone = true;
}
static void bgqd_sig_term_handler(
struct tevent_context *ev,
struct tevent_signal *se,
int signum,
int count,
void *siginfo,
void *private_data)
{
bool *pdone = private_data;
*pdone = true;
}
static bool ready_signal_filter(
struct messaging_rec *rec, void *private_data)
{
pid_t pid = getpid();
ssize_t written;
if (rec->msg_type != MSG_DAEMON_READY_FD) {
return false;
}
if (rec->num_fds != 1) {
return false;
}
written = sys_write(rec->fds[0], &pid, sizeof(pid));
if (written != sizeof(pid)) {
DBG_ERR("Could not write pid: %s\n", strerror(errno));
}
return false;
}
static int samba_bgqd_pidfile_create(
struct messaging_context *msg_ctx,
const char *progname,
int ready_signal_fd)
{
const char *piddir = lp_pid_directory();
size_t len = strlen(piddir) + strlen(progname) + 6;
char pidFile[len];
pid_t existing_pid;
int fd, ret;
snprintf(pidFile,
sizeof(pidFile),
"%s/%s.pid",
piddir, progname);
ret = pidfile_path_create(pidFile, &fd, &existing_pid);
if (ret == 0) {
struct tevent_req *ready_signal_req = NULL;
/*
* Listen for fd's sent via MSG_DAEMON_READY_FD:
* Multiple instances of this process might have raced
* for creating the pidfile. Make sure the parent does
* not suffer from this race, reply on behalf of the
* loser of this race.
*/
ready_signal_req = messaging_filtered_read_send(
msg_ctx,
messaging_tevent_context(msg_ctx),
msg_ctx,
ready_signal_filter,
NULL);
if (ready_signal_req == NULL) {
DBG_DEBUG("messaging_filtered_read_send failed\n");
pidfile_unlink(piddir, progname);
pidfile_fd_close(fd);
return ENOMEM;
}
/* leak fd */
return 0;
}
if (ret != EAGAIN) {
DBG_DEBUG("pidfile_path_create() failed: %s\n",
strerror(ret));
return ret;
}
DBG_DEBUG("%s pid %d exists\n", progname, (int)existing_pid);
if (ready_signal_fd != -1) {
/*
* We lost the race for the pidfile, but someone else
* can report readiness on our behalf.
*/
NTSTATUS status = messaging_send_iov(
msg_ctx,
pid_to_procid(existing_pid),
MSG_DAEMON_READY_FD,
NULL,
0,
&ready_signal_fd,
1);
if (!NT_STATUS_IS_OK(status)) {
DBG_DEBUG("Could not send ready_signal_fd: %s\n",
nt_errstr(status));
}
}
return EAGAIN;
}
int main(int argc, const char *argv[])
{
struct samba_cmdline_daemon_cfg *cmdline_daemon_cfg = NULL;
const struct loadparm_substitution *lp_sub =
loadparm_s3_global_substitution();
const char *progname = getprogname();
TALLOC_CTX *frame = NULL;
poptContext pc;
struct messaging_context *msg_ctx = NULL;
struct tevent_context *ev = NULL;
struct tevent_req *watch_req = NULL;
struct tevent_signal *sigterm_handler = NULL;
struct bq_state *bq = NULL;
int log_stdout = 0;
int ready_signal_fd = -1;
int watch_fd = -1;
NTSTATUS status;
int ret;
bool ok;
bool done = false;
int exitcode = 1;
struct poptOption long_options[] = {
POPT_AUTOHELP
POPT_COMMON_SAMBA
POPT_COMMON_DAEMON
/*
* File descriptor to write the PID of the helper
* process to
*/
{
.longName = "ready-signal-fd",
.argInfo = POPT_ARG_INT,
.arg = &ready_signal_fd,
.descrip = "Fd to signal readiness to" ,
},
/*
* Read end of a pipe held open by the parent
* smbd. Exit this process when it becomes readable.
*/
{
.longName = "parent-watch-fd",
.argInfo = POPT_ARG_INT,
.arg = &watch_fd,
.descrip = "Fd to watch for exiting",
},
POPT_TABLEEND
};
{
const char *fd_params[] = {
"ready-signal-fd", "parent-watch-fd",
};
closefrom_except_fd_params(
3, ARRAY_SIZE(fd_params), fd_params, argc, argv);
}
talloc_enable_null_tracking();
frame = talloc_stackframe();
umask(0);
set_remote_machine_name("smbd-bgqd", true);
ok = samba_cmdline_init(frame,
SAMBA_CMDLINE_CONFIG_SERVER,
true /* require_smbconf */);
if (!ok) {
DBG_ERR("Failed to setup cmdline parser!\n");
exit(ENOMEM);
}
cmdline_daemon_cfg = samba_cmdline_get_daemon_cfg();
pc = samba_popt_get_context(progname,
argc,
argv,
long_options,
0);
if (pc == NULL) {
DBG_ERR("Failed to get popt context!\n");
exit(ENOMEM);
}
ret = poptGetNextOpt(pc);
if (ret < -1) {
fprintf(stderr, "invalid options: %s\n", poptStrerror(ret));
goto done;
}
poptFreeContext(pc);
log_stdout = (debug_get_log_type() == DEBUG_STDOUT);
/* main process will notify systemd */
daemon_sd_notifications(false);
if (!cmdline_daemon_cfg->fork) {
daemon_status(progname, "Starting process ... ");
} else {
become_daemon(true,
cmdline_daemon_cfg->no_process_group,
log_stdout);
}
BlockSignals(true, SIGPIPE);
smb_init_locale();
dump_core_setup(progname, lp_logfile(frame, lp_sub));
msg_ctx = global_messaging_context();
if (msg_ctx == NULL) {
DBG_ERR("messaging_init() failed\n");
goto done;
}
ev = messaging_tevent_context(msg_ctx);
ret = samba_bgqd_pidfile_create(msg_ctx, progname, ready_signal_fd);
if (ret != 0) {
goto done;
}
if (watch_fd != -1) {
watch_req = wait_for_read_send(ev, ev, watch_fd, true);
if (watch_req == NULL) {
fprintf(stderr, "tevent_add_fd failed\n");
goto done;
}
tevent_req_set_callback(watch_req, watch_handler, &done);
}
(void)winbind_off();
ok = init_guest_session_info(frame);
(void)winbind_on();
if (!ok) {
DBG_ERR("init_guest_session_info failed\n");
goto done;
}
(void)winbind_off();
status = init_system_session_info(frame);
(void)winbind_on();
if (!NT_STATUS_IS_OK(status)) {
DBG_ERR("init_system_session_info failed: %s\n",
nt_errstr(status));
goto done;
}
sigterm_handler = tevent_add_signal(
ev, frame, SIGTERM, 0, bgqd_sig_term_handler, &done);
if (sigterm_handler == NULL) {
DBG_ERR("Could not install SIGTERM handler\n");
goto done;
}
bq = register_printing_bq_handlers(frame, msg_ctx);
if (bq == NULL) {
DBG_ERR("Could not register bq handlers\n");
goto done;
}
ok = locking_init();
if (!ok) {
DBG_ERR("locking_init failed\n");
goto done;
}
if (ready_signal_fd != -1) {
pid_t pid = getpid();
ssize_t written;
written = sys_write(ready_signal_fd, &pid, sizeof(pid));
if (written != sizeof(pid)) {
DBG_ERR("Reporting readiness failed\n");
goto done;
}
close(ready_signal_fd);
ready_signal_fd = -1;
}
while (!done) {
TALLOC_CTX *tmp = talloc_stackframe();
ret = tevent_loop_once(ev);
TALLOC_FREE(tmp);
if (ret != 0) {
DBG_ERR("tevent_loop_once failed\n");
break;
}
}
exitcode = 0;
done:
TALLOC_FREE(watch_req);
TALLOC_FREE(bq);
TALLOC_FREE(sigterm_handler);
global_messaging_context_free();
TALLOC_FREE(frame);
return exitcode;
}
|