ctdb-daemon: Move switch_from_server_to_client() to ctdb_daemon.c
[kai/samba-autobuild/.git] / ctdb / wscript
index 81039e61510dc581c1ee1879720151c678f13480..cea9e42a49ca84d1a2f9ebe0c4b3a3cce18593ed 100755 (executable)
@@ -26,18 +26,37 @@ else:
 version = samba_version.samba_version_file('%s/VERSION' % vdir, vdir, env)
 VERSION = version.STRING.replace('-', '.')
 
-Options.default_prefix = '/usr/local'
+default_prefix = Options.default_prefix = '/usr/local'
 
 samba_dist.DIST_DIRS('''ctdb:. lib/replace:lib/replace lib/talloc:lib/talloc
                         lib/tevent:lib/tevent lib/tdb:lib/tdb
                         lib/socket_wrapper:lib/socket_wrapper
                         third_party/popt:third_party/popt
-                        buildtools:buildtools''')
+                        lib/util:lib/util lib/tdb_wrap:lib/tdb_wrap
+                        lib/ccan:lib/ccan libcli/util:libcli/util
+                        buildtools:buildtools third_party/waf:third_party/waf''')
+
+manpages = [
+    'ctdb.1',
+    'ctdb.7',
+    'ctdbd.1',
+    'ctdbd.conf.5',
+    'ctdbd_wrapper.1',
+    'ctdb-statistics.7',
+    'ctdb-tunables.7',
+    'ltdbtool.1',
+    'onnode.1',
+    'ping_pong.1'
+]
 
 
 def set_options(opt):
     opt.PRIVATE_EXTENSION_DEFAULT('ctdb')
+
     opt.RECURSE('lib/replace')
+
+    opt.RECURSE('lib/util')
+
     opt.RECURSE('lib/talloc')
     opt.RECURSE('lib/tevent')
     opt.RECURSE('lib/tdb')
@@ -54,7 +73,7 @@ def set_options(opt):
                    action="store", dest='ctdb_logdir', default=None)
     opt.add_option('--with-socketpath',
                    help=("path to CTDB daemon socket"),
-                   action="store_true", dest='ctdb_sockpath', default=False)
+                   action="store", dest='ctdb_sockpath', default=None)
 
 
 def configure(conf):
@@ -64,9 +83,17 @@ def configure(conf):
 
     # No need to build python bindings for talloc/tevent/tdb
     if conf.IN_LAUNCH_DIR():
+        conf.env.standalone_ctdb = True
         Options.options.disable_python = True
 
     conf.RECURSE('lib/replace')
+
+    if conf.env.standalone_ctdb:
+        conf.SAMBA_CHECK_PERL(mandatory=True)
+
+        conf.SAMBA_CHECK_PYTHON(mandatory=True, version=(2,5,0))
+        conf.SAMBA_CHECK_PYTHON_HEADERS(mandatory=True)
+
     if conf.CHECK_FOR_THIRD_PARTY():
         conf.RECURSE('third_party/popt')
     else:
@@ -75,10 +102,40 @@ def configure(conf):
         else:
             conf.define('USING_SYSTEM_POPT', 1)
 
+    conf.RECURSE('lib/util')
+
     conf.RECURSE('lib/talloc')
     conf.RECURSE('lib/tevent')
     conf.RECURSE('lib/tdb')
-    conf.RECURSE('lib/socket_wrapper')
+    if conf.env.standalone_ctdb or conf.CONFIG_GET('ENABLE_SELFTEST'):
+        conf.RECURSE('lib/socket_wrapper')
+
+    conf.CHECK_HEADERS('sched.h')
+    conf.CHECK_HEADERS('procinfo.h')
+    if sys.platform.startswith('aix') and not conf.CHECK_FUNCS('thread_setsched'):
+        Logs.error('Need thread_setsched() on AIX')
+        sys.exit(1)
+    elif not conf.CHECK_FUNCS('sched_setscheduler'):
+        Logs.error('Need sched_setscheduler()')
+        sys.exit(1)
+    conf.CHECK_FUNCS('mlockall')
+
+    if not conf.CHECK_VARIABLE('ETIME', headers='errno.h'):
+        conf.DEFINE('ETIME', 'ETIMEDOUT')
+
+    if sys.platform.startswith('linux'):
+        conf.SET_TARGET_TYPE('pcap', 'EMPTY')
+    else:
+        if not conf.CHECK_HEADERS('pcap.h'):
+            Logs.error('Need libpcap')
+            sys.exit(1)
+        if not conf.CHECK_FUNCS_IN('pcap_open_live', 'pcap', headers='pcap.h'):
+            Logs.error('Need libpcap')
+            sys.exit(1)
+
+    if not conf.CHECK_FUNCS_IN('backtrace backtrace_symbols', 'execinfo',
+                               checklibc=True, headers='execinfo.h'):
+        Logs.error('backtrace support not available')
 
     have_pmda = False
     if Options.options.ctdb_pmda:
@@ -135,16 +192,15 @@ def configure(conf):
     else:
         conf.env.CTDB_SOCKPATH = os.path.join(conf.env.CTDB_RUNDIR,
                                               'ctdbd.socket')
+    conf.define('CTDB_SOCKET', conf.env.CTDB_SOCKPATH)
 
-    conf.ADD_CFLAGS('''-DBINDIR=\"%s\"
+    conf.ADD_CFLAGS('''-DCTDB_HELPER_BINDIR=\"%s\"
                        -DLOGDIR=\"%s\"
-                       -DSOCKPATH=\"%s\"
                        -DCTDB_ETCDIR=\"%s\"
                        -DCTDB_VARDIR=\"%s\"
                        -DCTDB_RUNDIR=\"%s\"''' % (
                     conf.env.CTDB_BINDIR,
                     conf.env.CTDB_LOGDIR,
-                    conf.env.CTDB_SOCKPATH,
                     conf.env.CTDB_ETCDIR,
                     conf.env.CTDB_VARDIR,
                     conf.env.CTDB_RUNDIR))
