*/
#define _LGPL_SOURCE
-#include <stdlib.h>
+#include <arpa/inet.h>
#include <ctype.h>
+#include <inttypes.h>
#include <limits.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <signal.h>
#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <inttypes.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/wait.h>
#include <unistd.h>
#include <common/error.hpp>
* Spawn session daemon and tell
* it to signal us when ready.
*/
- execlp(pathname, "lttng-relayd", "-L", url, NULL);
+ execlp(pathname, "lttng-relayd", "-L", url, "--daemonize", NULL);
/* execlp only returns if error happened */
if (errno == ENOENT) {
ERR("No relayd found. Use --relayd-path.");
} else {
PERROR("execlp");
}
- kill(getppid(), SIGTERM); /* wake parent */
+ kill(getppid(), SIGTERM); /* wake parent */
exit(EXIT_FAILURE);
} else if (pid > 0) {
+ /*
+ * In daemon mode (--daemonize), lttng-relayd only exits when
+ * it's ready to accept commands.
+ */
+ for (;;) {
+ int status;
+ pid_t wait_pid_ret = waitpid(pid, &status, 0);
+
+ if (wait_pid_ret < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
+ PERROR("waitpid");
+ ret = -errno;
+ goto end;
+ }
+
+ if (WIFSIGNALED(status)) {
+ ERR("Relay daemon was killed by signal %d", WTERMSIG(status));
+ ret = -1;
+ goto end;
+ } else if (WIFEXITED(status)) {
+ DBG("Relay daemon terminated normally (exit status: %d)",
+ WEXITSTATUS(status));
+
+ if (WEXITSTATUS(status) != 0) {
+ ERR("Relay daemon terminated with an error (exit status: %d)",
+ WEXITSTATUS(status));
+ ret = -1;
+ goto end;
+ }
+ break;
+ }
+ }
+
goto end;
} else {
PERROR("fork");