/* * Copyright (C), 2000-2004 by the monit project group. * All Rights Reserved. * * This program 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 2 of the * License, or (at your option) any later version. * * This program 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 this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef MONIT_UTIL_H #define MONIT_UTIL_H /** * General purpose utility methods. * * @author Jan-Henrik Haukeland, * @author "Martin Pala" * @author Christian Hopp * @author Michael Amster, * * @version \$Id: util.h,v 1.1 2004/12/12 23:45:09 hauk Exp $ * @file */ /** * Return only the filename with leading directory components * removed. This function does not modify the path string. * @param path A file path string * @return A pointer to the basename in path */ char *Util_basename(char* path); /** * Removes everything from the first newline (CR|LF) * @param string A string to be chomped */ void Util_chomp(char *string, int len); /** * Remove leading and trailing space from the string * @param s A string * @return s with leading and trailing spaces removed */ char *Util_trim(char *s); /** * Remove leading white space [ \t\r\n] from the string. * @param s A string * @return s with leading spaces removed */ char *Util_ltrim(char *s); /** * Remove trailing white space [ \t\r\n] from the string * @param s A string * @return s with trailing spaces removed */ char *Util_rtrim(char *s); /** * Remove any enclosing quotes ["'] from the string * @param s A string * @return s with any enclosed quotes removed */ void Util_trimQuotes(char *s); /** * Replace all occurrences of the old char in the string * s with the new char. * @param s A string * @param old The old char * @param new The new char * @return s where all occurrence of old are replaced with new */ char *Util_replace(char *s, char old, char new); /** * Replace all occurrences of the sub-string old in the string src * with the sub-string new. The method is case sensitive for the * sub-strings new and old. The string parameter src must be an * allocated string, not a character array. * @param src An allocated string reference (e.g. &string) * @param old The old sub-string * @param new The new sub-string * @return src where all occurrences of the old sub-string are * replaced with the new sub-string. */ char *Util_replaceString(char **src, const char *old, const char *new); /** * Count the number the sub-string word occurs in s. * @param s The String to search for word in * @param word The sub-string to count in s */ int Util_countWords(char *s, const char *word); /** * Return TRUE if the string a starts with the string * b. The test is case-insensitive but depends on that * all characters in the two strings can be translated in the current * locale. * @param a The string to search for b in * @param b The sub-string to test a against * @return TRUE if a starts with b, otherwise FALSE */ int Util_startsWith(const char *a, const char *b); /** * Exchanges \escape sequences in a string * @param buf A string */ void Util_handleEscapes(char *buf); /** * @param name A service name as stated in the config file * @return the named service or NULL if not found */ Service_T Util_getService(const char *name); /** * @param name A service name as stated in the config file * @return TRUE if the service name exist in the * servicelist, otherwise FALSE */ int Util_existService(const char *name); /** * Get the length of the service list, that is; the number of services * managed by monit * @return The number of services monitored */ int Util_getNumberOfServices(); /** * Print the Runtime object */ void Util_printRunList(); /** * Print a service object * @param p A Service_T object */ void Util_printService(Service_T s); /** * Print all the services in the servicelist */ void Util_printServiceList(); /** * Print file hashes from stdin or from the given file */ void Util_printHash(char *filename); /** * Open and read the pid from the given pidfile. * @param pidfile A pidfile with full path * @return the pid (TRUE) or FALSE if the pid could * not be read from the file */ pid_t Util_getPid(char *pidfile); /** * @return TRUE (i.e. the running pid id) if * the process is running, otherwise FALSE */ int Util_isProcessRunning(Service_T s); /** * Returns a RFC822 Date string. If the given date is NULL compute the * date now. * @param date * @return a date string or NULL if an error occured */ char *Util_getRFC822Date(long *date); /** * Compute an uptime for a process based on the ctime * from the pidfile. * @param pidfile A process pidfile * @return an uptime */ time_t Util_getProcessUptime(char *pidfile); /** * Compute an uptime string based on the delta time in seconds. The * caller must free the returned string. * @param delta seconds. * @param sep string separator * @return an uptime string */ char *Util_getUptime(time_t delta, char *sep); /** * @return a checksum for the given file, or NULL if error. */ char *Util_getChecksum(char *file, int hashtype); /** * Escape an uri string converting unsafe characters to a hex (%xx) * representation. The caller must free the returned string. * @param uri an uri string * @return the escaped string */ char *Util_urlEncode(char *uri); /** * Unescape an url string. The url parameter is modified * by this method. * @param url an escaped url string * @return A pointer to the unescaped urlstring */ char *Util_urlDecode(char *url); /** * Parse an url string. * @param string an url string * @return A pointer to the structure which describes particular url parts */ Url_T Util_parseURL(char *string); /** * @return a Basic Authentication Authorization string (RFC 2617), * with credentials from the Run object, NULL if credentials are not defined. */ char *Util_getBasicAuthHeader(); /** * Do printf style format line parsing * @param s format string * @param ap variable argument list * @param len The lenght of the bytes written, * may be different from the returned allocated buffer size * @return buffer with parsed string */ char *Util_formatString(const char *s, va_list ap, long *len); /** * Redirect the standard file descriptors to /dev/null and route any * error messages to the log file. */ void Util_redirectStdFds(); /* * Close all filedescriptors except standard. Everything * seems to have getdtablesize, so we'll use it here, and back * out to use 1024 if getdtablesize not available. */ void Util_closeFds(); /* * Check if monit does have credentials for this user. If successful * a pointer to the password is returned. */ Auth_T Util_getUserCredentials(char *uname); /** * Check if the given password match the registred password for the * given username. * @param uname Username * @param outside The password to test * @return TRUE if the passwords match for the given uname otherwise * FALSE */ int Util_checkCredentials(char *uname, char *outside); /** * Compute SHA1 and MD5 message digests simultaneously for bytes read * from STREAM (suitable for stdin, which is not always rewindable). * The resulting message digest numbers will be written into the first * bytes of resblock buffers. * @param stream The stream from where the digests are computed * @param sha_resblock The buffer to write the SHA1 result to * @param md5_resblock The buffer to write the MD5 result to */ int Util_getStreamDigests (FILE *stream, void *sha_resblock, void *md5_resblock); /** * Reset the process information structure */ void Util_resetProcInfo(Service_T s); /** * Are service status data available? * @param s The service to test * @return TRUE if available otherwise FALSE */ int Util_hasServiceStatus(Service_T s); /** * Construct a HTTP/1.1 Host header utilizing information from the * socket. The returned hostBuf is set to "hostname:port" or to the * empty string if information is not available or not applicable. * @param s A connected socket * @param hostBuf the buffer to write the host-header to * @param len Length of the hostBuf * @return the hostBuffer */ char *Util_getHTTPHostHeader(Socket_T s, char *hostBuf, int len); /** * Evaluate a qualification expression. * @param operator The qualification operator * @param left Expression lval * @param rightExpression rval * @return the boolean value of the expression */ int Util_evalQExpression(int operator, int left, int right); #endif