@@ -153,55 +209,96 @@ def configure(conf):
                                               'share/ctdb-tests')
     conf.env.CTDB_TEST_LIBDIR = os.path.join(conf.env.LIBDIR, 'ctdb-tests')
 
-    # Allow separate compilation of utilities to find includes
-    if srcdir == '.':
-        # Building from tarball
-        conf.ADD_EXTRA_INCLUDES('#include')
-        conf.ADD_EXTRA_INCLUDES('#include/internal')
+    # Allow unified compilation and separate compilation of utilities
+    # to find includes
+    if not conf.env.standalone_ctdb:
+        conf.ADD_EXTRA_INCLUDES('#include/public #ctdb/include #ctdb')
     else:
-        # Building standalone CTDB from within Samba tree
-        conf.ADD_EXTRA_INCLUDES('#ctdb/include')
-        conf.ADD_EXTRA_INCLUDES('#ctdb/include/internal')
-        conf.ADD_EXTRA_INCLUDES('#ctdb')
+        if srcdir == '.':
+            # Building from tarball
+            conf.ADD_EXTRA_INCLUDES('#include')
+        else:
+            # Building standalone CTDB from within Samba tree
+            conf.ADD_EXTRA_INCLUDES('#ctdb/include')
+            conf.ADD_EXTRA_INCLUDES('#ctdb')
+        conf.ADD_EXTRA_INCLUDES('#lib #lib/replace')
+
+        conf.DEFINE('HAVE_CONFIG_H', 1, add_to_cflags=True)
+        conf.DEFINE('SAMBA_UTIL_CORE_ONLY', 1, add_to_cflags=True)
+        conf.SAMBA_CONFIG_H()
 
-    conf.DEFINE('HAVE_CONFIG_H', 1, add_to_cflags=True)
-    conf.SAMBA_CONFIG_H()
+    if 'XSLTPROC_MANPAGES' in conf.env and conf.env['XSLTPROC_MANPAGES']:
+        conf.env.ctdb_generate_manpages = True
+    else:
+        conf.env.ctdb_generate_manpages = False
 
+        Logs.info("xsltproc unavailable, checking for pre-built manpages")
+        conf.env.ctdb_prebuilt_manpages = []
+        for m in manpages:
+            if os.path.exists(os.path.join("doc", m)):
+                Logs.info("  %s: yes" % (m))
+                conf.env.ctdb_prebuilt_manpages.append(m)
+            else:
+                Logs.info("  %s: no" % (m))
 
 def build(bld):
