lwatch.h File Reference

Public interface for watching log files. More...

#include <sys/param.h>
#include "watch_database.h"

Go to the source code of this file.

Classes

struct  lwatch_event_t
 Watch event. More...

Defines

#define LWATCH_ERROR_BASE   -4000
 Base error code (minimum value) for log watch errors.
#define LWATCH_ERROR_LIMIT   (LWATCH_ERROR_BASE + 50)
 Maximum error code for log watch errors.
#define IS_LWATCH_ERROR(error)   (((error) >= LWATCH_ERROR_BASE) && ((error) < LWATCH_ERROR_LIMIT))
 Return true if error code is within the range for log watch errors.
#define LWATCH_ERROR_CANNOT_MONITOR   (LWATCH_ERROR_BASE + 1)
 cannot monitor target
#define LWATCH_ERROR_CANNOT_FIND_PATH_INFO   (LWATCH_ERROR_BASE + 2)
 cannot find path info
#define LWATCH_ERROR_LOST_RENAME   (LWATCH_ERROR_BASE + 3)
 one of the paths for a rename event is unknown
#define LWATCH_ERROR_STAT_NOT_VALID   (LWATCH_ERROR_BASE + 4)
 stat information is not valid

Typedefs

typedef int(* lwatch_event_callback_t )(struct lwatch_event_t *event)
 This callback is used when a watch event occurs.

Enumerations

enum  lwatch_event_type {
  LWATCH_EVENT_CREATE, LWATCH_EVENT_DELETE, LWATCH_EVENT_RENAME, LWATCH_EVENT_MODIFY,
  LWATCH_EVENT_OPEN, LWATCH_EVENT_CLOSE
}
 The set of log watch event types. More...

Functions

int destroy_path_info (struct lwatch_path_info_t **ppath_info)
 Release lwatch_path_info_t resources, free it's memory, set variable to NULL.
int update_stat_info (struct stat *cur_stat_info, struct lwatch_path_info_t *path_info)
 Merge stat information into path into our saved information about the file.
bool is_log_backup (const char *path)
 Given a path name return true if it appears to be a rotated backup of a primary log file.


Detailed Description

Public interface for watching log files.

Definition in file lwatch.h.


Define Documentation

#define IS_LWATCH_ERROR ( error   )     (((error) >= LWATCH_ERROR_BASE) && ((error) < LWATCH_ERROR_LIMIT))

Return true if error code is within the range for log watch errors.

Parameters:
[in] error The error code to test if it's a log watch error code.
Returns:
True if error code is a log watch error.

Definition at line 39 of file lwatch.h.

Referenced by error_string().


Typedef Documentation

typedef int(* lwatch_event_callback_t)(struct lwatch_event_t *event)

This callback is used when a watch event occurs.

Parties interested in receiving watch events register a callback of this type. When a watch event occurs their callback is invoked passing the watch event to it.

Definition at line 87 of file lwatch.h.


Enumeration Type Documentation

The set of log watch event types.

Enumerator:
LWATCH_EVENT_CREATE  a file or directory was created
LWATCH_EVENT_DELETE  a file or directory was deleted
LWATCH_EVENT_RENAME  a file or directory was renamed
LWATCH_EVENT_MODIFY  a file or directory was modified
LWATCH_EVENT_OPEN  a file or directory was opened
LWATCH_EVENT_CLOSE  a file or directory was closed

Definition at line 53 of file lwatch.h.

00053              {
00054     LWATCH_EVENT_CREATE,        /**< a file or directory was created */
00055     LWATCH_EVENT_DELETE,        /**< a file or directory was deleted */
00056     LWATCH_EVENT_RENAME,        /**< a file or directory was renamed */
00057     LWATCH_EVENT_MODIFY,        /**< a file or directory was modified */
00058     LWATCH_EVENT_OPEN,          /**< a file or directory was opened */
00059     LWATCH_EVENT_CLOSE          /**< a file or directory was closed */
00060 } lwatch_event_type;


Function Documentation

int destroy_path_info ( struct lwatch_path_info_t **  ppath_info  ) 

Release lwatch_path_info_t resources, free it's memory, set variable to NULL.

Parameters:
[out] ppath_info Pointer to a pointer of saved information about a file. Upon a successful return this pointer will be set to NULL.
Returns:
SUCCESS (0) or non-zero error code otherwise

Definition at line 1151 of file lwatch.c.

01152 {
01153     struct lwatch_path_info_t *path_info;
01154 
01155     if (!ppath_info) return EINVAL;
01156     path_info = *ppath_info;
01157     if (!path_info) return EINVAL;
01158 
01159     free(path_info);
01160     *ppath_info = NULL;
01161 
01162     return SUCCESS;
01163 }

bool is_log_backup ( const char *  path  ) 

Given a path name return true if it appears to be a rotated backup of a primary log file.

