The Assimilation Monitoring Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
misc.c File Reference

Miscellaneous library functions - doing varied interesting things. More...

#include <projectcommon.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <signal.h>
#include <misc.h>
Include dependency graph for misc.c:

Go to the source code of this file.

Macros

#define SEP   '/'
 
#define PROCSELFEXE   "/proc/self/exe"
 
#define PROCOTHEREXE   "/proc/%d/exe"
 
#define MAXPIDLEN   16
 
#define MAXPATH   256
 
#define GETPID   getpid()
 

Functions

void assimilation_logger (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
 
FSTATIC void catch_pid_signal (int signum)
 Should be overridden with the name to appear in the logs. More...
 
void daemonize_me (gboolean stay_in_foreground, const char *dirtorunin, char *pidfile)
 Function to get system name (uname -n in UNIX terms) More...
 
void assimilation_openlog (const char *logname)
 Open logs in our style (syslog) More...
 
PidRunningStat are_we_already_running (const char *pidfile, int *pidarg)
 See if the pid file suggests we are already running or not. More...
 
gboolean create_pid_file (const char *pidfile)
 Create a pid file for the current process. More...
 
char * get_default_pid_fileName (const char *procname)
 get default pid file name More...
 
void remove_pid_file (const char *pidfile)
 Remove the pid file that goes with this service iff we created one during this invocation. More...
 
int kill_pid_service (const char *pidfile, int signal)
 kill the service that goes with our current pid file - return negative iff pidfile pid is running and kill fails More...
 
void rmpid_and_exit_on_signal (const char *pidfile, int signal_in)
 Remove PID file and exit when a signal is received. More...
 
guint pidrunningstat_to_status (PidRunningStat stat)
 Convert PidRunningStat to an exit code for status. More...
 
WINEXPORT gchar ** assim_merge_environ (const gchar *const *env, ConfigContext *update)
 Merge ConfigContext into possibly NULL current environment, returning a new environment. More...
 
WINEXPORT void assim_free_environ (gchar **env)
 Free the result of assim_merge_env. More...
 

Variables

const char * assim_syslogid = "assim"
 

Detailed Description

Miscellaneous library functions - doing varied interesting things.

These include:

  • daemonize_me - do the things to make this process into a daemon

This file is part of the Assimilation Project.

Author
Copyright © 2012 - Alan Robertson alanr.nosp@m.@uni.nosp@m.x.sh
The Assimilation software is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

The Assimilation software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with the Assimilation Project software. If not, see http://www.gnu.org/licenses/

Definition in file misc.c.

Macro Definition Documentation

#define GETPID   getpid()

Referenced by create_pid_file().

#define MAXPATH   256

Definition at line 287 of file misc.c.

Referenced by are_we_already_running().

#define MAXPIDLEN   16

Definition at line 286 of file misc.c.

#define PROCOTHEREXE   "/proc/%d/exe"

Definition at line 284 of file misc.c.

Referenced by are_we_already_running().

#define PROCSELFEXE   "/proc/self/exe"

Definition at line 283 of file misc.c.

Referenced by are_we_already_running().

#define SEP   '/'

Definition at line 282 of file misc.c.

Referenced by are_we_already_running().

Function Documentation

PidRunningStat are_we_already_running ( const char *  pidfile,
int *  pidarg 
)

See if the pid file suggests we are already running or not.

Determine service status.

Parameters
pidfileThe pathname of our expected pid file
pidargPid of the process (if running)

Definition at line 293 of file misc.c.

References __FUNCTION__, MAXPATH, PID_DEAD, PID_NOTRUNNING, PID_NOTUS, PID_RUNNING, PROCOTHEREXE, PROCSELFEXE, and SEP.

Referenced by create_pid_file(), daemonize_me(), kill_pid_service(), and main().

Here is the caller graph for this function:

WINEXPORT void assim_free_environ ( gchar **  env)

Free the result of assim_merge_env.

Free environment created by assim_merge_environ.

Parameters
envThe result of assim_merge_environ – to be freed

Definition at line 649 of file misc.c.

Referenced by childprocess_new().

Here is the caller graph for this function:

WINEXPORT gchar** assim_merge_environ ( const gchar *const *  env,
ConfigContext update 
)

Merge ConfigContext into possibly NULL current environment, returning a new environment.

Create new env merged from ConfigContext.

Parameters
envInitial environment – or NULL
updateUpdates to merge into this environment

Definition at line 538 of file misc.c.

References _NetAddr::baseclass, CFG_BOOL, CFG_EEXIST, CFG_INT64, CFG_NETADDR, CFG_STRING, FMT_64BIT, g_get_environ(), _ConfigContext::getaddr, _ConfigContext::getbool, _ConfigContext::getint, _ConfigContext::getstring, _ConfigContext::gettype, _ConfigContext::keycount, _ConfigContext::keys, and _AssimObj::toString.

Referenced by childprocess_new().

Here is the call graph for this function:

Here is the caller graph for this function:

void assimilation_logger ( const gchar *  log_domain,
GLogLevelFlags  log_level,
const gchar *  message,
gpointer  ignored 
)
Parameters
log_domainWhat domain are we logging to?
log_levelWhat is our log level?
messageWhat should we log
ignoredIgnored

Definition at line 212 of file misc.c.

References assim_syslogid, and assimilation_openlog().

Referenced by assimilation_openlog().

Here is the call graph for this function:

Here is the caller graph for this function:

void assimilation_openlog ( const char *  logname)

Open logs in our style (syslog)

Definition at line 187 of file misc.c.

References assim_syslogid, and assimilation_logger().

Referenced by assimilation_logger(), and main().

Here is the call graph for this function:

Here is the caller graph for this function:

FSTATIC void catch_pid_signal ( int  signum)

Should be overridden with the name to appear in the logs.

Definition at line 506 of file misc.c.

References g_unlink.

Referenced by rmpid_and_exit_on_signal().

Here is the caller graph for this function:

gboolean create_pid_file ( const char *  pidfile)

Create a pid file for the current process.

Create pid file - return TRUE on success.

Definition at line 396 of file misc.c.

References __FUNCTION__, are_we_already_running(), g_unlink, GETPID, PID_DEAD, PID_NOTUS, and PID_RUNNING.

Referenced by daemonize_me().

Here is the call graph for this function:

Here is the caller graph for this function:

void daemonize_me ( gboolean  stay_in_foreground,
const char *  dirtorunin,
char *  pidfile 
)

Function to get system name (uname -n in UNIX terms)

pathname of pid file, or NULL

Make us into a proper daemon.

Parameters
[in]stay_in_foregroundTRUE to not make a background job
[in]dirtoruninDirectory to cd to or NULL for default (/)
[in]pidfilePathname of pidfile or NULL for no pidfile

Definition at line 85 of file misc.c.

References __FUNCTION__, are_we_already_running(), create_pid_file(), DIMOF, and PID_RUNNING.

Referenced by main().

Here is the call graph for this function:

Here is the caller graph for this function:

char* get_default_pid_fileName ( const char *  procname)

get default pid file name

Make a daemon process out of this process.

Definition at line 443 of file misc.c.

References __FUNCTION__, and STD_PID_DIR.

Referenced by main().

Here is the caller graph for this function:

int kill_pid_service ( const char *  pidfile,
int  signal 
)

kill the service that goes with our current pid file - return negative iff pidfile pid is running and kill fails

Issue given signal to the pidfile-indicated running process.

Definition at line 465 of file misc.c.

References are_we_already_running(), g_unlink, and PID_RUNNING.

Referenced by main().

Here is the call graph for this function:

Here is the caller graph for this function:

guint pidrunningstat_to_status ( PidRunningStat  stat)

Convert PidRunningStat to an exit code for status.

Parameters
statStatus to convert

Definition at line 515 of file misc.c.

References PID_DEAD, PID_NOTRUNNING, PID_NOTUS, and PID_RUNNING.

Referenced by main().

Here is the caller graph for this function:

void remove_pid_file ( const char *  pidfile)

Remove the pid file that goes with this service iff we created one during this invocation.

Remove pid file we created (if we created one)

Definition at line 456 of file misc.c.

References g_unlink.

Referenced by main().

Here is the caller graph for this function:

void rmpid_and_exit_on_signal ( const char *  pidfile,
int  signal_in 
)

Remove PID file and exit when a signal is received.

Issue given signal to the pidfile-indicated running process.

Definition at line 488 of file misc.c.

References catch_pid_signal(), and signal.

Here is the call graph for this function:

Variable Documentation

const char* assim_syslogid = "assim"

Definition at line 47 of file misc.c.

Referenced by assimilation_logger(), and assimilation_openlog().