+    if bld.env.standalone_ctdb:
+        # enable building of public headers in the build tree
+        bld.env.build_public_headers = 'include/public'
+
+    version_h = samba_utils.os_path_relpath(os.path.join(Options.launch_dir,
+                                                         "version.h"),
+                                            bld.curdir)
+
+    if bld.env.standalone_ctdb:
+        ctdb_mkversion = '../packaging/mkversion.sh'
+        t = bld.SAMBA_GENERATOR('ctdb-version-header',
+                                target='include/ctdb_version.h',
+                                rule='%s ${TGT} %s' % (ctdb_mkversion, VERSION),
+                                dep_vars=['VERSION'])
+        t.env.VERSION = VERSION
+
+        t = bld.SAMBA_GENERATOR('ctdb-samba-version-header',
+                                target=version_h,
+                                rule='printf "#include \\"ctdb_version.h\\" \\n#define SAMBA_VERSION_STRING CTDB_VERSION_STRING\\n" > ${TGT}',
+                                dep_vars=['VERSION'])
+        t.env.VERSION = VERSION
+    else:
+        t = bld.SAMBA_GENERATOR('ctdb-samba-version-header',
+                                target='include/ctdb_version.h',
+                                rule='printf "#include \\"%s\\" \\n#define CTDB_VERSION_STRING SAMBA_VERSION_STRING\\n" > ${TGT}' % version_h,
+                                dep_vars=['VERSION'])
+        t.env.VERSION = VERSION
+
+    bld.env.PKGCONFIGDIR = '${LIBDIR}/pkgconfig'
+
     bld.RECURSE('lib/replace')
     if bld.CHECK_FOR_THIRD_PARTY():
         bld.RECURSE('third_party/popt')
 
+    bld.RECURSE('lib/tdb_wrap')
+    bld.RECURSE('lib/util')
+
     bld.RECURSE('lib/talloc')
     bld.RECURSE('lib/tevent')
     bld.RECURSE('lib/tdb')
-    bld.RECURSE('lib/socket_wrapper')
-
-    t = bld.SAMBA_GENERATOR('ctdb-version-header',
-                            target='include/ctdb_version.h',
-                            rule='../packaging/mkversion.sh ${TGT} %s' % (VERSION),
-                            dep_vars=['VERSION'])
-    t.env.VERSION = VERSION
-
-    bld.SAMBA_SUBSYSTEM('ctdb-util',
-                        source=bld.SUBDIR('lib/util',
-                                          'util.c util_file.c util_time.c'),
-                        includes='include include/internal',
-                        deps='replace talloc tevent tdb')
-
-    bld.SAMBA_SUBSYSTEM('ctdb-util-misc',
-                        source=bld.SUBDIR('lib/util',
-                                          '''db_wrap.c debug.c fault.c
-                                             idtree.c signal.c
-                                             substitute.c'''),
-                        includes='include include/internal lib/util',
-                        deps='replace talloc tevent tdb')
+    if bld.env.standalone_ctdb or bld.CONFIG_GET('SOCKET_WRAPPER'):
+        bld.RECURSE('lib/socket_wrapper')
+
+    if bld.env.standalone_ctdb:
+        # If a combined build is implemented, CTDB will want to
+        # build against samba-util rather than samba-util-core.
+        # Similarly, other Samba subsystems expect samba-util.  So,
+        # for a standalone build, just define a fake samba-util
+        # subsystem that pulls in samba-util-core.
+        bld.SAMBA_SUBSYSTEM('samba-util',
+                            source='',
+                            deps='samba-util-core')
 
     bld.SAMBA_SUBSYSTEM('ctdb-tcp',
                         source=bld.SUBDIR('tcp',
                                           'tcp_connect.c tcp_init.c tcp_io.c'),
-                        includes='include include/internal',
+                        includes='include',
                         deps='replace tdb talloc tevent')
 
     ib_deps = ''
@@ -210,29 +307,15 @@ def build(bld):
                             source=bld.SUBDIR('ib',
                                               '''ibwrapper.c ibw_ctdb.c
                                                  ibw_ctdb_init.c'''),
