util.c File Reference

General purpose utility methods. More...

Go to the source code of this file.

Functions

void error (const char *format,...)
 Print a formated message to stderr or to the logfile if no tty exist.

int is_strdefined (char *p)
char * stripfilename (char *path)
 Strip the path and return only the filename.

void chomp (char *string)
 Removes everything from the first newline (CR|LF).

char * trim (char *s)
 Remove leading and trailing space from the string.

char * ltrim (char *s)
 Remove leading white space [ \t\r
] from the string.


char * rtrim (char *s)
 Remove trailing white space [ \t\r
] from the string.


char * trim_quotes (char *s)
 Remove any enclosing quotes ["'] from the string.

int starts_with (char *a, char *b)
 Return TRUE if the string a starts with the string b.

void handle_string_escapes (char *buf)
 Exchanges \escape sequences in a string.

Process_T get_process (char *name)
int exist_process (char *name)
void printrunlist ()
 Print the Runtime object.

void printprocess (Process_T p)
 Print a process object.

void printprocesslist ()
 Print all the processes in the processlist.

pid_t get_pid (char *pidfile)
 Open and read the pid from the given pidfile.

int is_process_running (Process_T p)
char * get_RFC1123date (long *date)
 Returns a (RFC1123) Date string.

char * get_ctime ()
 Get a non
terminated ctime.


char * get_process_uptime (char *pidfile)
 Compute an uptime string for a process based on the ctime from the pidfile.

char * get_uptime (time_t delta)
 Compute an uptime string based on the delta time in seconds.

int set_md5sum (char **dest, char *file)
 Compute a md5 checksum for the given file and save the result in an allocated area pointed to by dest.

char * get_md5sum (char *file)
int check_md5 (char *file, char *sum)
char * url_encode (char *uri)
 Escape an uri string converting unsafe characters to a hex (xx) representation.

char * get_basic_authentication_header ()
char * format (const char *s, va_list ap)
 Do printf style format line parsing.

void redirect_stdfd ()
 Redirect the standard file descriptors to /dev/null and route any error messages to the log file.


Detailed Description

General purpose utility methods.

Author:
Jan-Henrik Haukeland, <hauk@tildeslash.com> , "Martin Pala" <martin.pala@hq.iol.cz> , Christian Hopp <chopp@iei.tu-clausthal.de> , Michael Amster, <mamster@webeasy.com>
Version:
$Id: util.c,v 1.51 2002/12/21 01:45:22 hauk Exp $

Definition in file util.c.


Function Documentation

int check_md5 char *    file,
char *    sum
 

Parameters:
file  A file to open and compute a md5 checksum for
sum  A previous checksum computed for this file
Returns:
TRUE if computation of md5sum on the given file, is equal to the given sum, otherwise return FALSE. FALSE is also returned if md5_stream fails.

Definition at line 918 of file util.c.

References ASSERT, and get_md5sum().

void chomp char *    string
 

Removes everything from the first newline (CR|LF).

Parameters:
string  A string to be chomped

Definition at line 143 of file util.c.

References ASSERT.

Referenced by check_ftp(), check_http(), check_imap(), check_nntp(), check_pop(), check_smtp(), check_ssh(), and get_ctime().

void error const char *    format,
...   
 

Print a formated message to stderr or to the logfile if no tty exist.

Parameters:
format  A formate (printf-style) string

Definition at line 86 of file util.c.

References ASSERT, format(), myrun::have_tty, log(), and Run.

Referenced by check_process(), create_ssl_server_socket(), create_ssl_socket(), d_check_process(), embed_accepted_ssl_socket(), embed_ssl_socket(), init_ssl_server(), kill_daemon(), log_close(), new_ssl_connection(), recv_ssl_socket(), redirect_stdfd(), send_ssl_socket(), start_httpd(), xcalloc(), xmalloc(), and xresize().

int exist_process char *    name
 

Parameters:
name  A process name as stated in the config file
Returns:
TRUE if the process name exist in the processlist, otherwise FALSE

Definition at line 343 of file util.c.

References ASSERT, is, myprocess::name, myprocess::next, and processlist.

char* format const char *    s,
va_list    ap
 

Do printf style format line parsing.

Parameters:
s  format string
ap  variable argument list
Returns:
buffer with parsed string

Definition at line 1009 of file util.c.

References ASSERT, xmalloc(), and xresize().

Referenced by error().

char* get_basic_authentication_header  
 

Returns:
a Basic Authentication Authorization string (RFC 2617), with credentials from the Run object, The string "\r
" if credentials are not defined.

Definition at line 981 of file util.c.

References myrun::Auth, myrun::myautentication::defined, encode_base64(), myrun::myautentication::passwd, Run, myrun::myautentication::uname, xmalloc(), and xstrdup().

Referenced by d_check_process().

char* get_ctime  
 

Get a non
terminated ctime.

Returns:
a ctime string (i.e. date)

Definition at line 768 of file util.c.

References chomp(), and xmalloc().

char* get_md5sum char *    file
 

Returns:
a md5 checksum for the given file, or NULL if error.

Definition at line 869 of file util.c.

References ASSERT, isreg_file(), and xstrdup().

Referenced by check_md5(), and set_md5sum().

pid_t get_pid char *    pidfile
 

Open and read the pid from the given pidfile.

Parameters:
pidfile  A pidfile with full path
Returns:
the pid (TRUE) or FALSE if the pid could not be read from the file

Definition at line 649 of file util.c.

References ASSERT, exist_file(), isreg_file(), log(), prog, and STRERROR.

Referenced by is_process_running().

Process_T get_process char *    name
 

Parameters:
name  A process name as stated in the config file
Returns:
the named process or NULL if not found

Definition at line 321 of file util.c.

References ASSERT, is, myprocess::name, myprocess::next, and processlist.

Referenced by check_process().

char* get_process_uptime char *    pidfile
 

Compute an uptime string for a process based on the ctime from the pidfile.

The caller must free the returned string.

Parameters:
pidfile  A process pidfile
Returns:
an uptime string

Definition at line 788 of file util.c.

References ASSERT, get_timestamp(), get_uptime(), and xstrdup().

Referenced by status().

char* get_RFC1123date long *    date
 

Returns a (RFC1123) Date string.

If the given date is NULL compute the date now.

Parameters:
date 
Returns:
a date string alligned with RFC1123

Definition at line 726 of file util.c.

References xstrdup().

Referenced by sendmail().

char* get_uptime time_t    delta
 

Compute an uptime string based on the delta time in seconds.

The caller must free the returned string.

Parameters:
delta  seconds.
Returns:
an uptime string

Definition at line 814 of file util.c.

References xstrdup().

Referenced by get_process_uptime().

void handle_string_escapes char *    buf
 

Exchanges \escape sequences in a string.

Parameters:
buf  A string

Definition at line 262 of file util.c.

References ASSERT.

int is_process_running Process_T    p
 

Returns:
TRUE (i.e. the running pid id) if the process is running, otherwise FALSE

Definition at line 698 of file util.c.

References ASSERT, get_pid(), getpgid(), and myprocess::pidfile.

Referenced by check_process(), and exist_daemon().

int is_strdefined char *    p
 

Returns:
TRUE if the string parameter is defined, otherwise FALSE

Definition at line 114 of file util.c.

char* ltrim char *    s
 

Remove leading white space [ \t\r
] from the string.

Parameters:
s  A string
Returns:
s with leading spaces removed

Definition at line 176 of file util.c.

References ASSERT.

Referenced by trim().

void printprocess Process_T    p
 

Print a process object.

Parameters:
p  A Process_T object

Definition at line 426 of file util.c.

References myresource::action, mytimestamp::action, mymail::alert_on_checksum, mymail::alert_on_resource, mymail::alert_on_restart, mymail::alert_on_stop, mymail::alert_on_timeout, mymail::alert_on_timestamp, mycommand::arg, ASSERT, myport::certmd5, myprocess::checksumlist, myprocess::def_every, myprocess::def_timeout, mydependant::dependant, myprocess::dependantlist, myprocess::every, myport::family, mychecksum::file, mymail::from, myprocess::group, myport::hostname, myresource::limit, myprocess::maillist, myresource::max_cycle, mychecksum::md5, mymail::message, myprocess::mode, myport::myprotocol::name, myprocess::name, mymail::next, myresource::next, mytimestamp::next, myport::next, mydependant::next, mychecksum::next, myresource::operator, mytimestamp::operator, mytimestamp::pathname, myport::pathname, myprocess::pidfile, myport::port, myprocess::portlist, myport::protocol, myport::request, myresource::resource_id, RESOURCE_ID_CPU_PERCENT, RESOURCE_ID_LOAD1, RESOURCE_ID_LOAD15, RESOURCE_ID_LOAD5, RESOURCE_ID_MEM_KBYTE, RESOURCE_ID_MEM_PERCENT, myprocess::resourcelist, myport::ssl, myprocess::start, myprocess::stop, mymail::subject, mytimestamp::time, myprocess::timestamplist, mymail::to, myprocess::to_cycle, and myprocess::to_start.

Referenced by printprocesslist().

void printprocesslist  
 

Print all the processes in the processlist.

Definition at line 624 of file util.c.

References myprocess::next, printprocess(), and processlist.

void printrunlist  
 

Print the Runtime object.

Definition at line 361 of file util.c.

References myrun::allowselfcert, myrun::Auth, myrun::bind_addr, myrun::controlfile, myrun::debug, myrun::myautentication::defined, myrun::dohttpd, myrun::dolog, myrun::doprocess, myrun::myformat::from, myrun::httpdport, myrun::httpdssl, myrun::httpsslclientpem, myrun::httpsslpem, myrun::isdaemon, myrun::logfile, myrun::MailFormat, myrun::mailserver, myrun::myformat::message, myrun::pidfile, myrun::polltime, Run, myrun::myformat::subject, and myrun::use_syslog.

void redirect_stdfd  
 

Redirect the standard file descriptors to /dev/null and route any error messages to the log file.

Definition at line 1042 of file util.c.

References error(), myrun::have_tty, Run, and STRERROR.

Referenced by daemonize(), and spawn().

char* rtrim char *    s
 

Remove trailing white space [ \t\r
] from the string.

Parameters:
s  A string
Returns:
s with trailing spaces removed

Definition at line 194 of file util.c.

References ASSERT.

Referenced by trim().

int set_md5sum char **    dest,
char *    file
 

Compute a md5 checksum for the given file and save the result in an allocated area pointed to by dest.

The caller is responsible for freeing dest.

Parameters:
dest  a pointer to an allocated area with the computed md5 sum
file  The name of file to compute a md5 sum for
Returns:
TRUE if success otherwise FALSE.

Definition at line 853 of file util.c.

References ASSERT, and get_md5sum().

int starts_with char *    a,
char *    b
 

Return TRUE if the string a starts with the string b.

Parameters:
a  The string to search for b in
b  The sub-string to test a against
Returns:
TRUE if a starts with b, otherwise FALSE

Definition at line 242 of file util.c.

char* stripfilename char *    path
 

Strip the path and return only the filename.

Parameters:
path  A file path string
Returns:
the basename

Definition at line 126 of file util.c.

References ASSERT.

Referenced by main().

char* trim char *    s
 

Remove leading and trailing space from the string.

Parameters:
s  A string
Returns:
s with leading and trailing spaces removed

Definition at line 159 of file util.c.

References ASSERT, ltrim(), and rtrim().

char* trim_quotes char *    s
 

Remove any enclosing quotes ["'] from the string.

Parameters:
s  A string
Returns:
s with any enclosed quotes removed

Definition at line 214 of file util.c.

References ASSERT.

char* url_encode char *    uri
 

Escape an uri string converting unsafe characters to a hex (xx) representation.

The caller must free the returned string.

Parameters:
uri  an uri string
Returns:
the escaped string

Definition at line 949 of file util.c.

References ASSERT, and xmalloc().