0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2025-01-09 18:41:55 +00:00

Birdtest: Purge 'int main()' from lib/birdlib.a #2

All origin static functions from sysdep/unix/main.c were rewrited to
non-static.

All origin static global variables from sysdep/unix/main.c was rewrited
to extern.

It is possible create executables bird, birdc and birdcl as well as run
unit tests without mockuping

After remake new BIRD's build system will be this commit reverting...
This commit is contained in:
Pavel Tvrdík 2015-08-05 11:42:40 +02:00
parent ce8d15a02d
commit 856250c27f
5 changed files with 147 additions and 40 deletions

View File

@ -1,4 +1,7 @@
#ifndef _BIRD_SYSPRIV_H_
#define _BIRD_SYSPRIV_H_
#include <unistd.h>
#include <sys/prctl.h> #include <sys/prctl.h>
#include <linux/capability.h> #include <linux/capability.h>
@ -70,3 +73,5 @@ drop_uid(uid_t uid)
if (setresuid(uid, uid, uid) < 0) if (setresuid(uid, uid, uid) < 0)
die("setresuid: %m"); die("setresuid: %m");
} }
#endif /* _BIRD_SYSPRIV_H_ */

View File

@ -1,5 +1,6 @@
log.c log.c
main_helper.c main_helper.c
main_helper.h
timer.h timer.h
io.c io.c
unix.h unix.h

View File

