Esempio n. 1
0
void main_task
   (
      uint_32 initial_data
   )
{
   ENTRY_STRUCT entry;
   _mqx_uint    result;
   _mqx_uint    i;
   uchar        c;

   /* Create the log component */
   result = _log_create_component();
   if (result != MQX_OK) {
      printf("Main task: _log_create_component failed");
      _task_block();  
   }

   /* Create a log */
   result = _log_create(MY_LOG,
      10 * (sizeof(ENTRY_STRUCT)/sizeof(_mqx_uint)), 0);
   if (result != MQX_OK) {
      printf("Main task: _log_create failed");   
      _task_block();  
   }

   /* Write the data to the log */   
   printf("Please type in 10 characters:\n");
   for (i = 0; i < 10; i++) {
      c = getchar();
      result = _log_write(MY_LOG, 2, (_mqx_uint)c, i);
      if (result != MQX_OK) {
         printf("Main task: _log_write failed");   
      }
   }

   /* Read data from the log */
   printf("\nLog contains:\n");
   while (_log_read(MY_LOG, LOG_READ_OLDEST_AND_DELETE, 2,
      (LOG_ENTRY_STRUCT_PTR)&entry) == MQX_OK)
   {
      printf("Time: %lu.%03u%03u, c=%c, i=%u\n",
         entry.HEADER.SECONDS,
         (_mqx_uint)entry.HEADER.MILLISECONDS,
         (_mqx_uint)entry.HEADER.MICROSECONDS,
         (uchar)entry.C & 0xff,
         entry.I);
   }

   /* Destroy the log */
   _log_destroy(MY_LOG);

   _task_block();

}
Esempio n. 2
0
int main(int argc, char *argv[]) {
	int status = 0;

	_log_init();

	_config_default_init();

	_cmdline_process(argc, argv);

	if (setregid(config.gid, config.gid) < 0)
		_failure("setregid");

	if (setreuid(config.uid, config.uid) < 0)
		_failure("setreuid");

	if (_signal_init() < 0)
		_failure("_signal_init");

	if (_daemonize() < 0)
		_failure("_daemonize");

	for (;;) {
		if (config.flags & CONFIG_FL_PIDF_CREATE)
			_file_pid_create(config.pidf_name);

		/* Make sure that we receive a valid status value... otherwise jump out of here. */
		if ((status = _bexec(config.binary, config.args)) == -1) {
			log_warn("main(): _bexec(): Unable to execute binary '%s': %s.\n", config.binary, strerror(errno));
			break;
		}

		/* Log process termination */
		log_info("main(): _bexec(): Execution of '%s' terminated (Exit status: %d).\n", config.binary, WEXITSTATUS(status));

		/* Log any signals that caused the termination */
		if (WIFSIGNALED(status))
			log_info("main(): Child process \'%s\' was terminated due to unhandled signal %d.\n", config.binary, WTERMSIG(status));

		/* Check if the exit status refers to a bad runtime state */
		if (WEXITSTATUS(status) == PROCESS_EXIT_STATUS_CUSTOM_BAD_RUNTIME_OR_CONFIG) {
			log_info("main(): Child process \'%s\' won't be restarted due to runtime or configuration errors.\n", config.binary);

			/* If so, do not attempt to restart the process, even if explicitly requested
			 * by command line options.
			 */
			break;
		}

		if (config.flags & CONFIG_FL_PROC_RSTIGN) {
			log_info("main(): Restarting child \'%s\' regardless of exit status code.\n", config.binary);

			/* Restart child regardless of signals and exit codes */
			_main_loop_restart_prepare();

			continue;
		} else if ((config.flags & CONFIG_FL_PROC_RSTUSIG) && WIFSIGNALED(status) && (WTERMSIG(status) != SIGKILL) && (WTERMSIG(status) != SIGQUIT)) {
			log_info("main(): Restarting child \'%s\' since its termination was caused by an unhandled signal other than SIGKILL and SIGQUIT. (Termination signal was %d)\n", config.binary, WTERMSIG(status));

			/* Restart child if any unhandled signal other than KILL and QUIT caused its
			 * termination
			 */
			_main_loop_restart_prepare();

			continue;
		} else if ((config.flags & CONFIG_FL_PROC_RESTART) && WEXITSTATUS(status)) {
			log_info("main(): Restarting child \'%s\' due to a non-zero exit status code.", config.binary);

			/* Restart child is exit status code isn't zero */
			_main_loop_restart_prepare();

			continue;
		}

		break;
	}

	if (status < 0)
		_failure("_bexec");

	if (config.flags & CONFIG_FL_PIDF_CREATE) {
		if (unlink(config.pidf_name) < 0)
			_failure("unlink");
	}

	_config_destroy();

	_log_destroy();

	return 0;
}