Add missing require for libwbclient.
[ira/fedora-git] / samba-4.1.7-Make_daemons_systemd_aware.patch
1 From 4be15cc68a49b353f1f2f4f198b968098bee4d83 Mon Sep 17 00:00:00 2001
2 From: Alexander Bokovoy <ab@samba.org>
3 Date: Tue, 25 Mar 2014 12:53:04 +0200
4 Subject: [PATCH 1/5] add systemd integration
5
6 Add --with-systemd / --without-systemd options to check whether
7 libsystemd-daemon library is available and use it to report service
8 startup status to systemd for smbd/winbindd/nmbd and AD DC.
9
10 The problem it solves is correct reporting of the Samba services
11 at the point when they are ready to serve clients, important for
12 high availability software integration.
13
14 BUG: https://bugzilla.samba.org/show_bug.cgi?id=10517
15
16 Signed-off-by: Alexander Bokovoy <ab@samba.org>
17 Reviewed-by: Andreas Schneider <asn@samba.org>
18
19 (cherry picked from commit 54b5d9a7384ae27b2a26586ff909128427c05abe)
20 ---
21  lib/util/become_daemon.c          | 37 ++++++++++++++++++++++++++++++++++++-
22  lib/util/samba_util.h             | 12 ++++++++++++
23  lib/util/wscript_build            |  2 +-
24  packaging/systemd/nmb.service     |  3 ++-
25  packaging/systemd/samba.service   |  3 ++-
26  packaging/systemd/smb.service     |  3 ++-
27  packaging/systemd/winbind.service |  3 ++-
28  source3/nmbd/nmbd.c               |  4 ++++
29  source3/smbd/server.c             |  4 ++++
30  source3/winbindd/winbindd.c       |  5 +++++
31  source4/smbd/server.c             |  4 ++++
32  wscript                           | 27 +++++++++++++++++++++++----
33  12 files changed, 97 insertions(+), 10 deletions(-)
34
35 diff --git a/lib/util/become_daemon.c b/lib/util/become_daemon.c
36 index 2ca0478..35c8b32 100644
37 --- a/lib/util/become_daemon.c
38 +++ b/lib/util/become_daemon.c
39 @@ -24,6 +24,9 @@
40  #include "includes.h"
41  #include "system/filesys.h"
42  #include "system/locale.h"
43 +#if HAVE_SYSTEMD
44 +#include <systemd/sd-daemon.h>
45 +#endif
46  
47  /*******************************************************************
48   Close the low 3 fd's and open dev/null in their place.
49 @@ -75,8 +78,13 @@ _PUBLIC_ void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too)
50  
51  _PUBLIC_ void become_daemon(bool do_fork, bool no_process_group, bool log_stdout)
52  {
53 +       pid_t newpid;
54         if (do_fork) {
55 -               if (fork()) {
56 +               newpid = fork();
57 +               if (newpid) {
58 +#if HAVE_SYSTEMD
59 +                       sd_notifyf(0, "READY=0\nSTATUS=Starting process...\nMAINPID=%lu", (unsigned long) newpid);
60 +#endif /* HAVE_SYSTEMD */
61                         _exit(0);
62                 }
63         }
64 @@ -100,3 +108,30 @@ _PUBLIC_ void become_daemon(bool do_fork, bool no_process_group, bool log_stdout
65          * never close stderr (but debug might dup it onto a log file) */
66         close_low_fds(do_fork, !log_stdout, false);
67  }
68 +
69 +_PUBLIC_ void exit_daemon(const char *msg, int error)
70 +{
71 +#ifdef HAVE_SYSTEMD
72 +       if (msg == NULL) {
73 +               msg = strerror(error);
74 +       }
75 +
76 +       sd_notifyf(0, "STATUS=daemon failed to start: %s\n"
77 +                                 "ERRNO=%i",
78 +                                 msg,
79 +                                 error);
80 +#endif
81 +       DEBUG(0, ("STATUS=daemon failed to start: %s, error code %d\n", msg, error));
82 +       exit(1);
83 +}
84 +
85 +_PUBLIC_ void daemon_ready(const char *daemon)
86 +{
87 +       if (daemon == NULL) {
88 +               daemon = "Samba";
89 +       }
90 +#ifdef HAVE_SYSTEMD
91 +       sd_notifyf(0, "READY=1\nSTATUS=%s: ready to serve connections...", daemon);
92 +#endif
93 +       DEBUG(0, ("STATUS=daemon '%s' finished starting up and ready to serve connections", daemon));
94 +}
95 diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h
96 index f52347e..2996710 100644
97 --- a/lib/util/samba_util.h
98 +++ b/lib/util/samba_util.h
99 @@ -842,6 +842,18 @@ _PUBLIC_ void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too);
100  _PUBLIC_ void become_daemon(bool do_fork, bool no_process_group, bool log_stdout);
101  
102  /**
103 + Exit daemon and print error message to the log at level 0
104 + Optionally report failure to systemd if systemd integration is enabled
105 +**/
106 +_PUBLIC_ void exit_daemon(const char *msg, int error);
107 +
108 +/**
109 + Report that the daemon is ready to serve connections to the log at level 0
110 + Optionally report status to systemd if systemd integration is enabled
111 +**/
112 +_PUBLIC_ void daemon_ready(const char *daemon);
113 +
114 +/**
115   * @brief Get a password from the console.
116   *
117   * You should make sure that the buffer is an empty string!
118 diff --git a/lib/util/wscript_build b/lib/util/wscript_build
119 index 39a1613..5087116 100755
120 --- a/lib/util/wscript_build
121 +++ b/lib/util/wscript_build
122 @@ -10,7 +10,7 @@ bld.SAMBA_LIBRARY('samba-util',
123                      server_id.c dprintf.c parmlist.c bitmap.c pidfile.c
124                      tevent_debug.c util_process.c''',
125                    deps='DYNCONFIG',
126 -                  public_deps='talloc tevent execinfo uid_wrapper pthread LIBCRYPTO charset util_setid',
127 +                  public_deps='talloc tevent execinfo uid_wrapper pthread LIBCRYPTO charset util_setid systemd-daemon',
128                    public_headers='debug.h attr.h byteorder.h data_blob.h memory.h safe_string.h time.h talloc_stack.h xfile.h dlinklist.h samba_util.h string_wrappers.h',
129                    header_path= [ ('dlinklist.h samba_util.h', '.'), ('*', 'util') ],
130                    local_include=False,
131 diff --git a/packaging/systemd/nmb.service b/packaging/systemd/nmb.service
132 index e5e81a1..3d71a7d 100644
133 --- a/packaging/systemd/nmb.service
134 +++ b/packaging/systemd/nmb.service
135 @@ -3,7 +3,8 @@ Description=Samba NMB Daemon
136  After=syslog.target network.target
137  
138  [Service]
139 -Type=forking
140 +Type=notify
141 +NotifyAccess=all
142  PIDFile=/run/nmbd.pid
143  EnvironmentFile=-/etc/sysconfig/samba
144  ExecStart=/usr/sbin/nmbd $NMBDOPTIONS
145 diff --git a/packaging/systemd/samba.service b/packaging/systemd/samba.service
146 index e2878d1..824f89c 100644
147 --- a/packaging/systemd/samba.service
148 +++ b/packaging/systemd/samba.service
149 @@ -3,7 +3,8 @@ Description=Samba AD Daemon
150  After=syslog.target network.target
151  
152  [Service]
153 -Type=forking
154 +Type=notify
155 +NotifyAccess=all
156  PIDFile=/run/samba.pid
157  LimitNOFILE=16384
158  EnvironmentFile=-/etc/sysconfig/samba
159 diff --git a/packaging/systemd/smb.service b/packaging/systemd/smb.service
160 index d0d945a..9810891 100644
161 --- a/packaging/systemd/smb.service
162 +++ b/packaging/systemd/smb.service
163 @@ -3,7 +3,8 @@ Description=Samba SMB Daemon
164  After=syslog.target network.target nmb.service winbind.service
165  
166  [Service]
167 -Type=forking
168 +Type=notify
169 +NotifyAccess=all
170  PIDFile=/run/smbd.pid
171  LimitNOFILE=16384
172  EnvironmentFile=-/etc/sysconfig/samba
173 diff --git a/packaging/systemd/winbind.service b/packaging/systemd/winbind.service
174 index eff266f..f711a17 100644
175 --- a/packaging/systemd/winbind.service
176 +++ b/packaging/systemd/winbind.service
177 @@ -3,7 +3,8 @@ Description=Samba Winbind Daemon
178  After=syslog.target network.target nmb.service
179  
180  [Service]
181 -Type=forking
182 +Type=notify
183 +NotifyAccess=all
184  PIDFile=/run/winbindd.pid
185  EnvironmentFile=-/etc/sysconfig/samba
186  ExecStart=/usr/sbin/winbindd "$WINBINDOPTIONS"
187 diff --git a/source3/nmbd/nmbd.c b/source3/nmbd/nmbd.c
188 index ec0e7d0..aced774 100644
189 --- a/source3/nmbd/nmbd.c
190 +++ b/source3/nmbd/nmbd.c
191 @@ -1088,6 +1088,10 @@ static bool open_sockets(bool isdaemon, int port)
192                  exit(1);
193          }
194  
195 +       if (is_daemon && !opt_interactive) {
196 +               daemon_ready("nmbd");
197 +       }
198 +
199         TALLOC_FREE(frame);
200         process(msg);
201  
202 diff --git a/source3/smbd/server.c b/source3/smbd/server.c
203 index d3cd33e..b2a9d8f 100644
204 --- a/source3/smbd/server.c
205 +++ b/source3/smbd/server.c
206 @@ -1489,6 +1489,10 @@ extern void build_options(bool screen);
207                 exit(1);
208         }
209  
210 +       if (is_daemon && !interactive) {
211 +               daemon_ready("smbd");
212 +       }
213 +
214         /* only start other daemons if we are running as a daemon
215          * -- bad things will happen if smbd is launched via inetd
216          *  and we fork a copy of ourselves here */
217 diff --git a/source3/winbindd/winbindd.c b/source3/winbindd/winbindd.c
218 index 50573ac..61c1dbc 100644
219 --- a/source3/winbindd/winbindd.c
220 +++ b/source3/winbindd/winbindd.c
221 @@ -1581,6 +1581,11 @@ int main(int argc, char **argv, char **envp)
222         }
223  
224         TALLOC_FREE(frame);
225 +
226 +       if (!interactive) {
227 +               daemon_ready("winbindd");
228 +       }
229 +
230         /* Loop waiting for requests */
231         while (1) {
232                 frame = talloc_stackframe();
233 diff --git a/source4/smbd/server.c b/source4/smbd/server.c
234 index 37aac62..3a23190 100644
235 --- a/source4/smbd/server.c
236 +++ b/source4/smbd/server.c
237 @@ -498,6 +498,10 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
238                 return 1;
239         }
240  
241 +       if (opt_daemon) {
242 +               daemon_ready("samba");
243 +       }
244 +
245         /* wait for events - this is where smbd sits for most of its
246            life */
247         tevent_loop_wait(event_ctx);
248 diff --git a/wscript b/wscript
249 index 83c82e3..2f02c6e 100644
250 --- a/wscript
251 +++ b/wscript
252 @@ -64,8 +64,15 @@ def set_options(opt):
253                    help=("Disable Position Independent Executable builds"),
254                    action="store_false", dest='enable_pie')
255  
256 -    gr = opt.option_group('developer options')
257 +    opt.add_option('--with-systemd',
258 +                   help=("Enable systemd integration"),
259 +                   action='store_true', dest='enable_systemd')
260 +
261 +    opt.add_option('--without-systemd',
262 +                   help=("Disable systemd integration"),
263 +                   action='store_false', dest='enable_systemd')
264  
265 +    gr = opt.option_group('developer options')
266  
267      opt.tool_options('python') # options for disabling pyc or pyo compilation
268      # enable options related to building python extensions
269 @@ -162,11 +169,8 @@ def configure(conf):
270      if not conf.CHECK_CODE('#include "tests/summary.c"',
271                             define='SUMMARY_PASSES',
272                             addmain=False,
273 -                           execute=True,
274                             msg='Checking configure summary'):
275          raise Utils.WafError('configure summary failed')
276 -    
277 -    conf.SAMBA_CONFIG_H('include/config.h')
278  
279      if Options.options.enable_pie != False:
280          if Options.options.enable_pie == True:
281 @@ -178,6 +182,21 @@ def configure(conf):
282                           msg="Checking compiler for PIE support"):
283                 conf.env['ENABLE_PIE'] = True
284  
285 +    if Options.options.enable_systemd != False:
286 +        conf.check_cfg(package='libsystemd-daemon', args='--cflags --libs',
287 +                       msg='Checking for libsystemd-daemon', uselib_store="SYSTEMD-DAEMON")
288 +        conf.CHECK_HEADERS('systemd/sd-daemon.h', lib='systemd-daemon')
289 +        conf.CHECK_LIB('systemd-daemon', shlib=True)
290 +
291 +    if conf.CONFIG_SET('HAVE_SYSTEMD_SD_DAEMON_H'):
292 +        conf.DEFINE('HAVE_SYSTEMD', '1')
293 +        conf.env['ENABLE_SYSTEMD'] = True
294 +    else:
295 +        conf.SET_TARGET_TYPE('systemd-daemon', 'EMPTY')
296 +        conf.undefine('HAVE_SYSTEMD')
297 +
298 +    conf.SAMBA_CONFIG_H('include/config.h')
299 +
300  def etags(ctx):
301      '''build TAGS file using etags'''
302      import Utils
303 -- 
304 1.8.5.3
305
306
307 From a6250f706655bd8fda81fe15ccc9441f7d4e1c84 Mon Sep 17 00:00:00 2001
308 From: Alexander Bokovoy <ab@samba.org>
309 Date: Wed, 26 Mar 2014 10:56:12 +0200
310 Subject: [PATCH 2/5] smbd: use exit_daemon() to support reporting to systemd
311  from smbd
312
313 BUG: https://bugzilla.samba.org/show_bug.cgi?id=10517
314
315 Signed-off-by: Alexander Bokovoy <ab@samba.org>
316 Reviewed-by: Andreas Schneider <asn@samba.org>
317 ---
318  source3/smbd/server.c | 44 +++++++++++++++++++-------------------------
319  1 file changed, 19 insertions(+), 25 deletions(-)
320
321 diff --git a/source3/smbd/server.c b/source3/smbd/server.c
322 index b2a9d8f..34c949d 100644
323 --- a/source3/smbd/server.c
324 +++ b/source3/smbd/server.c
325 @@ -1314,8 +1314,7 @@ extern void build_options(bool screen);
326                                    ev_ctx,
327                                    false);
328         if (!NT_STATUS_IS_OK(status)) {
329 -               DEBUG(0,("reinit_after_fork() failed\n"));
330 -               exit(1);
331 +               exit_daemon("reinit_after_fork() failed", map_errno_from_nt_status(status));
332         }
333  
334         if (!interactive) {
335 @@ -1326,8 +1325,7 @@ extern void build_options(bool screen);
336                  */
337                 status = init_before_fork();
338                 if (!NT_STATUS_IS_OK(status)) {
339 -                       DEBUG(0, ("init_before_fork failed: %s\n", nt_errstr(status)));
340 -                       exit(1);
341 +                       exit_daemon(nt_errstr(status), map_errno_from_nt_status(status));
342                 }
343         }
344  
345 @@ -1360,7 +1358,7 @@ extern void build_options(bool screen);
346         /* Setup all the TDB's - including CLEAR_IF_FIRST tdb's. */
347  
348         if (smbd_memcache() == NULL) {
349 -               exit(1);
350 +               exit_daemon("no memcache available", EACCES);
351         }
352  
353         memcache_set_global(smbd_memcache());
354 @@ -1372,69 +1370,65 @@ extern void build_options(bool screen);
355                 exit(1);
356  
357         if (!secrets_init()) {
358 -               DEBUG(0, ("ERROR: smbd can not open secrets.tdb\n"));
359 -               exit(1);
360 +               exit_daemon("smbd can not open secrets.tdb", EACCES);
361         }
362  
363         if (lp_server_role() == ROLE_DOMAIN_BDC || lp_server_role() == ROLE_DOMAIN_PDC) {
364                 struct loadparm_context *lp_ctx = loadparm_init_s3(NULL, loadparm_s3_helpers());
365                 if (!open_schannel_session_store(NULL, lp_ctx)) {
366 -                       DEBUG(0,("ERROR: Samba cannot open schannel store for secured NETLOGON operations.\n"));
367 -                       exit(1);
368 +                       exit_daemon("ERROR: Samba cannot open schannel store for secured NETLOGON operations.", EACCES);
369                 }
370                 TALLOC_FREE(lp_ctx);
371         }
372  
373         if(!get_global_sam_sid()) {
374 -               DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
375 -               exit(1);
376 +               exit_daemon("Samba cannot create a SAM SID", EACCES);
377         }
378  
379         server_id = messaging_server_id(msg_ctx);
380         status = smbXsrv_version_global_init(&server_id);
381         if (!NT_STATUS_IS_OK(status)) {
382 -               exit(1);
383 +               exit_daemon("Samba cannot init server context", EACCES);
384         }
385  
386         status = smbXsrv_session_global_init();
387         if (!NT_STATUS_IS_OK(status)) {
388 -               exit(1);
389 +               exit_daemon("Samba cannot init session context", EACCES);
390         }
391  
392         status = smbXsrv_tcon_global_init();
393         if (!NT_STATUS_IS_OK(status)) {
394 -               exit(1);
395 +               exit_daemon("Samba cannot init tcon context", EACCES);
396         }
397  
398         if (!locking_init())
399 -               exit(1);
400 +               exit_daemon("Samba cannot init locking", EACCES);
401  
402         if (!messaging_tdb_parent_init(ev_ctx)) {
403 -               exit(1);
404 +               exit_daemon("Samba cannot init TDB messaging", EACCES);
405         }
406  
407         if (!smbd_parent_notify_init(NULL, msg_ctx, ev_ctx)) {
408 -               exit(1);
409 +               exit_daemon("Samba cannot init notification", EACCES);
410         }
411  
412         if (!smbd_scavenger_init(NULL, msg_ctx, ev_ctx)) {
413 -               exit(1);
414 +               exit_daemon("Samba cannot init scavenging", EACCES);
415         }
416  
417         if (!serverid_parent_init(ev_ctx)) {
418 -               exit(1);
419 +               exit_daemon("Samba cannot init server id", EACCES);
420         }
421  
422         if (!W_ERROR_IS_OK(registry_init_full()))
423 -               exit(1);
424 +               exit_daemon("Samba cannot init registry", EACCES);
425  
426         /* Open the share_info.tdb here, so we don't have to open
427            after the fork on every single connection.  This is a small
428            performance improvment and reduces the total number of system
429            fds used. */
430         if (!share_info_db_init()) {
431 -               DEBUG(0,("ERROR: failed to load share info db.\n"));
432 -               exit(1);
433 +               exit_daemon("ERROR: failed to load share info db.", EACCES);
434         }
435  
436         status = init_system_session_info();
437 @@ -1455,7 +1449,7 @@ extern void build_options(bool screen);
438         }
439         status = smbXsrv_open_global_init();
440         if (!NT_STATUS_IS_OK(status)) {
441 -               exit(1);
442 +               exit_daemon("Samba cannot init global open", map_errno_from_nt_status(status));
443         }
444  
445         /* This MUST be done before start_epmd() because otherwise
446 @@ -1486,7 +1480,7 @@ extern void build_options(bool screen);
447         }
448  
449         if (!dcesrv_ep_setup(ev_ctx, msg_ctx)) {
450 -               exit(1);
451 +               exit_daemon("Samba cannot setup ep pipe", EACCES);
452         }
453  
454         if (is_daemon && !interactive) {
455 @@ -1507,7 +1501,7 @@ extern void build_options(bool screen);
456                         bool bgq = lp_parm_bool(-1, "smbd", "backgroundqueue", true);
457  
458                         if (!printing_subsystem_init(ev_ctx, msg_ctx, true, bgq)) {
459 -                               exit(1);
460 +                               exit_daemon("Samba failed to init printing subsystem", EACCES);
461                         }
462                 }
463         } else if (!lp__disable_spoolss() &&
464 -- 
465 1.8.5.3
466
467
468 From 580933198c1da02ad668b32f68bfeac833d97980 Mon Sep 17 00:00:00 2001
469 From: Alexander Bokovoy <ab@samba.org>
470 Date: Wed, 26 Mar 2014 11:34:56 +0200
471 Subject: [PATCH 3/5] nmbd: use exit_daemon() to report status to systemd
472
473 BUG: https://bugzilla.samba.org/show_bug.cgi?id=10517
474
475 Signed-off-by: Alexander Bokovoy <ab@samba.org>
476 Reviewed-by: Andreas Schneider <asn@samba.org>
477
478 (cherry picked from commit a54e94559b5ebabbb8c5c938fdf159efceff39e6)
479 ---
480  source3/nmbd/nmbd.c | 39 ++++++++++++++-------------------------
481  1 file changed, 14 insertions(+), 25 deletions(-)
482
483 diff --git a/source3/nmbd/nmbd.c b/source3/nmbd/nmbd.c
484 index aced774..dc2d4c7 100644
485 --- a/source3/nmbd/nmbd.c
486 +++ b/source3/nmbd/nmbd.c
487 @@ -966,16 +966,12 @@ static bool open_sockets(bool isdaemon, int port)
488  
489         ok = directory_create_or_exist(lp_lockdir(), geteuid(), 0755);
490         if (!ok) {
491 -               DEBUG(0, ("Failed to create directory %s for lock files - %s\n",
492 -                         lp_lockdir(), strerror(errno)));
493 -               exit(1);
494 +               exit_daemon("Failed to create directory for lock files, check 'lock directory'", errno);
495         }
496  
497         ok = directory_create_or_exist(lp_piddir(), geteuid(), 0755);
498         if (!ok) {
499 -               DEBUG(0, ("Failed to create directory %s for pid files - %s\n",
500 -                         lp_piddir(), strerror(errno)));
501 -               exit(1);
502 +               exit_daemon("Failed to create directory for pid files, check 'pid directory'", errno);
503         }
504  
505         pidfile_create(lp_piddir(), "nmbd");
506 @@ -984,8 +980,7 @@ static bool open_sockets(bool isdaemon, int port)
507                                    false);
508  
509         if (!NT_STATUS_IS_OK(status)) {
510 -               DEBUG(0,("reinit_after_fork() failed\n"));
511 -               exit(1);
512 +               exit_daemon("reinit_after_fork() failed", map_errno_from_nt_status(status));
513         }
514  
515         /*
516 @@ -995,16 +990,15 @@ static bool open_sockets(bool isdaemon, int port)
517          */
518         status = init_before_fork();
519         if (!NT_STATUS_IS_OK(status)) {
520 -               DEBUG(0, ("init_before_fork failed: %s\n", nt_errstr(status)));
521 -               exit(1);
522 +               exit_daemon(nt_errstr(status), map_errno_from_nt_status(status));
523         }
524  
525         if (!nmbd_setup_sig_term_handler(msg))
526 -               exit(1);
527 +               exit_daemon("NMBD failed to setup signal handler", EINVAL);
528         if (!nmbd_setup_stdin_handler(msg, !Fork))
529 -               exit(1);
530 +               exit_daemon("NMBD failed to setup stdin handler", EINVAL);
531         if (!nmbd_setup_sig_hup_handler(msg))
532 -               exit(1);
533 +               exit_daemon("NMBD failed to setup SIGHUP handler", EINVAL);
534  
535         /* get broadcast messages */
536  
537 @@ -1012,8 +1006,7 @@ static bool open_sockets(bool isdaemon, int port)
538                                 FLAG_MSG_GENERAL |
539                                 FLAG_MSG_NMBD |
540                                 FLAG_MSG_DBWRAP)) {
541 -               DEBUG(1, ("Could not register myself in serverid.tdb\n"));
542 -               exit(1);
543 +               exit_daemon("Could not register NMBD process in serverid.tdb", EACCES);
544         }
545  
546         messaging_register(msg, NULL, MSG_FORCE_ELECTION,
547 @@ -1044,9 +1037,8 @@ static bool open_sockets(bool isdaemon, int port)
548  
549         /* Create an nmbd subnet record for each of the above. */
550         if( False == create_subnets() ) {
551 -               DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n"));
552                 kill_async_dns_child();
553 -               exit(1);
554 +               exit_daemon("NMBD failed when creating subnet lists", EACCES);
555         }
556  
557         /* Load in any static local names. */ 
558 @@ -1058,9 +1050,8 @@ static bool open_sockets(bool isdaemon, int port)
559  
560         /* If we are acting as a WINS server, initialise data structures. */
561         if( !initialise_wins() ) {
562 -               DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) );
563                 kill_async_dns_child();
564 -               exit(1);
565 +               exit_daemon( "NMBD failed when initialising WINS server.", EACCES);
566         }
567  
568         /* 
569 @@ -1072,21 +1063,19 @@ static bool open_sockets(bool isdaemon, int port)
570          */
571  
572         if( False == register_my_workgroup_and_names() ) {
573 -               DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n"));
574                 kill_async_dns_child();
575 -               exit(1);
576 +               exit_daemon( "NMBD failed when creating my workgroup.", EACCES);
577         }
578  
579         if (!initialize_nmbd_proxy_logon()) {
580 -               DEBUG(0,("ERROR: Failed setup nmbd_proxy_logon.\n"));
581                 kill_async_dns_child();
582 -               exit(1);
583 +               exit_daemon( "NMBD failed to setup nmbd_proxy_logon.", EACCES);
584         }
585  
586         if (!nmbd_init_packet_server()) {
587                 kill_async_dns_child();
588 -                exit(1);
589 -        }
590 +               exit_daemon( "NMBD failed to setup packet server.", EACCES);
591 +       }
592  
593         if (is_daemon && !opt_interactive) {
594                 daemon_ready("nmbd");
595 -- 
596 1.8.5.3
597
598
599 From 1067bdd3e590d5ab880044456c667e3663d99c95 Mon Sep 17 00:00:00 2001
600 From: Alexander Bokovoy <ab@samba.org>
601 Date: Wed, 26 Mar 2014 11:45:21 +0200
602 Subject: [PATCH 4/5] winbindd: use exit_daemon() to pass startup status to
603  systemd
604
605 BUG: https://bugzilla.samba.org/show_bug.cgi?id=10517
606
607 Signed-off-by: Alexander Bokovoy <ab@samba.org>
608 Reviewed-by: Andreas Schneider <asn@samba.org>
609 (cherry picked from commit 91013315c9fa946bf85c85df0ccd40a803f3dc6f)
610 ---
611  source3/winbindd/winbindd.c | 13 ++++---------
612  1 file changed, 4 insertions(+), 9 deletions(-)
613
614 diff --git a/source3/winbindd/winbindd.c b/source3/winbindd/winbindd.c
615 index 61c1dbc..68ffd7e 100644
616 --- a/source3/winbindd/winbindd.c
617 +++ b/source3/winbindd/winbindd.c
618 @@ -1543,8 +1543,7 @@ int main(int argc, char **argv, char **envp)
619                                    winbind_event_context(),
620                                    false);
621         if (!NT_STATUS_IS_OK(status)) {
622 -               DEBUG(0,("reinit_after_fork() failed\n"));
623 -               exit(1);
624 +               exit_daemon("Winbindd reinit_after_fork() failed", map_errno_from_nt_status(status));
625         }
626  
627         /*
628 @@ -1554,17 +1553,14 @@ int main(int argc, char **argv, char **envp)
629          */
630         status = init_before_fork();
631         if (!NT_STATUS_IS_OK(status)) {
632 -               DEBUG(0, ("init_before_fork failed: %s\n", nt_errstr(status)));
633 -               exit(1);
634 +               exit_daemon(nt_errstr(status), map_errno_from_nt_status(status));
635         }
636  
637         winbindd_register_handlers(winbind_messaging_context(), !Fork);
638  
639         status = init_system_session_info();
640         if (!NT_STATUS_IS_OK(status)) {
641 -               DEBUG(1, ("ERROR: failed to setup system user info: %s.\n",
642 -                         nt_errstr(status)));
643 -               exit(1);
644 +               exit_daemon("Winbindd failed to setup system user info", map_errno_from_nt_status(status));
645         }
646  
647         rpc_lsarpc_init(NULL);
648 @@ -1576,8 +1572,7 @@ int main(int argc, char **argv, char **envp)
649         /* setup listen sockets */
650  
651         if (!winbindd_setup_listeners()) {
652 -               DEBUG(0,("winbindd_setup_listeners() failed\n"));
653 -               exit(1);
654 +               exit_daemon("Winbindd failed to setup listeners", EPIPE);
655         }
656  
657         TALLOC_FREE(frame);
658 -- 
659 1.8.5.3
660
661
662 From 037b9fd66e777571e2c963945e41535371b4683e Mon Sep 17 00:00:00 2001
663 From: Alexander Bokovoy <ab@samba.org>
664 Date: Wed, 26 Mar 2014 12:30:30 +0200
665 Subject: [PATCH 5/5] ad-dc: use exit_daemon() to communicate status of startup
666  to systemd
667
668 BUG: https://bugzilla.samba.org/show_bug.cgi?id=10517
669
670 Signed-off-by: Alexander Bokovoy <ab@samba.org>
671 Reviewed-by: Andreas Schneider <asn@samba.org>
672 (cherry picked from commit a343303cbcd5af303455843ebf202e2f30daf3a6)
673 ---
674  source4/smbd/server.c | 19 +++++++------------
675  1 file changed, 7 insertions(+), 12 deletions(-)
676
677 diff --git a/source4/smbd/server.c b/source4/smbd/server.c
678 index 3a23190..d5d9d9c 100644
679 --- a/source4/smbd/server.c
680 +++ b/source4/smbd/server.c
681 @@ -398,15 +398,13 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
682  
683         if (lpcfg_server_role(cmdline_lp_ctx) == ROLE_ACTIVE_DIRECTORY_DC) {
684                 if (!open_schannel_session_store(talloc_autofree_context(), cmdline_lp_ctx)) {
685 -                       DEBUG(0,("ERROR: Samba cannot open schannel store for secured NETLOGON operations.\n"));
686 -                       exit(1);
687 +                       exit_daemon("Samba cannot open schannel store for secured NETLOGON operations.", EACCES);
688                 }
689         }
690  
691         /* make sure we won't go through nss_winbind */
692         if (!winbind_off()) {
693 -               DEBUG(0,("Failed to disable recusive winbindd calls.  Exiting.\n"));
694 -               exit(1);
695 +               exit_daemon("Samba failed to disable recusive winbindd calls.", EACCES);
696         }
697  
698         gensec_init(); /* FIXME: */
699 @@ -431,8 +429,7 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
700         event_ctx = s4_event_context_init(talloc_autofree_context());
701  
702         if (event_ctx == NULL) {
703 -               DEBUG(0,("Initializing event context failed\n"));
704 -               return 1;
705 +               exit_daemon("Initializing event context failed", EACCES);
706         }
707  
708         if (opt_interactive) {
709 @@ -449,7 +446,7 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
710  #endif
711  
712         if (fstat(0, &st) != 0) {
713 -               exit(1);
714 +               exit_daemon("Samba failed to set standard input handler", ENOTTY);
715         }
716  
717         if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
718 @@ -478,15 +475,14 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
719                 DEBUG(0, ("At this time the 'samba' binary should only be used for either:\n"));
720                 DEBUGADD(0, ("'server role = active directory domain controller' or to access the ntvfs file server with 'server services = +smb' or the rpc proxy with 'dcerpc endpoint servers = remote'\n"));
721                 DEBUGADD(0, ("You should start smbd/nmbd/winbindd instead for domain member and standalone file server tasks\n"));
722 -               exit(1);
723 +               exit_daemon("Samba detected misconfigured 'server role' and exited. Check logs for details", EINVAL);
724         };
725  
726         prime_ldb_databases(event_ctx);
727  
728         status = setup_parent_messaging(event_ctx, cmdline_lp_ctx);
729         if (!NT_STATUS_IS_OK(status)) {
730 -               DEBUG(0,("Failed to setup parent messaging - %s\n", nt_errstr(status)));
731 -               return 1;
732 +               exit_daemon("Samba failed to setup parent messaging", NT_STATUS_V(status));
733         }
734  
735         DEBUG(0,("%s: using '%s' process model\n", binary_name, model));
736 @@ -494,8 +490,7 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
737         status = server_service_startup(event_ctx, cmdline_lp_ctx, model, 
738                                         lpcfg_server_services(cmdline_lp_ctx));
739         if (!NT_STATUS_IS_OK(status)) {
740 -               DEBUG(0,("Starting Services failed - %s\n", nt_errstr(status)));
741 -               return 1;
742 +               exit_daemon("Samba failed to start services", NT_STATUS_V(status));
743         }
744  
745         if (opt_daemon) {
746 -- 
747 1.8.5.3
748