|
From: Anders J. <and...@us...> - 2003-10-21 10:09:46
|
The following file was modified in apps/bluetooth/experimental:
Name Old version New version Tag Comment
---- ----------- ----------- --- -------
btinit.c 1.25 1.26=20=20=20=20=20=20=20=20=20=20=20=20=20=20
The accompanying log:
bti is now a daemon as default. Override with -n.=20
The diff of the modified file(s):
--- btinit.c 2002/08/19 15:18:57 1.25
+++ btinit.c 2003/10/21 09:15:56 1.26
@@ -129,10 +129,11 @@ static struct option long_options[] =3D
{ "noflow", 1, NULL, 'f' }, /* do not use flow control */
{ "initial-speed", 1, NULL, 'i' }, /* initial uart speed */
{ "physdev", 1, NULL, 'u' }, /* phys device used from stack */
- { "local-name", 1, NULL, 'n' }, /* set local bluetooth name */
+ { "local-name", 1, NULL, 'l' }, /* set local bluetooth name */
{ "reset", 0, NULL, 'R' }, /* reset BT HW */
{ "speed", 1, NULL, 's' }, /* uart speed towards hw */
{ "use-dtr", 1, NULL, 'D' }, /* user dtr for hard reset */
+ { "nodaemon", 0, NULL, 'n' }, /* don't daemonize */
{ 0, 0, 0, 0 }
};
=20
@@ -143,6 +144,7 @@ static void init_sighandler(void);
static void btd_cleanup(void);
static void btd_killchilds(void);
static void sighandler(int sig);
+static void daemonize(void);
=20
=20
void unreset_force_rts()
@@ -188,21 +190,20 @@ main(int argc, char **argv)
int opt;
int bt_disc =3D N_BT;
volatile int dtr_hard_reset =3D 0;
+ int daemon =3D 1;
=20
syslog(LOG_INFO, "Bluetooth daemon starting");
=20
- if (atexit(btd_cleanup) < 0)
- {
- printf("btd failed to register cleanup function.\n");
- exit(1);
- }
-
/* now parse options */
- while ((opt =3D getopt_long(argc, argv, "fi:n:Rs:u:D",
+ while ((opt =3D getopt_long(argc, argv, "fi:l:Rs:u:Dn",
long_options, &option_index)) !=3D -1)
{
switch(opt)
{
+ case 'n':
+ daemon =3D 0;
+ break;
+=20=20=20=20=20=20
case 'f':
/* do not use flow control */
flow_control =3D USE_NO_FLOW;
@@ -215,7 +216,7 @@ main(int argc, char **argv)
D(syslog(LOG_INFO, "init_hw_speed %s baud", init_hw_speedstr));
break;
=20
- case 'n':
+ case 'l':
local_name =3D optarg;
D(syslog(LOG_INFO, "setting local name to %s", local_name));
break;
@@ -300,11 +301,51 @@ main(int argc, char **argv)
init_hw(bt_cfd, phys_fd, speedstr);
=20=20=20
/* All initialized and ready to accept connections in other BT apps */
+ if(daemon)
+ {
+ daemonize();
+ }
+
+ if (atexit(btd_cleanup) < 0)
+ {
+ printf("btd failed to register cleanup function.\n");
+ exit(1);
+ }
+ init_sighandler();
=20
while (1)
sleep(100);
} /* main */
=20
+static void daemonize(void) /* go to background */
+{
+ switch (fork())
+ {
+ case -1: /* fork failed */
+ fprintf(stderr,"fork() failed!\n");
+ syslog(LOG_ERR, "Exit after failure to fork. %m");
+ exit(EXIT_FAILURE);
+ case 0: /* child continues */
+ if (chdir("/"))
+ {
+ syslog(LOG_ERR, "Failed changing working directory to '/': %m");
+ exit(EXIT_FAILURE);
+ }
+ break;
+ default: /* parent quits */
+ exit(EXIT_SUCCESS);
+ }
+ if (setsid() =3D=3D -1)
+ {
+ fprintf(stderr,"setsid() failed!\n");
+ syslog(LOG_ERR, "Exit after failed call to setsid().");
+ exit(EXIT_FAILURE);
+ }
+ fclose(stdin);
+ fclose(stdout);
+ fclose(stderr);
+}
+
/* =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D */
/* Signal handler */
=20
@@ -345,7 +386,6 @@ sighandler(int sig)
static void init()
{=20
btd_pid =3D write_pidfile(PID_FILE);=20=20
- init_sighandler();
=20=20=20
if ((sdpsrv_pid =3D start_sdp_server()) < 0)
{
|