libtools
Functions
Input/Output routines

Functionality includes closing descriptors of current process, running server as a daemon, recursive remove of a directory or a file, opening and locking file, closing file. More...

Collaboration diagram for Input/Output routines:

Functions

int closeall (int nostdclose)
 close all file descriptors of current process More...
 
pid_t daemonize (const char *path, char *const argv[], int nochdir, int noclose)
 run program as daemon More...
 
int pidlock_set (const char *path)
 try to save pid of calling process to file More...
 
pid_t pidlock_get (const char *path)
 read pid from file and verify it More...
 
int dir_remove (const char *path)
 recursive remove directory or file More...
 
int mkdirectory (const char *path, mode_t mode)
 verify or create directory More...
 
int executefile (const char *path)
 execute file as program More...
 
int put_file_contents (const char *path, const void *buf, size_t len)
 save data to file More...
 
void * get_file_contents (const char *path, void **buf, size_t *len)
 allocate memory and read data from file More...
 
FILE * flopen (const char *path, const char *mode, FILE **fp)
 open and lock file More...
 
FILE * fltryopen (const char *path, const char *mode, FILE **fp)
 open and try lock file More...
 
int flclose (FILE *fp)
 close file openned by flopen() or fltryopen() More...
 
int movefile (const char *oldpath, const char *newpath)
 move file to new path More...
 

Detailed Description

Functionality includes closing descriptors of current process, running server as a daemon, recursive remove of a directory or a file, opening and locking file, closing file.

Function Documentation

int closeall ( int  nostdclose)

close all file descriptors of current process

Parameters
[in]nostdclosedon't close stdio file descriptors
Returns
zero is returned
Return values
-1error occurred

This function can be useful for implementation system() like functions.

pid_t daemonize ( const char *  path,
char *const  argv[],
int  nochdir,
int  noclose 
)

run program as daemon

Parameters
[in]pathpath to program file
[in]argvarguments for program (last item should be NULL)
[in]nochdirdon't change current directory to '/' for daemon
[in]noclosedon't close stdio streams for daemon
Returns
pid of started daemon
Return values
-1error occured
int dir_remove ( const char *  path)

recursive remove directory or file

Parameters
[in]pathname of directory or file
Returns
on success, zero is returned

Function updates errno if error occurred:

  • ENOMEM no memory format path

If function fails to delete directory (file) it will continue to delete rest of the directories (files), but don't return zero.

int executefile ( const char *  path)

execute file as program

Parameters
[in]pathpath to executable file
Returns
status code of child process
Return values
-1error occurred
int flclose ( FILE *  fp)

close file openned by flopen() or fltryopen()

Parameters
[in]fpfile descriptor (can be NULL)
Returns
on success, zero is returned
Return values
-1error occurred

If error occurred function update errno - see errors specified for the routines fileno(3), flock(2), and fclose(3).

FILE* flopen ( const char *  path,
const char *  mode,
FILE **  fp 
)

open and lock file

Parameters
[in]pathname of the file to be created or opened
[in]modepermissive mode
[out]fpfile descriptor
Returns
pointer to descriptor
Return values
NULLerror occurred, fp also should be NULL

If error occurred function update errno - see errors specified for the routines fopen(3), fileno(3), flock(2), and fclose(3).

FILE* fltryopen ( const char *  path,
const char *  mode,
FILE **  fp 
)

open and try lock file

Parameters
[in]pathname of the file to be created or opened
[in]modepermissive mode
[out]fpfile descriptor
Returns
pointer to descriptor
Return values
NULLerror occurred, fp also should be NULL

If error occurred function update errno - see errors specified for the routines fopen(3), fileno(3), flock(2), and fclose(3).

void* get_file_contents ( const char *  path,
void **  buf,
size_t *  len 
)

allocate memory and read data from file

Parameters
[in]pathfile name
[out]bufpointer to file data
[out]lenpointer to file length
Returns
on success pointer to allocated memory
Return values
NULLerror occurred
int mkdirectory ( const char *  path,
mode_t  mode 
)

verify or create directory

Parameters
[in]pathdirectory name
[in]modedirectory permissions
Returns
zero returned
Return values
-1error occurred

This function make parent directories, if needed.

int movefile ( const char *  oldpath,
const char *  newpath 
)

move file to new path

Parameters
[in]oldpathfilename to move
[in]newpathnew path of file
Returns
zero is returned
Return values
-1error occured, check errno for more info

When oldpath and newpath on the same filesystem - rename() syscall will be used. If rename() syscall failed function will try create new and delete old file.

pid_t pidlock_get ( const char *  path)

read pid from file and verify it

Parameters
[in]pathname of pid file
Returns
pid of process
Return values
0process don't exist
-1error occurred

If error occurred function update errno - see errors specified for the routines flopen(), flclose(), fscanf(3) and kill(2).

int pidlock_set ( const char *  path)

try to save pid of calling process to file

Parameters
[in]pathname of pid file
Returns
on success, zero is returned
Return values
-1error occurred

Function fail, if another process already set pidlock.

If error occurred function update errno - see errors specified for the routines fltryopen(), flclose(), fscanf(3), fprintf(3), ftruncate(2) and kill(2).

int put_file_contents ( const char *  path,
const void *  buf,
size_t  len 
)

save data to file

Parameters
[in]pathfile name
[in]bufpointer to data buffer
[in]lenlength of data at buffer buf
Returns
on success, zero is returned
Return values
-1error occurred