29#include <linux/limits.h>
44 va_start(args, format);
46 vsnprintf(str,
sizeof str, format, args);
48 fprintf(stderr,
"%s", str);
62 memset(computer_name, 0, len);
65 DWORD bufCharCount = len;
66 if( GetComputerName( infoBuf, &bufCharCount ) )
68 memcpy( computer_name , infoBuf , len );
72 strcpy(computer_name,
"Unknown_Host_Name");
76 if( gethostname(computer_name, len) == -1 )
79 strcpy(computer_name,
"Unknown_Host_Name");
80 n_log(
LOG_ERR ,
"%s on gethostname !" , strerror( error ) );
97 if( ( file = fopen( filename,
"r" ) ) != NULL )
113 char strbuf[ PATH_MAX ] =
"" ;
117 unsigned long int bytes = GetModuleFileName( NULL, strbuf, PATH_MAX );
121 return strdup( dirname(strbuf ) );
124 char procbuf[ PATH_MAX ] =
"" ;
126 sprintf( procbuf,
"/proc/%d/exe", (
int)getpid() );
128 sprintf( procbuf,
"/proc/%d/path/a.out", (
int)getpid() );
130 int bytes = MIN( readlink( procbuf, strbuf, PATH_MAX ), PATH_MAX - 1 );
134 strbuf[ bytes ] =
'\0';
135 return strdup( dirname( strbuf ) );
138 fprintf( stderr,
"%s", strerror( error ) );
149 char strbuf[ PATH_MAX ] =
"" ;
152 unsigned long int bytes = GetModuleFileName( NULL, strbuf, PATH_MAX );
156 return strdup( basename(strbuf ) );
159 char procbuf[ PATH_MAX ] =
"" ;
161 sprintf( procbuf,
"/proc/%d/exe", (
int)getpid() );
163 sprintf( procbuf,
"/proc/%d/path/a.out", (
int)getpid() );
165 int bytes = MIN( readlink( procbuf, strbuf, PATH_MAX ), PATH_MAX - 1 );
169 strbuf[ bytes ] =
'\0';
170 return strdup( basename( strbuf ) );
173 fprintf( stderr,
"%s", strerror( error ) );
187int n_popen(
char *cmd,
int read_buf_size,
void **nstr_output,
int *ret )
195 char read_buf[ read_buf_size ] ;
197 fp = popen( cmd,
"r" );
201 n_log(
LOG_ERR,
"popen( %s ) returned NULL , %s (%d)", cmd, strerror( err ), err );
206 while( fgets( read_buf, read_buf_size, fp ) != NULL )
208 length = strlen( read_buf );
209 if( read_buf[ length - 1 ] ==
'\n' )
211 read_buf[ length - 1 ] =
'\0' ;
215 status = pclose( fp );
219 n_log(
LOG_ERR,
"pclose( %s ) returned -1 , %s (%d)", cmd, strerror( err ), err );
220 if( WIFEXITED( status ) )
222 n_log(
LOG_ERR,
"Child exited with RC=%d", WEXITSTATUS( status ) );
223 (*ret) = WEXITSTATUS( status );
227 (*ret) = WEXITSTATUS( status );
228 (*nstr_output) = output_pointer ;
241 int saved_errno = errno;
243 while(waitpid(-1, NULL, WNOHANG) > 0);
257 sigemptyset(&sa.sa_mask);
258 sa.sa_flags = SA_RESTART;
259 if (sigaction(SIGCHLD, &sa, NULL) == -1)
262 n_log(
LOG_ERR ,
"sigaction error: %s" , strerror( error ) );
275 extern char **environ;
276 for(
char **env = environ ; *env != 0 ; env++ )
278 n_log( loglevel ,
"env: %s" , *env );
305 n_log(
LOG_ERR,
"Error: unable to fork child: %s", strerror( error ) );
321 n_log(
LOG_ERR,
"Error: unable to set session leader with setsid(): %s", strerror( error ) );
333 signal(SIGCHLD, SIG_IGN);
349 n_log(
LOG_ERR,
"Error: unable to double fork child: %s", strerror( error ) );
368 if( chdir(
"/" ) < 0 )
371 n_log(
LOG_ERR,
"couldn't chdir() to '/': %s" , strerror( error ) );
379 for(
int x = 3 ; x < sysconf( _SC_OPEN_MAX ) ; x++ )
388 int fd = open(
"/dev/null",O_RDWR, 0);
392 dup2 (fd, STDIN_FILENO);
393 dup2 (fd, STDOUT_FILENO);
394 dup2 (fd, STDERR_FILENO);
402 n_log(
LOG_ERR,
"Failed to open /dev/null: %s" , strerror( errno ) );
417pid_t
system_nb(
const char *command,
int *infp,
int *outfp) {
421 int p_stdin[2] = { -1, -1 };
422 int p_stdout[2] = { -1, -1 };
427 if( pipe( p_stdin ) != 0 )
432 if( pipe( p_stdout ) != 0 )
468 dup2(p_stdout[1], 1);
470 execlp(
"sh",
"sh",
"-c", command, (
char *)NULL);
473 n_log(
LOG_ERR ,
"%s:%d: exec failed: %s", __FILE__, __LINE__, strerror( error ));
501 va_start (args, buf);
505 while((arg = va_arg(args,
int)))
521 char *result = NULL ;
528 while ((i > 0) && (path[i] !=
'.') && (path[i] !=
'/') && (path[i] !=
'\\')) {
531 if ((i > 0) && (i < n - 1) && (path[i] ==
'.') && (path[i - 1] !=
'/') && (path[i - 1] !=
'\\')) {
void log_environment(int loglevel)
log environment in syslog
#define N_DAEMON_NO_UMASK
daemonize flag: do not call umask( 0 )
char * get_prog_name(void)
get current program name
#define N_DAEMON_NO_DOUBLE_FORK
daemonize flag: do not double fork
void N_HIDE_STR(char *buf,...)
store a hidden version of a string
#define N_DAEMON_NO_SIGCHLD_IGN
daemonize flag: do not ignore SIGCHLD
#define N_DAEMON_NO_CHDIR
daemonize flag: do not call chdir("/")
#define __n_assert(__ptr, __ret)
macro to assert things
#define N_DAEMON_NO_CLOSE
daemonize flag: no descriptor close at all
#define N_DAEMON_NO_SIGCHLD_HANDLER
daemonize flag: do not use internal zombie SIGCHLD handler
#define N_DAEMON_NO_STD_REDIRECT
daemonize flag: just do not redirect stdin/out/err to /dev/null
int get_computer_name(char *computer_name, size_t len)
abort program with a text
#define N_DAEMON_NO_SETSID
daemonize flag: do not call setsid
void sigchld_handler(int sig)
Handles SIGCHLD issues when forking.
pid_t system_nb(const char *command, int *infp, int *outfp)
Non blocking system call.
char * n_get_file_extension(char path[])
get extension of path+filename
int n_popen(char *cmd, int read_buf_size, void **nstr_output, int *ret)
launch a command abd return output and status
char * get_prog_dir(void)
get current program running directory
int n_daemonize_ex(int mode)
Daemonize program.
void n_abort(char const *format,...)
abort program with a text
int sigchld_handler_installer()
install signal SIGCHLD handler to reap zombie processes
int n_daemonize(void)
Daemonize program.
int file_exist(const char *filename)
test if file exist and if it's readable
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
#define LOG_NOTICE
normal but significant condition
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
#define nstrprintf_cat(__nstr_var,...)
Macro to quickly allocate and sprintf and cat to a N_STR *.
A box including a string and his lenght.
Common headers and low-level hugly functions & define.
N_STR and string function declaration.