/**
@brief Become a proper daemon.
+ @param callback Ptr to a function. From parent, called with child PID as first argument, and the next argument to *this* function as the second argument. From child, called with -1, -1 (yes pid_t is signed)
+ @param callback_arg An integer argument passed as the second argument to the callback function from the parent process post-fork()
@return 0 if successful, or -1 if not.
Call fork(). The parent exits. The child moves to the root directory, detaches from
the terminal, and redirects the standard streams (stdin, stdout, stderr) to /dev/null.
*/
-int daemonize( void ) {
- return daemonize_write_pid( NULL );
-}
-/**
- @brief Become a proper daemon, and report the childs process ID.
- @return 0 if successful, or -1 if not.
-
- Call fork(). If pidfile is not NULL, the parent writes the process ID of the child
- process to the specified file. Then it exits. The child moves to the root
- directory, detaches from the terminal, and redirects the standard streams (stdin,
- stdout, stderr) to /dev/null.
- */
-int daemonize_write_pid( FILE* pidfile ) {
+int daemonizeWithCallback( void (*callback)(pid_t, int), int callback_arg ) {
pid_t f = fork();
if (f == -1) {
} else if (f == 0) { // We're in the child now...
+ if( callback )
+ callback( -1, -1 );
+
// Change directories. Otherwise whatever directory
// we're in couldn't be deleted until the program
// terminated -- possibly causing some inconvenience.
return 0;
} else { // We're in the parent...
- if( pidfile ) {
- fprintf( pidfile, "%ld\n", (long) f );
- fclose( pidfile );
- }
+ if( callback )
+ callback( f, callback_arg );
+
_exit(0);
}
}
+/**
+ @brief Become a proper daemon.
+ @return 0 if successful, or -1 if not.
+
+ See daemonizeWithCallback() for details.
+*/
+int daemonize( void ) {
+ return daemonizeWithCallback( NULL, 0 );
+}
+
/**
@brief Determine whether a string represents a decimal integer.
return 0;
}
+size_t osrfXmlEscapingLength ( const char* str ) {
+ int extra = 0;
+ const char* s;
+ for (s = str; *s; ++s) {
+ switch (*s) {
+ case '>':
+ case '<':
+ extra += 3;
+ break;
+ case '&':
+ extra += 4;
+ break;
+ case '"':
+ extra += 11;
+ break;
+ default:
+ break;
+ }
+ }
+
+ return extra;
+}
+