示例#1
0
int
main (int argc, char **argv)
{
  int ret;
  int c;

  FILE *pidfile;
  struct utsname sysinfo;

  machine_type machine;

  while ((c = getopt(argc, argv, "fdv")) != -1)
    {
      switch (c)
	{
	  case 'f':
	    console = 1;
	    break;

	  case 'd':
	    debug = 1;
	    console = 1;
	    break;

	  case 'v':
	    printf("pommed v" M_VERSION " Apple laptops hotkeys handler\n");
	    printf("Copyright (C) 2006-2011 Julien BLACHE <*****@*****.**>\n");

	    exit(0);
	    break;

	  default:
	    usage();

	    exit(1);
	    break;
	}
    }

  if (geteuid() != 0)
    {
      logmsg(LOG_ERR, "pommed needs root privileges to operate");

      exit(1);
    }

  if (!console)
    {
      openlog("pommed", LOG_PID, LOG_DAEMON);
    }

  logmsg(LOG_INFO, "pommed v" M_VERSION " Apple laptops hotkeys handler");
  logmsg(LOG_INFO, "Copyright (C) 2006-2011 Julien BLACHE <*****@*****.**>");

  /* Load our configuration */
  ret = config_load();
  if (ret < 0)
    {
      exit(1);
    }

  /* Identify the machine we're running on */
  machine = check_machine();
  switch (machine)
    {
      case MACHINE_MAC_UNKNOWN:
	logmsg(LOG_ERR, "Unknown Apple machine");

	exit(1);
	break;

      case MACHINE_UNKNOWN:
	logmsg(LOG_ERR, "Unknown non-Apple machine");

	exit(1);
	break;

      case MACHINE_ERROR:
	exit(1);
	break;

      default:
	if (machine < MACHINE_LAST)
	  {
#ifdef __powerpc__
	    mops = &pb_mops[machine];
#else
	    mops = &mb_mops[machine];
#endif /* __powerpc__ */
	  }
	break;
    }

  /* Runtime sanity check: catch errors in the mb_mops and pb_mops arrays */
  if (mops->type != machine)
    {
      logmsg(LOG_ERR, "machine_ops mismatch: expected %d, found %d", machine, mops->type);

      exit(1);
    }

  if (debug)
    {
      ret = uname(&sysinfo);

      if (ret < 0)
	logmsg(LOG_ERR, "uname() failed: %s", strerror(errno));
      else
	logdebug("System: %s %s %s\n", sysinfo.sysname, sysinfo.release, sysinfo.machine);
    }

  ret = evloop_init();
  if (ret < 0)
    {
      logmsg(LOG_ERR, "Event loop initialization failed");
      exit (1);
    }

  ret = mops->lcd_backlight_probe();
  if (ret < 0)
    {
      logmsg(LOG_ERR, "LCD backlight probe failed, check debug output");

      exit(1);
    }

  ret = evdev_init();
  if (ret < 1)
    {
      logmsg(LOG_ERR, "No suitable event devices found");

      exit(1);
    }

  kbd_backlight_init();

  ret = audio_init();
  if (ret < 0)
    {
      logmsg(LOG_WARNING, "Audio initialization failed, audio support disabled");
    }

  ret = mbpdbus_init();
  if (ret < 0)
    {
      logmsg(LOG_WARNING, "Could not connect to DBus system bus");
    }

  power_init();

  if (!console)
    {
      /*
       * Detach from the console
       */
      if (daemon(0, 0) != 0)
	{
	  logmsg(LOG_ERR, "daemon() failed: %s", strerror(errno));

	  evdev_cleanup();

	  exit(1);
	}
    }

  pidfile = fopen(PIDFILE, "w");
  if (pidfile == NULL)
    {
      logmsg(LOG_WARNING, "Could not open pidfile %s: %s", PIDFILE, strerror(errno));

      evdev_cleanup();

      exit(1);
    }
  fprintf(pidfile, "%d\n", getpid());
  fclose(pidfile);

  /* Spawn the beep thread */
  beep_init();

  signal(SIGINT, sig_int_term_handler);
  signal(SIGTERM, sig_int_term_handler);


  do
    {
      ret = evloop_iteration();
    }
  while (ret >= 0);

  evdev_cleanup();

  beep_cleanup();

  mbpdbus_cleanup();

  kbd_backlight_cleanup();

  power_cleanup();

  evloop_cleanup();

  config_cleanup();

  logmsg(LOG_INFO, "Exiting");

  if (!console)
    closelog();

  unlink(PIDFILE);

  return 0;
}
示例#2
0
int main(int argc, char **argv)
{
	

	//#setup#
	//data_collector is responsible for processing 
	//any files specified in the user-specified options.
	set_error_file(stderr);


	int temp_status = temp_init();
	if(temp_status != 0)
	{
		die("error %d: temperature sensor initialization", 
			temp_status);
	}

	if(INTERVAL < MIN_INTERVAL)
	{
		die("error: INTERVAL %d is less than %d\n", 
			(int)INTERVAL,	(int)MIN_INTERVAL);
	}

//	sigemptyset(&mask_chld);
//	sigaddset(&mask_chld, SIGCHLD);

	//if(signal(SIGCHLD, child_terminated) == SIG_ERR)
	//{
	//	die("setting SIGCHLD handler failed\n");
	//}

	options_populate(argc, argv);

	//Determine the proper error file ASAP.
	//Otherwise keep the default, stderr, set above.
	if(options_opt.error_file != NULL)
	{
		if(!options_opt.append_error && 
			(access(options_opt.output_file, F_OK) != -1))
		{
			die("Error file %s already exists and -a not specified\n", 
				options_opt.error_file);
		}

		//Open in append mode. If the file doesn't exist,
		//it will be created.
		set_error_file(fopen(options_opt.error_file, "a"));
	}
		
	timeout_ms = options_opt.timeout_sec*1000;
		
	//##open file##
	//If an output file is unspecified by the user, output
	//to stdout.
	if(options_opt.output_file != NULL)
	{
		if(access(options_opt.output_file, F_OK) != -1)
		{
			die("File %s already exists\n", options_opt.output_file);
		}

		output_file_handle = fopen(options_opt.output_file, "w");
	}
	else
	{
		output_file_handle = stdout;
	}

	start_power();

	#ifdef COLLECT_PWS
	init_pws();
	start_pws();	
	#endif 
	//signal typically returns a pointer to the previous
	//signal handler, but can also error
	//if(signal(SIGALRM, collect) == SIG_ERR)
	//{
	//	fprintf(stderr, "error: could not set SIGALRM signal handler\n");
	//	exit(1);
	//}

	//#debug#
	collect_data();
	


		


	
	//#loop#
//	alarm(INTERVAL);
//	pause();

	//#cleanup#
	fclose(output_file_handle);
	temp_cleanup();
	power_cleanup();
	delete_options();

	#ifdef COLLECT_PWS
	pws_cleanup();
	#endif

}