-                            includes='include include/internal',
-                            deps='replace')
+                            includes='include',
+                            deps='replace talloc tevent')
         ib_deps = ' ctdb-ib rdmacm ibverbs'
 
-    bld.SAMBA_SUBSYSTEM('ctdb-common',
-                        source=bld.SUBDIR('common',
-                                          '''ctdb_io.c ctdb_util.c ctdb_ltdb.c
-                                             ctdb_message.c cmdline.c rb_tree.c
-                                             system_common.c ctdb_fork.c'''),
-                        includes='include include/internal common . lib/util',
-                        deps='replace popt talloc tevent tdb popt')
-
-    bld.SAMBA_SUBSYSTEM('ctdb-common-util',
-                        source=bld.SUBDIR('common',
-                                          'system_util.c ctdb_logging.c'),
-                        includes='include include/internal',
-                        deps='replace tevent tdb')
-
-    if sys.platform == 'linux2':
+    if sys.platform.startswith('linux'):
         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_linux.c')
-    elif sys.platform == 'aix':
+    elif sys.platform.startswith('aix'):
         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_aix.c')
-    elif sys.platform == 'freebsd':
+    elif sys.platform.startswith('freebsd'):
         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_freebsd.c')
     elif sys.platform == 'kfreebsd':
         CTDB_SYSTEM_SRC = bld.SUBDIR('common', 'system_kfreebsd.c')
