1 /* @(#) interface to utility functions for udpxy
3 * Copyright 2008-2011 Pavel V. Cherenkov (pcherenkov@gmail.com)
5 * This file is part of udpxy.
7 * udpxy is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * udpxy is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with udpxy. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef UTILH_UDPXY_200712181853
22 #define UTILH_UDPXY_200712181853
24 #include <sys/types.h>
33 /* write buffer to a file
36 save_buffer( const void* buf
, size_t len
, const char* filename
);
38 /* read text file into a buffer
42 txtf_read (const char* fpath
, char* dst
, size_t maxlen
, FILE* log
);
44 /* start process as a daemon
47 #define DZ_STDIO_OPEN 1 /* do not close STDIN, STDOUT, STDERR */
49 daemonize(int options
, FILE* log
);
52 /* multiplex error output to custom log
56 mperror( FILE* fp
, int err
, const char* format
, ... );
59 /* create and lock file with process's ID
62 make_pidfile( const char* fpath
, pid_t pid
, FILE* log
);
65 /* write path to application's pidfile into the the buffer
66 * (fail of destination directory is not writable)
69 set_pidfile( const char* appname
, int port
, char* buf
, size_t len
);
71 /* write buffer to designated socket/file
72 * return number of bytes read/written or one of the error
75 #define IO_ERR -1 /* generic error */
76 #define IO_BLK -2 /* operation timed out or would block */
78 write_buf( int fd
, const char* data
, const ssize_t len
, FILE* log
);
80 /* read data chunk of designated size into buffer
83 read_buf( int fd
, char* buf
, const ssize_t len
, FILE* log
);
85 /* output hex dump of a memory fragment
88 hex_dump( const char* msg
, const char* data
, size_t len
, FILE* log
);
90 /* check for expected size, complain if not matched
93 sizecheck( const char* msg
, ssize_t expct
, ssize_t len
,
94 FILE* log
, const char* func
);
96 /* check for a potential buffer overrun by
97 * evaluating target buffer and the portion
98 * of that buffer to be accessed
101 buf_overrun( const char* buf
, size_t buflen
,
102 size_t offset
, size_t dlen
,
106 /* write timestamp-prepended formatted message to file
109 tmfprintf( FILE* stream
, const char* format
, ... );
112 /* write timestamp-prepended message to file
115 tmfputs( const char* s
, FILE* stream
);
118 /* print out command-line
121 printcmdln( FILE* stream
, const char* msg
,
122 int argc
, char* const argv
[] );
125 /* convert timespec to time_t
127 * timespec format: [+|-]dd:hh24:mi:ss
129 * @return 0 if success, n>0 if parse error at position n,
133 a2time( const char* str
, time_t* t
, time_t from
);
136 /* convert ASCII size spec (positive) into numeric value
138 * num[modifier], where num is an ASCII representation of
139 * any positive integer and
140 * modifier ::= [Kb|K|Mb|M|Gb|G]
143 a2size( const char* str
, ssize_t
* pval
);
146 a2int64( const char* str
, int64_t* pval
);
148 /* returns asctime w/o CR character at the end
153 Zasctime( const struct tm
* tm
);
156 /* adjust nice value if needed
159 set_nice( int val
, FILE* log
);
162 /* check and report a deviation in values: n_was and n_is are not supposed
163 * to differ more than by delta
166 check_fragments( const char* action
, ssize_t total
, ssize_t n_was
, ssize_t n_is
,
167 ssize_t delta
, FILE* log
);
169 /* create timestamp string in YYYY-mm-dd HH24:MI:SS.MSEC from struct timeval
171 static const int32_t TVSTAMP_GMT
= 1;
174 mk_tvstamp( const struct timeval
* tv
, char* buf
, size_t* len
,
178 /* retrieve UNIX time value from given environment
179 * variable, otherwise return default */
181 get_timeval( const char* envar
, const time_t deflt
);
184 /* retrieve flag value as 1 = true, 0 = false
185 * from an environment variable set in the form
186 * of 0|1|'true'|'false'|'yes'|'no'
189 get_flagval( const char* envar
, const int deflt
);
191 /* retrieve LONG value from given environment
192 * variable, otherwise return default */
194 get_sizeval( const char* envar
, const ssize_t deflt
);
197 /* retrieve/reset string representation of pid,
200 get_pidstr( int reset
, const char* pfx
);
202 /* retrieve system info string
205 get_sysinfo (int* perr
);
211 #endif /* UTILH_UDPXY_200712181853 */