Parameters:
[in] path The path name to test.
Returns:
SUCCESS (0) or non-zero error code otherwise
By convention backing up log files is done via a process called rotation where the current version of a (primary) log file is renamed to a backup name and the primary log file is reopened. Usually the last N copies of the backup are kept, backup.1 is moved to backup.2, backup.2 is moved to backup.3, and backup.N+1 is discarded (hence the notion of rotation). Also by convention the name of backup files is the name of the primary file with a rotation number appended, or the date of the rotation appended.

This routine uses regular expressions to intuit from a pathname if the pathname appears to follow a rotation backup naming convention. Only the pathname is considered, the file contents are not examined.

Currently there are two rotation backup naming conventions which are looked for, both are supported by the logrotate facility found on many machines.

  • numbered backups
  • dated backups

See also:
logrotate_numbered_backup_regexp

logrotate_date_backup_regexp

Definition at line 1229 of file lwatch.c.

Referenced by find_log_files_callback(), and start_monitoring().

01230 {
01231     int num_matches;
01232 
01233     if ((num_matches = regexp_search(&logrotate_numbered_backup_regexp, path)) > 0) {
01234 # if 0
01235         // FIXME: currently we don't use the sub-fields, this code is left as an
01236         // example of how to do that
01237 
01238         char directory[PATH_MAX];
01239         char filename[PATH_MAX];
01240         char number[9];
01241         char extension[PATH_MAX];
01242 
01243         regexp_substring(&logrotate_numbered_backup_regexp, 1, directory, sizeof(directory));
01244         regexp_substring(&logrotate_numbered_backup_regexp, 2, filename, sizeof(filename));
01245         regexp_substring(&logrotate_numbered_backup_regexp, 3, number, sizeof(number));
01246         regexp_substring(&logrotate_numbered_backup_regexp, 4, extension, sizeof(extension));
01247 
01248         if (0) printf(" num_matches=%d directory=\"%s\" filename=\"%s\" number=\"%s\" extension=\"%s\" ",
01249                       num_matches, directory, filename, number, extension);
01250 #endif
01251         return true;
01252     }
01253     if ((num_matches = regexp_search(&logrotate_date_backup_regexp, path)) > 0) {
01254 # if 0
01255         // FIXME: currently we don't use the sub-fields, this code is left as an
01256         // example of how to do that
01257 
01258         char directory[PATH_MAX];
01259         char filename[PATH_MAX];
01260         char date[9];
01261         char year[5];
01262         char month[3];
01263         char day[3];
01264         char extension[PATH_MAX];
01265 
01266         regexp_substring(&logrotate_date_backup_regexp, 1, directory, sizeof(directory));
01267         regexp_substring(&logrotate_date_backup_regexp, 2, filename, sizeof(filename));
01268         regexp_substring(&logrotate_date_backup_regexp, 3, date, sizeof(date));
01269         regexp_substring(&logrotate_date_backup_regexp, 4, year, sizeof(year));
01270         regexp_substring(&logrotate_date_backup_regexp, 5, month, sizeof(month));
01271         regexp_substring(&logrotate_date_backup_regexp, 6, day, sizeof(day));
01272         regexp_substring(&logrotate_date_backup_regexp, 7, extension, sizeof(extension));
01273 
01274         if (0) printf(" num_matches=%d directory=\"%s\" filename=\"%s\" date=\"%s\" year=\"%s\" month=\"%s\" day=\"%s\" extension=\"%s\" ",
01275                       num_matches, directory, filename, date, year, month, day, extension);
01276 #endif
01277         return true;
01278     }
01279     return false;
01280 }

int update_stat_info ( struct stat *  cur_stat_info,
struct lwatch_path_info_t path_info 
)

Merge stat information into path into our saved information about the file.

Parameters:
[in] cur_stat_info Pointer to stat data to update from, or if NULL call stat() to obtain info
[in] path_info Our saved information about the file
Returns:
SUCCESS (0) or non-zero error code otherwise

Definition at line 1173 of file lwatch.c.

Referenced by update_stat_info_and_write().

01174 {
01175     int error;
01176     struct stat stat_info;
01177 
01178     if (cur_stat_info == NULL) {
01179         if (stat(path_info->path, &stat_info) < 0) {
01180             error = errno;
01181             log_msg(LOG_ERROR, _("cannot perform stat on \"%s\" (%s) \n"),
01182                     path_info->path, error_string(error));
01183             return error;
01184         }
01185         cur_stat_info = &stat_info;
01186     }
01187 
01188     path_info->dev               = cur_stat_info->st_dev;
01189     path_info->inode             = cur_stat_info->st_ino;
01190     path_info->mode              = cur_stat_info->st_mode;
01191     path_info->uid               = cur_stat_info->st_uid;
01192     path_info->gid               = cur_stat_info->st_gid;
01193     path_info->size              = cur_stat_info->st_size;
01194     path_info->access_time       = cur_stat_info->st_atime;
01195     path_info->modification_time = cur_stat_info->st_mtime;
01196     path_info->change_time       = cur_stat_info->st_ctime;
01197 
01198     path_info->flags |= STAT_DATA_VALID_FLAG;
01199 
01200     return SUCCESS;
01201 }


Generated on Mon Aug 31 10:06:21 2009 by  doxygen 1.5.8