Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backgrounding support #37

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ Stop ntpd if it is running, and then::

That should synchronize your clock to those servers.

Because this is a preview release, the process will not "daemonize"
into the background.

The '-t /tmp/somefile' arguments tells it to write a full blow-by-blow
tracefile, for analysis and debugging.

Expand All @@ -114,8 +111,8 @@ What happens when you run it ?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

After a few seconds, your clock will be stepped if necessary.
(In the final version, this is where the process daemonizes into the
background -- from which point you can trust your clock to be good.)
Then the program will get into the background you can trust
your clock to be good.

In the next 30-60 seconds, the PLL will eliminate any residual phase
error and from this point in time, your computer's clock should be
Expand Down
1 change: 1 addition & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ SRCS='
param.c
pll_std.c
suckaddr.c
sys_unix.c
time_sim.c
time_stuff.c
time_unix.c
Expand Down
1 change: 1 addition & 0 deletions main_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ main_client(int argc, char *const *argv)

tdl = TODO_NewList();
Time_Unix(tdl);
Background_Unix_Init();

PLL_Init();

Expand Down
6 changes: 6 additions & 0 deletions ntimed.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ void PLL_Init(void);

int SA_Equal(const void *sa1, size_t sl1, const void *sa2, size_t sl2);

/* sys_*.c */

typedef void sb_background_f(struct ocx*);
extern sb_background_f *SB_Background;
void Background_Unix_Init(void);

/* time_sim.c -- Simulated timebase ***********************************/

extern double Time_Sim_delta;
Expand Down
8 changes: 8 additions & 0 deletions pll_std.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ pll_std(struct ocx *ocx, double offset, double weight)
pll_b = pll_a / param_pll_std_i_init;
pll_t0 = t0;
pll_mode = 3;
/*
* Put process in the background if the client
* configured that.
*/
if (SB_Background != NULL) {
SB_Background(ocx);
}
}
break;

Expand Down Expand Up @@ -133,6 +140,7 @@ pll_std(struct ocx *ocx, double offset, double weight)
p_term = -offset * used_a;
pll_integrator += p_term * used_b;
dur = dt;

break;
default:
WRONG("Wrong PLL state");
Expand Down
59 changes: 59 additions & 0 deletions sys_unix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2017 Rune Magnussen
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* UNIX-specific stuff
* ====================
* Function to put the calling process into the background.
*/
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "ntimed.h"

sb_background_f *SB_Background = NULL;

static int bg = 0;

static void __match_proto__(sb_background_f)
unix_background(struct ocx *ocx)
{
AZ(bg);

if (daemon(1, 1)) {
Fail(ocx, errno, "daemon() failed\n");
}

bg++;
Debug(ocx, "BACKGROUND\n");
}

/*
* Register backgrounding function.
* Programs calling this may end up in the background.
*/
void
Background_Unix_Init(void)
{
SB_Background = unix_background;
}