81 lines
2.5 KiB
C
81 lines
2.5 KiB
C
#ifndef UC_RESTART_COUNTER_H_
|
|
#define UC_RESTART_COUNTER_H_
|
|
/** @file
|
|
* Simple support for keeping track of number of restarts.
|
|
*
|
|
* Certain protocols desire support for comminicating the number
|
|
* of times an entity has restarted.
|
|
* The counter might also serve as a seed for sequence number generation
|
|
* to minimize recycling a recently used sequence number.
|
|
*
|
|
* The number of times a process has been restarted is simply kept
|
|
* track of via a counter in a restart state ile in ascii representation.
|
|
*/
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
struct UCRestartCounter {
|
|
unsigned int counter;
|
|
int fd;
|
|
};
|
|
|
|
typedef enum {
|
|
UC_RC_OK,
|
|
/** accessing the file failed, inspect errno
|
|
* for more info */
|
|
UC_RC_ERR_FILE_ACCESS_ERROR,
|
|
/* The file existed and is opened but
|
|
* no valid number could be read from the file.
|
|
* This is a transient error,
|
|
* the now new counter is initialized to 0, and
|
|
* written to the file */
|
|
UC_RC_ERR_FILE_CONTENT_ERROR,
|
|
/* UC_RC_FL_LOCK_FILE was specified but the file
|
|
* is already locked by some other process */
|
|
UC_RC_ERR_FILE_LOCKED,
|
|
/** unspecified error. Inspect errno. */
|
|
UC_RC_ERR_UNSPECIFIED,
|
|
/** Given by uc_restart_counter_close if the
|
|
* file never was opened */
|
|
UC_RC_ERR_NOT_OPENED,
|
|
} UCRCResult;
|
|
|
|
/** Lock the restart state file and keep it open.
|
|
* e.g. to prevent several processes from being started
|
|
* and using the same restart state file.*/
|
|
#define UC_RC_FL_LOCK_FILE ( 1 << 0)
|
|
|
|
/** Read and update the current restart counter.
|
|
* This is typically called once at program startup.
|
|
*
|
|
* @param counter the counter to initialize
|
|
* @param filename the filename to use to keep track of the state
|
|
* @param flags one of UC_RC_FL_XX
|
|
*/
|
|
UCRCResult uc_restart_counter_init(struct UCRestartCounter *counter,
|
|
const char *filename,
|
|
unsigned int flags);
|
|
|
|
/** Close the current restart counter file if it was opened with the
|
|
* UC_RC_FL_LOCK_FILE flag.
|
|
*
|
|
* @counter counter file to close. (uc_restart_counter can still be called
|
|
* after * the file is closed)
|
|
* @return UC_RC_OK or if the file was not kept open with UC_RC_ERR_FILE_LOCKED
|
|
*/
|
|
UCRCResult uc_restart_counter_close(struct UCRestartCounter *counter);
|
|
|
|
/** Get the current restart counter.
|
|
* In case uc_restart_counter_init failed, this will return 0.
|
|
*/
|
|
unsigned int uc_restart_counter_get(const struct UCRestartCounter *counter);
|
|
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|