@ -6,7 +6,39 @@
* Can be freely distributed and used under the terms of the GNU GPL. * Can be freely distributed and used under the terms of the GNU GPL.
*/ */
#include "lib/main_helper.c" #ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
#include <libgen.h>
#include "nest/bird.h"
#include "lib/lists.h"
#include "lib/resource.h"
#include "lib/socket.h"
#include "lib/event.h"
#include "lib/string.h"
#include "nest/route.h"
#include "nest/protocol.h"
#include "nest/iface.h"
#include "nest/cli.h"
#include "nest/locks.h"
#include "conf/conf.h"
#include "filter/filter.h"
#include "unix.h"
#include "krt.h"
#include "lib/main_helper.h"
/* /*
* Hic Est main() * Hic Est main()

View File

@ -8,7 +8,9 @@
#undef LOCAL_DEBUG #undef LOCAL_DEBUG
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1 #define _GNU_SOURCE 1
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -42,9 +44,9 @@
*/ */
#ifdef DEBUGGING #ifdef DEBUGGING
static int debug_flag = 1; int debug_flag = 1;
#else #else
static int debug_flag = 0; int debug_flag = 0;
#endif #endif
void void
@ -72,7 +74,7 @@ async_dump(void)
#include "lib/syspriv.h" #include "lib/syspriv.h"
#else #else
static inline void void
drop_uid(uid_t uid) drop_uid(uid_t uid)
{ {
die("Cannot change user on this platform"); die("Cannot change user on this platform");
@ -80,7 +82,7 @@ drop_uid(uid_t uid)
#endif #endif
static inline void void
drop_gid(gid_t gid) drop_gid(gid_t gid)
{ {
if (setgid(gid) < 0) if (setgid(gid) < 0)
@ -93,7 +95,7 @@ drop_gid(gid_t gid)
#ifdef PATH_IPROUTE_DIR #ifdef PATH_IPROUTE_DIR
static inline void void
add_num_const(char *name, int val) add_num_const(char *name, int val)
{ {
struct symbol *s = cf_find_symbol(name); struct symbol *s = cf_find_symbol(name);
@ -105,7 +107,7 @@ add_num_const(char *name, int val)
/* the code of read_iproute_table() is based on /* the code of read_iproute_table() is based on
rtnl_tab_initialize() from iproute2 package */ rtnl_tab_initialize() from iproute2 package */
static void void
read_iproute_table(char *file, char *prefix, int max) read_iproute_table(char *file, char *prefix, int max)
{ {
char buf[512], namebuf[512]; char buf[512], namebuf[512];
@ -152,9 +154,9 @@ read_iproute_table(char *file, char *prefix, int max)
#endif // PATH_IPROUTE_DIR #endif // PATH_IPROUTE_DIR
static char *config_name = PATH_CONFIG_FILE; char *config_name = PATH_CONFIG_FILE;
static int int
cf_read(byte *dest, uint len, int fd) cf_read(byte *dest, uint len, int fd)
{ {
int l = read(fd, dest, len); int l = read(fd, dest, len);
@ -186,7 +188,7 @@ sysdep_commit(struct config *new, struct config *old UNUSED)
return 0; return 0;
} }
static int int
unix_read_config(struct config **cp, char *name) unix_read_config(struct config **cp, char *name)
{ {
struct config *conf = config_alloc(name); struct config *conf = config_alloc(name);
@ -202,7 +204,7 @@ unix_read_config(struct config **cp, char *name)
return ret; return ret;
} }
static struct config * struct config *
read_config(void) read_config(void)
{ {
struct config *conf; struct config *conf;
@ -236,7 +238,7 @@ async_config(void)
config_commit(conf, RECONFIG_HARD, 0); config_commit(conf, RECONFIG_HARD, 0);
} }
static struct config * struct config *
cmd_read_config(char *name) cmd_read_config(char *name)
{ {
struct config *conf; struct config *conf;
@ -269,7 +271,7 @@ cmd_check_config(char *name)
config_free(conf); config_free(conf);
} }
static void void
cmd_reconfig_msg(int r) cmd_reconfig_msg(int r)
{ {
switch (r) switch (r)
@ -346,11 +348,11 @@ cmd_reconfig_undo(void)
* Command-Line Interface * Command-Line Interface
*/ */
static sock *cli_sk; sock *cli_sk;
static char *path_control_socket = PATH_CONTROL_SOCKET; char *path_control_socket = PATH_CONTROL_SOCKET;
static void void
cli_write(cli *c) cli_write(cli *c)
{ {
sock *s = c->priv; sock *s = c->priv;
@ -383,7 +385,7 @@ cli_write_trigger(cli *c)
cli_write(c); cli_write(c);
} }
static void void
cli_tx(sock *s) cli_tx(sock *s)
{ {
cli_write(s->data); cli_write(s->data);
@ -418,14 +420,14 @@ cli_get_command(cli *c)
return 0; return 0;
} }
static int int
cli_rx(sock *s, int size UNUSED) cli_rx(sock *s, int size UNUSED)
{ {
cli_kick(s->data); cli_kick(s->data);
return 0; return 0;
} }
static void void
cli_err(sock *s, int err) cli_err(sock *s, int err)
{ {
if (config->cli_debug) if (config->cli_debug)
@ -438,7 +440,7 @@ cli_err(sock *s, int err)
cli_free(s->data); cli_free(s->data);
} }
static int int
cli_connect(sock *s, int size UNUSED) cli_connect(sock *s, int size UNUSED)
{ {
cli *c; cli *c;
@ -456,7 +458,7 @@ cli_connect(sock *s, int size UNUSED)
return 1; return 1;
} }
static void void
cli_init_unix(uid_t use_uid, gid_t use_gid) cli_init_unix(uid_t use_uid, gid_t use_gid)
{ {
sock *s; sock *s;
@ -485,10 +487,10 @@ cli_init_unix(uid_t use_uid, gid_t use_gid)
* PID file * PID file
*/ */
static char *pid_file; char *pid_file;
static int pid_fd; int pid_fd;
static inline void void
open_pid_file(void) open_pid_file(void)
{ {
if (!pid_file) if (!pid_file)
@ -499,7 +501,7 @@ open_pid_file(void)
die("Cannot create PID file %s: %m", pid_file); die("Cannot create PID file %s: %m", pid_file);
} }
static inline void void
write_pid_file(void) write_pid_file(void)
{ {
int pl, rv; int pl, rv;
@ -525,7 +527,7 @@ write_pid_file(void)
close(pid_fd); close(pid_fd);
} }
static inline void void
unlink_pid_file(void) unlink_pid_file(void)
{ {
if (pid_file) if (pid_file)
@ -567,21 +569,21 @@ sysdep_shutdown_done(void)
* Signals * Signals
*/ */
static void void
handle_sighup(int sig UNUSED) handle_sighup(int sig UNUSED)
{ {
DBG("Caught SIGHUP...\n"); DBG("Caught SIGHUP...\n");
async_config_flag = 1; async_config_flag = 1;
} }
static void void
handle_sigusr(int sig UNUSED) handle_sigusr(int sig UNUSED)
{ {
DBG("Caught SIGUSR...\n"); DBG("Caught SIGUSR...\n");
async_dump_flag = 1; async_dump_flag = 1;
} }
static void void
handle_sigterm(int sig UNUSED) handle_sigterm(int sig UNUSED)
{ {
DBG("Caught SIGTERM...\n"); DBG("Caught SIGTERM...\n");
@ -590,7 +592,7 @@ handle_sigterm(int sig UNUSED)
void watchdog_sigalrm(int sig UNUSED); void watchdog_sigalrm(int sig UNUSED);
static void void
signal_init(void) signal_init(void)
{ {
struct sigaction sa; struct sigaction sa;
@ -615,21 +617,21 @@ signal_init(void)
* Parsing of command-line arguments * Parsing of command-line arguments
*/ */
static char *opt_list = "c:dD:ps:P:u:g:fR"; char *opt_list = "c:dD:ps:P:u:g:fR";
static int parse_and_exit; int parse_and_exit;
char *bird_name; char *bird_name;
static char *use_user; char *use_user;
static char *use_group; char *use_group;
static int run_in_foreground = 0; int run_in_foreground = 0;
static void void
usage(void) usage(void)
{ {
fprintf(stderr, "Usage: %s [-c <config-file>] [-d] [-D <debug-file>] [-p] [-s <control-socket>] [-P <pid-file>] [-u <user>] [-g <group>] [-f] [-R]\n", bird_name); fprintf(stderr, "Usage: %s [-c <config-file>] [-d] [-D <debug-file>] [-p] [-s <control-socket>] [-P <pid-file>] [-u <user>] [-g <group>] [-f] [-R]\n", bird_name);
exit(1); exit(1);
} }
static inline char * char *
get_bird_name(char *s, char *def) get_bird_name(char *s, char *def)
{ {
char *t; char *t;
@ -643,7 +645,7 @@ get_bird_name(char *s, char *def)
return t+1; return t+1;
} }
static inline uid_t uid_t
get_uid(const char *s) get_uid(const char *s)
{ {
struct passwd *pw; struct passwd *pw;
@ -666,7 +668,7 @@ get_uid(const char *s)
return pw->pw_uid; return pw->pw_uid;
} }
static inline gid_t gid_t
get_gid(const char *s) get_gid(const char *s)
{ {
struct group *gr; struct group *gr;
@ -689,7 +691,7 @@ get_gid(const char *s)
return gr->gr_gid; return gr->gr_gid;
} }
static void void
parse_args(int argc, char **argv) parse_args(int argc, char **argv)
{ {
int c; int c;

67
sysdep/unix/main_helper.h Normal file
View File

@ -0,0 +1,67 @@
/*
* BIRD Internet Routing Daemon -- Helper for main.c
*
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#ifndef _BIRD_MAIN_HELPER_H_
#define _BIRD_MAIN_HELPER_H_
/*
* Global variables
*/
extern int debug_flag;
extern char *config_name;
extern int run_in_foreground;
extern char *path_control_socket;
extern char *opt_list;
extern sock *cli_sk;
extern char *pid_file;
extern int pid_fd;
extern int parse_and_exit;
extern char *bird_name;
extern char *use_user;
extern char *use_group;
/*
* Origin 'static' functions from main.c
*/
void async_dump(void);
void drop_gid(gid_t gid);
void add_num_const(char *name, int val);
void read_iproute_table(char *file, char *prefix, int max);
int cf_read(byte *dest, uint len, int fd);
int unix_read_config(struct config **cp, char *name);
struct config * read_config(void);
struct config * cmd_read_config(char *name);
void cmd_reconfig_msg(int r);
void cli_write(cli *c);
void cli_tx(sock *s);
int cli_rx(sock *s, int size UNUSED);
void cli_err(sock *s, int err);
int cli_connect(sock *s, int size UNUSED);
void cli_init_unix(uid_t use_uid, gid_t use_gid);
void open_pid_file(void);
void write_pid_file(void);
void unlink_pid_file(void);
void handle_sighup(int sig UNUSED);
void handle_sigusr(int sig UNUSED);
void handle_sigterm(int sig UNUSED);
void signal_init(void);
void usage(void);
char * get_bird_name(char *s, char *def);
uid_t get_uid(const char *s);
gid_t get_gid(const char *s);
void parse_args(int argc, char **argv);
#ifdef CONFIG_RESTRICTED_PRIVILEGES
#include "lib/syspriv.h"
#else
void drop_uid(uid_t uid);
#endif
#endif /* _BIRD_MAIN_HELPER_H_ */