Qsmtp  0.30dev
Functions | Variables
control.c File Reference

functions to load data from configuration files More...

#include <control.h>
#include <fmt.h>
#include <log.h>
#include <mmap.h>
#include <qdns.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <syslog.h>
#include <unistd.h>

Functions

static size_t compact_buffer (char **buf, char *inbuf, size_t oldlen)
 compact a given buffer More...
 
size_t lloadfilefd (int fd, char **buf, const int striptab)
 
int loadintfd (int fd, unsigned long *result, const unsigned long def)
 
size_t loadoneliner (int base, const char *filename, char **buf, const int optional)
 read a configuration file that only may contain one line More...
 
size_t loadonelinerfd (int fd, char **buf)
 
char ** data_array (unsigned int entries, size_t datalen, void *oldbuf, size_t oldlen)
 create a combined data and pointer array More...
 
int loadlistfd (int fd, char ***bufa, checkfunc cf)
 
int finddomainfd (int fd, const char *domain, const int cl)
 
int finddomain (const char *buf, const off_t size, const char *domain)
 search a domain entry in a given buffer More...
 

Variables

int controldir_fd = -1
 

Detailed Description

functions to load data from configuration files

Function Documentation

static size_t compact_buffer ( char **  buf,
char *  inbuf,
size_t  oldlen 
)
static

compact a given buffer

Parameters
bufoutput parameter
inbufthe buffer to compact
oldlenthe current length of inbuf
Returns
the length of the new buffer
Return values
0no valid entries were found in inbuf, inbuf was freed
char** data_array ( unsigned int  entries,
size_t  datalen,
void *  oldbuf,
size_t  oldlen 
)

create a combined data and pointer array

Parameters
entrieshow many entries should be in the pointer array (not counting the terminating NULL entry)
datalenlength needed for the data, 1 byte per entry is added for the terminating '\0' characters
oldbufif a previous buffer should be realloc()ed, may be NULL
oldlenlength of oldbuf, must be 0 if oldbuf is NULL
Returns
new buffer or NULL on error

If oldbuf is given the contents of the old buffer are moved to the beginning of the data section of the new memory area.

If oldbuf is given, but reallocation fails (i.e. NULL is returned), then oldbuf is not freed.

int finddomain ( const char *  buf,
const off_t  size,
const char *  domain 
)

search a domain entry in a given buffer

Parameters
bufcontaining the domain list
sizesize of buffer
domaindomain name to find
Return values
1on match
0if none

trailing spaces and tabs in a line are ignored, lines beginning with '#' are ignored, CR in file will cause trouble

int finddomainfd ( int  fd,
const char *  domain,
const int  cl 
)

mmap a file and search a domain entry in it

Parameters
fdfile descriptor
domaindomain name to find
clclose fd or not
Return values
1on match
0if none
-1on error

trainling spaces and tabs in a line are ignored, lines beginning with '#' are ignored, CR in file will cause trouble

size_t lloadfilefd ( int  fd,
char **  buf,
const int  striptab 
)

load a text file into a buffer using locked IO

Parameters
fdfile descriptor of the file to load
bufthe contents of the file will go here, memory will be malloced
striptab2: strip trailing whitespace 1: compact {'\0'}* to a single '\0' 0: do nothing but load the file into the buffer
Returns
length of buffer
Return values
-1on error (errno is set)

if the file is empty (size 0 or only comments and blank lines) 0 is returned and buf is set to NULL

Warning
if lloadfilefd can't get a lock on the input file (e.g. currently opened for writing by another process) the file is treated as non existent

The input file will be closed before this function returns.

striptab == 2 means: the only whitespace in a non-comment line may be immediately before the line break, in any amount.

buf is always in a sane state when this function returns: either it is NULL or a valid buffer, in the latter case the return value will be >0.

int loadintfd ( int  fd,
unsigned long *  result,
const unsigned long  def 
)

read a control file containing a single integer

Parameters
fdfile descriptor to read from (will be closed)
resultvalue will be stored here
defdefault value if file does not exist
Return values
0on success
-1on error, parse errors in the file will set errno to EINVAL
int loadlistfd ( int  fd,
char ***  bufa,
checkfunc  cf 
)

read a list from config file and validate entries

Parameters
fdfile descriptor to read from (is closed on exit!)
bufaarray to be build from buf (memory will be malloced)
cffunction to check if an entry is valid or NULL if not to
Return values
0on success
-1on error

If the file does not exist or has no content *bufa will be set to NULL and 0 is returned.

size_t loadoneliner ( int  base,
const char *  filename,
char **  buf,
const int  optional 
)

read a configuration file that only may contain one line

Parameters
basedescriptor of a file descriptor serving as base for relative paths
filenamedon't know what this can ever mean ;)
bufthe buffer where the contents of the file will go, memory will be malloced
optionalif set to 0 write an error message to syslog if the file does not exist
Returns
length of the string
Return values
-1on error
size_t loadonelinerfd ( int  fd,
char **  buf 
)

read a configuration file that only may contain one line

Parameters
fdopened file descriptor
bufthe buffer where the contents of the file will go, memory will be malloced
Returns
length of the string
Return values
-1on error

fd will be closed.

Variable Documentation

int controldir_fd = -1

descriptor of the control directory