The Assimilation 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 <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 _GNU_SOURCE   /* Needed for the F_[SG]ETPIPE_SZ definitions */
 
#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...
 
FSTATIC char * _shell_array_value (GSList *arrayvalue)
 Return the value of an array in a shell-compatible way - to put in an environment variable. 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...
 
WINEXPORT gsize setpipebuf (int fd, gsize bufsize)
 Set the buffer size of a pipe (if possible) More...
 
WINEXPORT gsize getpipebuf (int fd)
 Return the buffer size of a pipe - if not possible return 4096 (a good guess) More...
 
WINEXPORT void assim_g_notify_unref (gpointer assimobj)
 Unref for glib notify. 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
  • setpipebuf - set the buffer size of a pipe

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 _GNU_SOURCE   /* Needed for the F_[SG]ETPIPE_SZ definitions */

Definition at line 26 of file misc.c.

#define GETPID   getpid()

Referenced by create_pid_file().

#define MAXPATH   256

Definition at line 292 of file misc.c.

Referenced by are_we_already_running().

#define MAXPIDLEN   16

Definition at line 291 of file misc.c.

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

Definition at line 289 of file misc.c.

Referenced by are_we_already_running().

#define PROCSELFEXE   "/proc/self/exe"

Definition at line 288 of file misc.c.

Referenced by are_we_already_running().

#define SEP   '/'

Definition at line 287 of file misc.c.

Referenced by are_we_already_running().

Function Documentation

FSTATIC char * _shell_array_value ( GSList *  arrayvalue)

Return the value of an array in a shell-compatible way - to put in an environment variable.

Definition at line 657 of file misc.c.

References CASTTOCLASS, CFG_STRING, _ConfigValue::strvalue, _ConfigValue::u, and _ConfigValue::valtype.

Referenced by assim_merge_environ().

Here is the caller graph for this function:

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 298 of file misc.c.

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

Referenced by create_pid_file(), daemonize_me(), kill_pid_service(), cma.cma::main(), 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 676 of file misc.c.

Referenced by childprocess_new().

Here is the caller graph for this function:

WINEXPORT void assim_g_notify_unref ( gpointer  assimobj)

Unref for glib notify.

Definition at line 720 of file misc.c.

References CASTTOCLASS, and _AssimObj::unref.

Referenced by _cryptframe_initialize_maps().

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 543 of file misc.c.

References _shell_array_value(), _NetAddr::baseclass, CFG_ARRAY, CFG_BOOL, CFG_EEXIST, CFG_INT64, CFG_NETADDR, CFG_STRING, FMT_64BIT, g_get_environ(), _ConfigContext::getaddr, _ConfigContext::getarray, _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 217 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 192 of file misc.c.

References assim_syslogid, and assimilation_logger().

Referenced by assimilation_logger(), cma.cma::main(), 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 511 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 401 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 90 of file misc.c.

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

Referenced by cma.cma::main(), and 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 448 of file misc.c.

References STD_PID_DIR.

Referenced by main().

Here is the caller graph for this function:

WINEXPORT gsize getpipebuf ( int  fd)

Return the buffer size of a pipe - if not possible return 4096 (a good guess)

Return pipe buffer size.

Definition at line 710 of file misc.c.

Referenced by setpipebuf().

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 470 of file misc.c.

References are_we_already_running(), g_unlink, and PID_RUNNING.

Referenced by cma.cma::main(), and 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 520 of file misc.c.

References PID_DEAD, PID_NOTRUNNING, PID_NOTUS, and PID_RUNNING.

Referenced by cma.cma::main(), and 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 461 of file misc.c.

References g_unlink.

Referenced by cma.cma::main(), and 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 493 of file misc.c.

References catch_pid_signal(), and signal.

Referenced by cma.cma::main().

Here is the call graph for this function:

Here is the caller graph for this function:

WINEXPORT gsize setpipebuf ( int  fd,
gsize  bufsize 
)

Set the buffer size of a pipe (if possible)

Set pipe buffer size (if possible)

Definition at line 684 of file misc.c.

References getpipebuf().

Here is the call graph for this function:

Variable Documentation

const char* assim_syslogid = "assim"

Definition at line 51 of file misc.c.

Referenced by assimilation_logger(), and assimilation_openlog().