MAOS
Multithreaded Adaptive Optics Simulator
daemonize.h File Reference

Macros

#define CACHE_FILE(var, fn_cache, f_read, f_create, f_write)
 

Functions

int single_instance_daemonize (const char *lockfolder_in, const char *progname, int version, void(*daemon_func)(void *), void *daemon_arg)
 
int lock_file (const char *fn, int block)
 
int lock_file_version (const char *fn, int block, int version)
 
void daemonize (void)
 
void redirect (void)
 
pid_t launch_exe (const char *exepath, const char *cmd)
 
char * find_exe (const char *name)
 
int spawn_process (const char *exename, const char *arg, const char *path)
 

Detailed Description

Handling process creation.

Function Documentation

◆ single_instance_daemonize()

int single_instance_daemonize ( const char *  lockfolder_in,
const char *  progname,
int  version,
void(*)(void *)  daemon_func,
void *  daemon_arg 
)

Launch a singleton daemon. First try to create a lock file progname.pid in folder lockfolder_in. If lock failes, it means other daemon already exists, will not proceed. If lock succeed, will write pid and version in the file.

the stdin and stderr stream will then be directed to progname.log in folder lockfolder_in. buffering is disabled to ensure real time display by tail.

if daemon_func is true, will fork and run it with argument daemon_arg. otherwise the routine returns.

◆ lock_file()

int lock_file ( const char *  fn,
int  block 
)
Parameters
fnThe filename to lock on
blockblock on waiting. set to 0 for no waiting.

◆ lock_file_version()

int lock_file_version ( const char *  fnlock,
int  block,
int  version 
)

Ensure exclusive access by opening and maintaining lock of file fn.

Will proceed if the lock if succeed, in which case non-negative number of the fd is returned. If other process already locked, will return negative number. If version is specified and is bigger than the value contained in existing fnlock, will kill the old process that created fnlock.

returns: -PID (<-1): if a running process already locked file -1: failed to lock fd (>-1): successfully locked

Parameters
fnlockThe filename to lock on
blockblock on waiting. set to 0 for no waiting.
versionThe version of the software that locks the file, primarily for managing scheduler. May be zero.

◆ daemonize()

void daemonize ( void  )

Daemonize a process by fork it and exit the parent. no need to fork twice since the parent exits.

◆ redirect()

void redirect ( void  )

Redirect output. If we are in detached mode, will output to file, otherwise will output to both file and screen.

◆ launch_exe()

pid_t launch_exe ( const char *  exepath,
const char *  cmd 
)

fork and launch exe as specified in cmd. cmd should composed of the path to start the exe, exe name, and parameters. used by maos/skyc.

if cwd is NULL, cmd must include path information. if cwd is not NULL cmd must only contain arguments, without path information.

◆ find_exe()

char* find_exe ( const char *  name)

Find an executable from predetermined locations and return the absolute path.

◆ spawn_process()

int spawn_process ( const char *  exename,
const char *  args,
const char *  path 
)

fork twice and launch exename, with arguments args. Returns PID of the grandchild process.