@@ -242,15 +325,58 @@ def build(bld):
         Logs.error("Platform %s not supported" % sys.platform)
 
     bld.SAMBA_SUBSYSTEM('ctdb-system',
-                        source=CTDB_SYSTEM_SRC,
-                        includes='include include/internal',
-                        deps='replace talloc tevent tdb')
+                        source='common/system_common.c ' + CTDB_SYSTEM_SRC,
+                        includes='include',
+                        deps='replace talloc tevent tdb pcap')
+
+    bld.SAMBA_SUBSYSTEM('ctdb-common',
+                        source=bld.SUBDIR('common',
+                                          '''ctdb_io.c ctdb_util.c ctdb_ltdb.c
+                                             cmdline.c rb_tree.c
+                                             ctdb_fork.c'''),
+                        includes='include',
+                        deps='replace popt talloc tevent tdb popt ctdb-system')
+
+    bld.SAMBA_SUBSYSTEM('ctdb-common-util',
+                        source=bld.SUBDIR('common',
+                                          'system_util.c ctdb_logging.c'),
+                        includes='include',
+                        deps='replace tevent tdb')
+
+    bld.SAMBA_SUBSYSTEM('ctdb-util',
+                        source=bld.SUBDIR('common',
+                                          '''db_hash.c srvid.c reqid.c
+                                             pkt_read.c pkt_write.c comm.c
+                                             logging.c'''),
+                        deps='replace talloc tevent tdb tevent-unix-util')
+
+    bld.SAMBA_SUBSYSTEM('ctdb-protocol',
+                        source=bld.SUBDIR('protocol',
+                                          '''protocol_header.c protocol_packet.c
+                                             protocol_types.c protocol_call.c
+                                             protocol_message.c
+                                             protocol_control.c
+                                             protocol_client.c
+                                             protocol_util.c'''),
+                        includes='include',
+                        deps='replace talloc tdb')
 
     bld.SAMBA_SUBSYSTEM('ctdb-client',
                         source=bld.SUBDIR('client', 'ctdb_client.c'),
-                        includes='include include/internal lib/util',
-                        public_headers='include/ctdb_client.h',
-                        deps='replace popt talloc tevent tdb ctdb-util ctdb-util-misc')
+                        includes='include',
+                        deps='''replace popt talloc tevent tdb
+                                samba-util tdb-wrap ctdb-util''')
+
+    bld.SAMBA_SUBSYSTEM('ctdb-client2',
+                        source=bld.SUBDIR('client',
+                                          '''client_connect.c client_call.c
+                                             client_message.c client_control.c
+                                             client_message_sync.c
+                                             client_control_sync.c
+                                             client_db.c client_util.c
+                                          '''),
+                        includes='include',
+                        deps='replace talloc tevent tdb tdb-wrap')
 
     bld.SAMBA_SUBSYSTEM('ctdb-server',
                         source='server/ctdbd.c ' +
@@ -263,51 +389,56 @@ def build(bld):
                                              ctdb_traverse.c eventscript.c
                                              ctdb_takeover.c ctdb_serverids.c
                                              ctdb_persistent.c ctdb_keepalive.c
-                                             ctdb_logging.c ctdb_uptime.c
+                                             ctdb_logging.c
+                                             ctdb_logging_syslog.c
+                                             ctdb_logging_file.c
+                                             ctdb_uptime.c
                                              ctdb_vacuum.c ctdb_banning.c
                                              ctdb_statistics.c
                                              ctdb_update_record.c
                                              ctdb_lock.c'''),
-                        includes='include include/internal lib/util',
-                        public_headers='''include/ctdb.h
-                                          include/ctdb_private.h
-                                          include/ctdb_protocol.h
-                                          include/ctdb_typesafe_cb.h''',
-                        deps='replace popt talloc tevent tdb')
+                        includes='include',
+                        deps='replace popt talloc tevent tdb talloc_report')
 
     bld.SAMBA_BINARY('ctdbd',
                      source='',
                      deps='''ctdb-server ctdb-client ctdb-common
-                             ctdb-common-util ctdb-system ctdb-tcp''' +
+                             ctdb-common-util ctdb-tcp ctdb-util''' +
                           ib_deps,
                      install_path='${SBINDIR}',
-                     manpages='doc/ctdbd.1')
+                     manpages='ctdbd.1')
 
     bld.SAMBA_BINARY('ctdb',
-                     source='tools/ctdb.c tools/ctdb_vacuum.c',
+                     source='tools/ctdb.c',
                      deps='''ctdb-client ctdb-common ctdb-common-util
-                             ctdb-system''',
-                     includes='include include/internal',
+                             ctdb-util''',
+                     includes='include',
                      install_path='${BINDIR}',
-                     manpages='doc/ctdb.1')
+                     manpages='ctdb.1')
 
     bld.SAMBA_BINARY('ltdbtool',
                      source='tools/ltdbtool.c',
                      includes='include',
                      deps='tdb',
                      install_path='${BINDIR}',
-                     manpages='doc/ltdbtool.1')
+                     manpages='ltdbtool.1')
 
     bld.SAMBA_BINARY('ctdb_lock_helper',
                      source='server/ctdb_lock_helper.c',
-                     deps='ctdb-util ctdb-util-misc ctdb-common-util talloc tdb',
-                     includes='include include/internal',
+                     deps='samba-util ctdb-common-util talloc tdb',
+                     includes='include',
                      install_path='${BINDIR}')
 
     bld.SAMBA_BINARY('ctdb_event_helper',
                      source='server/ctdb_event_helper.c',
-                     includes='include include/internal',
-                     deps='ctdb-util ctdb-util-misc ctdb-common-util replace tdb',
+                     includes='include',
+                     deps='samba-util ctdb-common-util replace tdb',
+                     install_path='${BINDIR}')
+
+    bld.SAMBA_BINARY('ctdb_recovery_helper',
+                     source='server/ctdb_recovery_helper.c',
+                     deps='''ctdb-client2 ctdb-protocol ctdb-util
+                             samba-util replace tdb''',
                      install_path='${BINDIR}')
 
     bld.SAMBA_GENERATOR('ctdb-smnotify-h',
@@ -338,14 +469,14 @@ def build(bld):
                      source='utils/ping_pong/ping_pong.c',
                      deps='',
                      install_path='${BINDIR}',
-                     manpages='doc/ping_pong.1')
+                     manpages='ping_pong.1')
 
     if bld.env.HAVE_PMDA:
         bld.SAMBA_BINARY('pmdactdb',
                          source='utils/pmda/pmda_ctdb.c',
-                         includes='include include/internal',
-                         deps='''ctdb-client ctdb-common ctdb-system
-                                 pcp_pmda pcp''',
+                         includes='include',
+                         deps='''ctdb-client ctdb-common
+                                 ctdb-common-util pcp_pmda pcp''',
                          install_path='${CTDB_PMDADIR}')
         bld.INSTALL_FILES('${CTDB_PMDADIR}', 'utils/pmda/Install',
                           destname='Install')
@@ -360,14 +491,52 @@ def build(bld):
         bld.INSTALL_FILES('${CTDB_PMDADIR}', 'utils/pmda/README',
                           destname='README')
 
-    bld.MANPAGES('''doc/onnode.1 doc/ctdbd_wrapper.1 doc/ctdbd.conf.5
-                    doc/ctdb.7 doc/ctdb-tunables.7''', True)
-
-    bld.INSTALL_FILES('${BINDIR}', 'tools/onnode',
+    sed_expr1 = 's|/usr/local/var/lib/ctdb|%s|g'  % (bld.env.CTDB_VARDIR)
+    sed_expr2 = 's|/usr/local/etc/ctdb|%s|g'      % (bld.env.CTDB_ETCDIR)
+    sed_expr3 = 's|/usr/local/var/log|%s|g'       % (bld.env.CTDB_LOGDIR)
+    sed_expr4 = 's|/usr/local/var/run/ctdb|%s|g'  % (bld.env.CTDB_RUNDIR)
+    sed_expr5 = 's|/usr/local/sbin|%s|g'          % (bld.env.SBINDIR)
+    sed_cmdline = '-e "%s" -e "%s" -e "%s" -e "%s" -e "%s"' % \
+                       (sed_expr1, sed_expr2, sed_expr3, sed_expr4, sed_expr5)
+
+    for f in manpages:
+        x = '%s.xml' % (f)
+        bld.SAMBA_GENERATOR(x,
+                            source=os.path.join('doc', x),
+                            target=x,
+                            rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
+
+    if bld.env.ctdb_generate_manpages:
+        bld.MANPAGES('''onnode.1 ctdbd_wrapper.1 ctdbd.conf.5
+                        ctdb.7 ctdb-statistics.7 ctdb-tunables.7''',
+                      True)
+    else:
+        for m in bld.env.ctdb_prebuilt_manpages:
+            bld.SAMBA_GENERATOR(m,
+                                source=os.path.join("doc", m),
+                                target=m,
+                                rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
+            bld.INSTALL_FILES('${MANDIR}/man%s' % m[-1], m)
+
+    bld.SAMBA_GENERATOR('ctdb-onnode',
+                        source='tools/onnode',
+                        target='onnode',
+                        rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
+    bld.INSTALL_FILES('${BINDIR}', 'onnode',
                       destname='onnode', chmod=0755)
-    bld.INSTALL_FILES('${BINDIR}', 'tools/ctdb_diagnostics',
+
+    bld.SAMBA_GENERATOR('ctdb-diagnostics',
+                        source='tools/ctdb_diagnostics',
+                        target='ctdb_diagnostics',
+                        rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
+    bld.INSTALL_FILES('${BINDIR}', 'ctdb_diagnostics',
                       destname='ctdb_diagnostics', chmod=0755)
-    bld.INSTALL_FILES('${SBINDIR}', 'config/ctdbd_wrapper',
+
+    bld.SAMBA_GENERATOR('ctdbd-wrapper',
+                        source='config/ctdbd_wrapper',
+                        target='ctdbd_wrapper',
+                        rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
+    bld.INSTALL_FILES('${SBINDIR}', 'ctdbd_wrapper',
                       destname='ctdbd_wrapper', chmod=0755)
 
     def SUBDIR_MODE_callback(arg, dirname, fnames):
@@ -377,7 +546,7 @@ def build(bld):
                 continue
             mode = os.lstat(fl).st_mode & 0777
             if arg['trim_path']:
-                    fl = samba_utils.os_path_relpath(fl, arg['trim_path'])
+                fl = samba_utils.os_path_relpath(fl, arg['trim_path'])
             arg['file_list'].append([fl, mode])
 
     def SUBDIR_MODE(path, trim_path=None):
@@ -387,32 +556,45 @@ def build(bld):
 
     etc_subdirs = [
         'events.d',
-        'nfs-rpc-checks.d'
+        'nfs-checks.d'
     ]
 
+    if bld.env.standalone_ctdb:
+        configdir = 'config'
+    else:
+        configdir = 'ctdb/config'
+
     for t in etc_subdirs:
-        files = SUBDIR_MODE('config/%s' % t, trim_path='config')
+        files = SUBDIR_MODE('%s/%s' % (configdir, t), trim_path=configdir)
         for fmode in files:
             bld.INSTALL_FILES(bld.env.CTDB_ETCDIR, 'config/%s' % fmode[0],
                               destname=fmode[0], chmod=fmode[1])
 
-    bld.INSTALL_FILES(bld.env.CTDB_ETCDIR, 'config/functions',
-                      destname='functions')
+    bld.SAMBA_GENERATOR('ctdb-functions',
+                        source='config/functions',
+                        target='functions',
+                        rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
+    bld.INSTALL_FILES(bld.env.CTDB_ETCDIR, 'functions', destname='functions')
 
     etc_scripts = [
         'ctdb-crash-cleanup.sh',
         'debug-hung-script.sh',
         'debug_locks.sh',
         'gcore_trace.sh',
+        'nfs-linux-kernel-callout',
         'notify.sh',
         'statd-callout'
     ]
 
     for t in etc_scripts:
-        bld.INSTALL_FILES(bld.env.CTDB_ETCDIR, 'config/' + t,
+        bld.INSTALL_FILES(bld.env.CTDB_ETCDIR, 'config/%s' % t,
                           destname=t, chmod=0755)
 
-    bld.INSTALL_FILES('${SYSCONFDIR}/sudoers.d', 'config/ctdb.sudoers',
+    bld.SAMBA_GENERATOR('ctdb-sudoers',
+                        source='config/ctdb.sudoers',
+                        target='ctdb.sudoers',
+                        rule='sed %s ${SRC} > ${TGT}' % (sed_cmdline))
+    bld.INSTALL_FILES('${SYSCONFDIR}/sudoers.d', 'ctdb.sudoers',
                       destname='ctdb')
 
     bld.INSTALL_FILES('${CTDB_ETCDIR}/notify.d', 'config/notify.d.README',
@@ -431,6 +613,32 @@ def build(bld):
     t.env.VERSION = VERSION
     bld.INSTALL_FILES('${LIBDIR}/pkgconfig', 'ctdb.pc')
 
+    # Unit tests
+    ctdb_unit_tests = [
+        'db_hash_test',
+        'srvid_test',
+        'pkt_read_test',
+        'pkt_write_test',
+        'comm_test',
+        'comm_server_test',
+        'comm_client_test',
+        'protocol_types_test',
+        'protocol_client_test',
+    ]
+
+    for target in ctdb_unit_tests:
+        src = 'tests/src/' + target + '.c'
+
+        bld.SAMBA_BINARY(target,
+                         source=src,
+                         deps='talloc tevent tdb tevent-unix-util',
+                         install_path='${CTDB_TEST_LIBDIR}')
+
+    bld.SAMBA_BINARY('reqid_test',
+                     source='tests/src/reqid_test.c',
+                     deps='samba-util',
+                     install_path='${CTDB_TEST_LIBDIR}')
+
     # Test binaries
     ctdb_tests = [
         'rb_test',
@@ -456,41 +664,45 @@ def build(bld):
 
         bld.SAMBA_BINARY(target,
                          source=src,
-                         includes='include include/internal',
+                         includes='include',
                          deps='''ctdb-client ctdb-common ctdb-common-util
-                                 ctdb-system''',
+                                 ctdb-util''',
                          install_path='${CTDB_TEST_LIBDIR}')
 
     bld.SAMBA_BINARY('ctdb_takeover_tests',
                      source='tests/src/ctdb_takeover_tests.c',
-                     deps='replace popt tdb tevent talloc ctdb-system' +
+                     deps='''replace popt tdb tevent talloc ctdb-system
+                             samba-util tdb-wrap talloc_report''' +
                           ib_deps,
-                     includes='include include/internal lib/util',
+                     includes='include',
                      install_path='${CTDB_TEST_LIBDIR}')
 
     bld.SAMBA_BINARY('ctdb_functest',
                      source='tests/src/ctdb_functest.c',
-                     deps='replace tdb tevent talloc popt ctdb-system',
-                     includes='include include/internal lib/util',
+                     deps='''replace tdb tevent talloc popt ctdb-system
+                             samba-util tdb-wrap''',
+                     includes='include',
                      install_path='${CTDB_TEST_LIBDIR}')
 
     bld.SAMBA_BINARY('ctdb_stubtest',
                      source='tests/src/ctdb_test.c',
-                     deps='replace tdb tevent talloc popt ctdb-system',
-                     includes='include include/internal lib/util',
+                     deps='''replace tdb tevent talloc popt ctdb-system
+                             samba-util tdb-wrap''',
+                     includes='include',
                      install_path='${CTDB_TEST_LIBDIR}')
 
     if bld.env.HAVE_INFINIBAND:
         bld.SAMBA_BINARY('ibwrapper_test',
                          source='ib/ibwrapper_test.c',
-                         includes='include include/internal',
+                         includes='include',
                          deps='''replace talloc ctdb-client ctdb-common
-                                 ctdb-system''' +
+                                 ctdb-common-util''' +
                               ib_deps,
                          install_path='${CTDB_TEST_LIBDIR}')
 
     test_subdirs = [
         'complex',
+        'cunit',
         'events.d',
         'eventscripts',
         'onnode',
@@ -542,7 +754,9 @@ def build(bld):
     test_eventscript_links = [
         'events.d',
         'functions',
-        'nfs-rpc-checks.d'
+        'nfs-checks.d',
+        'nfs-linux-kernel-callout',
+        'statd-callout'
     ]
 
     test_link_dir = os.path.join(bld.env.CTDB_TEST_DATADIR,
@@ -551,6 +765,17 @@ def build(bld):
         bld.symlink_as(os.path.join(test_link_dir, t),
                        os.path.join(bld.env.CTDB_ETCDIR, t))
 
+    # Tests that use onnode need to overwrite link to in-tree
+    # functions file when installed
+    bld.symlink_as(os.path.join(bld.env.CTDB_TEST_DATADIR, 'onnode/functions'),
+                   os.path.join(bld.env.CTDB_ETCDIR, 'functions'))
+    bld.symlink_as(os.path.join(bld.env.CTDB_TEST_DATADIR, 'simple/functions'),
+                   os.path.join(bld.env.CTDB_ETCDIR, 'functions'))
+
+    # Need a link to nodes file because $CTDB_BASE is overridden
+    bld.symlink_as(os.path.join(bld.env.CTDB_TEST_DATADIR, 'simple/nodes'),
+                   os.path.join(bld.env.CTDB_ETCDIR, 'nodes'))
+
 
 def testonly(ctx):
     cmd = 'tests/run_tests.sh -V tests/var'
@@ -567,7 +792,8 @@ def test(ctx):
 
 
 def autotest(ctx):
-    cmd = 'LD_PRELOAD=bin/shared/libsocket-wrapper.so tests/run_tests.sh -e -S -C'
+    ld = 'LD_PRELOAD=%s/bin/shared/libsocket-wrapper.so' % os.getcwd()
+    cmd = '%s tests/run_tests.sh -e -S -C' % ld
     ret = samba_utils.RUN_COMMAND(cmd)
     if ret != 0:
         print('autotest exited with exit status %d' % ret)
@@ -600,16 +826,6 @@ def dist():
         sys.exit(ret)
     samba_dist.DIST_FILES('ctdb/%s:%s' % (t, t), extend=True)
 
-    manpages = [
-        'ctdb.1',
-        'ctdb.7',
-        'ctdbd.1',
-        'ctdbd.conf.5',
-        'ctdbd_wrapper.1',
-        'ctdb-tunables.7',
-        'ltdbtool.1'
-    ]
-
     cmd = 'make -C doc'
     ret = samba_utils.RUN_COMMAND(cmd)
     if ret != 0:
@@ -624,7 +840,8 @@ def dist():
 
 
 def rpmonly(ctx):
-    cmd = 'rpmbuild -ta --clean --rmsource ctdb-%s.tar.gz' % VERSION
+    opts = os.getenv('RPM_OPTIONS') or ''
+    cmd = 'rpmbuild -ta --clean --rmsource %s ctdb-%s.tar.gz' % (opts, VERSION)
     ret = samba_utils.RUN_COMMAND(cmd)
     if ret != 0:
         print('rpmbuild exited with exit status %d' % ret)