示例#1
0
/*******************************************************************************
 *
 * Render backend context functions.
 *
 ******************************************************************************/
int
rb_create_context
  (struct mem_allocator* specific_allocator,
   struct rb_context** out_ctxt)
{
  struct mem_allocator* allocator = NULL;
  struct rb_context* ctxt = NULL;
  int err = 0;

  if(!out_ctxt)
    goto error;

  allocator = specific_allocator ? specific_allocator : &mem_default_allocator;
  ctxt = MEM_CALLOC(allocator, 1, sizeof(struct rb_context));
  if(!ctxt)
    goto error;
  ctxt->allocator = allocator;
  ref_init(&ctxt->ref);

  setup_config(&ctxt->config);

exit:
  if(ctxt)
    *out_ctxt = ctxt;
  return err;

error:
  if(ctxt) {
    RB(context_ref_put(ctxt));
    ctxt = NULL;
  }
  err = -1;
  goto exit;
}
示例#2
0
文件: main.c 项目: danfrincu/booth
static int loop(int type)
{
	void (*workfn) (int ci);
	void (*deadfn) (int ci);
	int rv, i;

	rv = setup_config(type);
	if (rv < 0)
		goto fail;

	rv = setup_timer();
	if (rv < 0)
		goto fail;

	rv = setup_transport();
	if (rv < 0)
		goto fail;

	rv = setup_ticket();
	if (rv < 0)
		goto fail;

	rv = setup_listener(BOOTHC_SOCK_PATH);
	if (rv < 0)
		goto fail;
	client_add(rv, process_listener, NULL);

        while (1) {
                rv = poll(pollfd, client_maxi + 1, poll_timeout);
                if (rv == -1 && errno == EINTR)
                        continue;
                if (rv < 0) {
                        log_error("poll errno %d", errno);
			goto fail;
                }

                for (i = 0; i <= client_maxi; i++) {
                        if (client[i].fd < 0)
                                continue;
                        if (pollfd[i].revents & POLLIN) {
                                workfn = client[i].workfn;
                                if (workfn)
                                        workfn(i);
                        }
                        if (pollfd[i].revents &
			    (POLLERR | POLLHUP | POLLNVAL)) {
                                deadfn = client[i].deadfn;
                                if (deadfn)
                                        deadfn(i);
                        }
                }

		process_timerlist();
	}

	return 0;

fail:
	return -1;
}
示例#3
0
文件: main.c 项目: inouekazu/booth
static int do_client(void)
{
	int rv;

	rv = setup_config(CLIENT);
	if (rv < 0) {
		log_error("cannot read config");
		goto out;
	}

	switch (cl.op) {
	case CMD_LIST:
	case CMD_PEERS:
		rv = query_get_string_answer(cl.op);
		break;

	case CMD_GRANT:
	case CMD_REVOKE:
		rv = do_command(cl.op);
		break;
	}

out:
	return rv;
}
示例#4
0
文件: main.c 项目: aspiers/booth
static int setup(int type)
{
	int rv;

	rv = setup_config(type);
	if (rv < 0)
		goto fail;

	rv = setup_timer();
	if (rv < 0)
		goto fail;

	rv = setup_transport();
	if (rv < 0)
		goto fail;

	rv = setup_ticket();
	if (rv < 0)
		goto fail;

	rv = setup_listener(BOOTHC_SOCK_PATH);
	if (rv < 0)
		goto fail;
	client_add(rv, process_listener, NULL);

	return 0;

fail:
	return -1;
}
示例#5
0
/******************************************************************************
* Name  main
* 
* This utility, as currently written, repacks objects in the trash directory 
* due to mismatches between the chunk count specified in the trash file xattr
* and the actual number of files found in trash for the specified packed 
* object.
*
* The method for determining whether objects need to repacked is to read the 
* the file specied by the -d option.  This is typically the 
* tmp_packed_log file from garbage collection.  This file lists the packed 
* object name and associated files for the packed object.  It only lists those
* objects that could NOT reconcile with the file counts.  The format of this 
* file is:
*
* OBJECT_NAME  FILE_NAME  EXPECTED_FILE_COUNT  HOST BUCKED  OBJID   
*
* This utility reads the file and finds all files for the associated object
* The number of files found will be less than EXPECTED_FILE_COUNT (post xattr
* chunk count).  It then proceeds to read in the file objects and rewrite
* them back to a new object with offsets re-defined.  Xattrs are updated
* to reflect the new object, chunk_count, and offsets 
*
******************************************************************************/
int main(int argc, char **argv){
   int c;
   char *fnameP = NULL;
   char *outfile = NULL;
   //char *ns = NULL;

   while ((c=getopt(argc,argv,"d:o:h")) != EOF) {
      switch(c) {
         case 'd': fnameP = optarg; break;
         //case 'n': ns = optarg; break;
         case 'o': outfile = optarg; break;
         case 'h': print_usage();
         default:
            exit(-1);
      }
   }
   if (fnameP == NULL || outfile == NULL) {
      print_usage();
      exit(-1);
   }

   if ( setup_config() == -1 ) {
      fprintf(stderr,"Error:  Initializing Configs and Aws failed, quitting!!\n");
      exit(-1);
   }


   File_Handles file_info;
   File_Handles *file_status = &file_info;

   // open associated log file and packed log file
   if ((file_status->outfd = fopen(outfile,"w")) == NULL){
      fprintf(stderr, "Failed to open %s\n", outfile);
      exit(1);
   }
   strcpy(file_status->packed_log, fnameP);



   //MarFS_Namespace* namespace;
   //namespace = find_namespace_by_name(ns);
        //MarFS_Repo* repo = namespace->iwrite_repo;
        // Find the correct repo - the one with the largest range
   //MarFS_Repo* repo = find_repo_by_range(namespace, (size_t)-1);


   repack_objects *objs = NULL;
   find_repack_objects(file_status, &objs);
   pack_objects(file_status, objs);
   update_meta(file_status, objs);
   free_objects(objs);
   return 0;
}
示例#6
0
文件: turing_main.c 项目: dvdhrm/uni
int main(int argc, char **argv)
{
	int ret;
	struct turing_machine *mach;
	struct turing_band *band;
	struct config *conf = NULL;
	int i;
	bool verbose = false;

	if (argc <= 1) {
		printf("Busy-Beaver: %s [3456] [v]\n", argv[0]);
		printf("  Please pass as argument a number to specify the\n");
		printf("  turing machine. Pass 'v' for verbose mode.\n");
		return EXIT_FAILURE;
	}

	for (i = 1; i < argc; ++i) {
		if (!strcmp(argv[i], "3"))
			conf = &bb3_conf;
		if (!strcmp(argv[i], "4"))
			conf = &bb4_conf;
		if (!strcmp(argv[i], "5"))
			conf = &bb5_conf;
		if (!strcmp(argv[i], "6"))
			conf = &bb6_conf;
		if (!strcmp(argv[i], "v"))
			verbose = true;
	}

	if (!conf)
		conf = &bb3_conf;

	ret = setup_config(&mach, &band, conf);
	if (ret)
		goto err_mach;

	turing_machine_print(mach);

	printf("\nSimulating turing machine:\n\n");
	ret = turing_machine_simulate_limited(mach, band, 0, verbose ? 2 : 1);
	if (ret) {
		printf("Cannot simulate turing machine: %d\n", ret);
		goto err_mach;
	}
	printf("\nTuring simulation completed\n");

err_mach:
	turing_machine_unref(mach);
	turing_band_free(band);
	return abs(ret);
}
示例#7
0
文件: rtc_linux.c 项目: SuperQ/chrony
int
RTC_Linux_Initialise(void)
{
  rtc_sec = MallocArray(time_t, MAX_SAMPLES);
  rtc_trim = MallocArray(double, MAX_SAMPLES);
  system_times = MallocArray(struct timeval, MAX_SAMPLES);

  /* Setup details depending on configuration options */
  setup_config();

  /* In case it didn't get done by pre-init */
  coefs_file_name = CNF_GetRtcFile();

  /* Try to open device */

  fd = open (CNF_GetRtcDevice(), O_RDWR);
  if (fd < 0) {
    LOG(LOGS_ERR, LOGF_RtcLinux, "Could not open RTC device %s : %s",
        CNF_GetRtcDevice(), strerror(errno));
    return 0;
  }

  /* Close on exec */
  UTI_FdSetCloexec(fd);

  n_samples = 0;
  n_samples_since_regression = 0;
  n_runs = 0;
  coefs_valid = 0;

  measurement_period = LOWEST_MEASUREMENT_PERIOD;

  operating_mode = OM_NORMAL;

  /* Register file handler */
  SCH_AddInputFileHandler(fd, read_from_device, NULL);

  /* Register slew handler */
  LCL_AddParameterChangeHandler(slew_samples, NULL);

  logfileid = CNF_GetLogRtc() ? LOG_FileOpen("rtc",
      "   Date (UTC) Time   RTC fast (s) Val   Est fast (s)   Slope (ppm)  Ns  Nr Meas")
    : -1;
  return 1;
}
示例#8
0
文件: main.c 项目: inouekazu/booth
static int do_attr(void)
{
	int rv = -1;

	rv = setup_config(GEOSTORE);
	if (rv < 0) {
		log_error("cannot read config");
		goto out;
	}

	/* We don't check for existence of ticket, so that asking can be
	 * done without local configuration, too.
	 * Although, that means that the UDP port has to be specified, too. */
	if (!cl.attr_msg.attr.tkt_id[0]) {
		/* If the loaded configuration has only a single ticket defined, use that. */
		if (booth_conf->ticket_count == 1) {
			strncpy(cl.attr_msg.attr.tkt_id, booth_conf->ticket[0].name,
				sizeof(cl.attr_msg.attr.tkt_id));
		} else {
			rv = 1;
			log_error("No ticket given.");
			goto out;
		}
	}

	switch (cl.op) {
	case ATTR_LIST:
	case ATTR_GET:
		rv = query_get_string_answer(cl.op);
		break;

	case ATTR_SET:
	case ATTR_DEL:
		rv = do_attr_command(cl.op);
		break;
	}

out:
	return rv;
}
示例#9
0
/** read playback file */
static struct replay_scenario*
setup_playback(const char* filename, int* pass_argc, char* pass_argv[])
{
    struct replay_scenario* scen = NULL;
    int lineno = 0;

    if(filename) {
        FILE *in = fopen(filename, "rb");
        if(!in) {
            perror(filename);
            exit(1);
        }
        setup_config(in, &lineno, pass_argc, pass_argv);
        scen = replay_scenario_read(in, filename, &lineno);
        fclose(in);
        if(!scen)
            fatal_exit("Could not read: %s", filename);
    }
    else fatal_exit("need a playback file (-p)");
    log_info("Scenario: %s", scen->title);
    return scen;
}
示例#10
0
int Read()
{
	VRAM   = init_VRAM();			AllClr_VRAM( VRAM   );
	char * VRAM_A = init_VRAM();	AllClr_VRAM( VRAM_A );
	char * VRAM_B = init_VRAM();	AllClr_VRAM( VRAM_B );
	int total_page , page = 0 , i ,last_key_pressed=0;
	char buff[1000] , statue_bar[100];
	int auto_down = 0 , _auto = 0 , mark = 1 ;
	int user_contrast = *p_contrast;
	long auto_time = 0 ;
	int msg_utf8 = 0;
	auto_down = _auto = 0 ;
	strcpy( txtname , fname );
	TXT = fopen( txtname , "rb" ) ;
	fseek(TXT , 0 , SEEK_END);
	filesize = ftell( TXT );
	
	total_page = divid();
	
	memset( buff , 0x0 , 1000 );
	
	load_mark( &page , total_page , 1 );

	fseek( TXT , pagepos[page] , SEEK_SET );
	fread( buff , pagepos[ page+1 ] - pagepos[ page ] , 1 , TXT );
	AllFill_VRAM(VRAM_A,cl_bg);
	PrintChStr( VRAM_A , 0 , 0 , buff , cl_fg , cl_bg );

	PutDisp_DDVRAM( VRAM_A );
	auto_down = 0 ;
	while( 1 )
	{
		if(isKeyPressed(KEY_NSPIRE_SCRATCHPAD))
		{
			wait_no_key_pressed();
			if(msg_utf8==0)
				i = show_msgbox_3b( "提示" , "调用API必须保证文本为UTF-8编码,也就是此时用nNovel无法正常阅读,要继续吗?" , "确定" , "取消" ,"确定且不再提示" );
			else
				i = 1;
			if( i == 3 )
			{
				i = 1;
				msg_utf8 = 1;
			}
			if( i == 1 )
			{
				wait_no_key_pressed();
				show_msgbox( "本页内容:" , buff );
			}
		}
		if(isKeyPressed(KEY_NSPIRE_MULTIPLY))
		{
			if( is_cx && (*p_contrast)< 225)
				(*p_contrast)++;
			else if( (!is_cx) && (*p_contrast) < 0xc0 )
				(*p_contrast)++;
		}
		else if(isKeyPressed(KEY_NSPIRE_DIVIDE))
		{
			if( is_cx && (*p_contrast)> 1 )
				(*p_contrast)--;
			else if( (!is_cx) && (*p_contrast) >50 )
				(*p_contrast)--;
		}
		else if(isKeyPressed(KEY_NSPIRE_TAB))
		{
			i = num_input(VRAM , 30 , 30 , "  ����ҳ��" );
			if( i == -1) continue;
			else if( i == 0 ){ page = 0;}
			else if( i > total_page){ page = total_page;}
			else page = i - 1;
			memset( buff , 0x0 , 1000 );
			fseek( TXT , pagepos[page] , SEEK_SET );
			fread( buff , pagepos[ page +1 ] - pagepos[ page ] , 1 , TXT );
			AllFill_VRAM(VRAM_A,cl_bg);
			PrintChStr( VRAM_A , 0 , 0 , buff , cl_fg , cl_bg );
			PutDisp_DDVRAM( VRAM_A );
			wait_no_key_pressed();
		}
		else if(isKeyPressed(KEY_NSPIRE_DOC))
		{
			save_mark( page , total_page , 0 );
			wait_no_key_pressed();
		}
		else if(isKeyPressed(KEY_NSPIRE_MENU))
		{
			load_mark( &page , total_page , 0 );
			memset( buff , 0x0 , 1000 );
			fseek( TXT , pagepos[page] , SEEK_SET );
			fread( buff , pagepos[ page +1 ] - pagepos[ page ] , 1 , TXT );
			AllFill_VRAM(VRAM_B,cl_bg);
			PrintChStr( VRAM_A , 0 , 0 , buff , cl_fg , cl_bg );
			PutDisp_DDVRAM( VRAM_A );
			wait_no_key_pressed();
		}
		else if(isKeyPressed(KEY_NSPIRE_SHIFT))
		{
			if(cl_bg == 15)
			{
				cl_fg = 15;
				cl_bg = 0;
			}
			else
			{
				cl_fg = 0;
				cl_bg = 15;
			}
			memset( buff , 0x0 , 1000 );
			fseek( TXT , pagepos[page] , SEEK_SET );
			fread( buff , pagepos[ page +1 ] - pagepos[ page ] , 1 , TXT );
			AllFill_VRAM(VRAM_A,cl_bg);
			PrintChStr( VRAM_A , 0 , 0 , buff , cl_fg , cl_bg );
			PutDisp_DDVRAM( VRAM_A );
			wait_no_key_pressed();
		}
		
		if((*p_RTC - auto_time)%(_auto) != 0)
			mark = 0;
		if( ( isKeyPressed(KEY_NSPIRE_DOWN ) || isKeyPressed(KEY_NSPIRE_MINUS )|| auto_down == 1 )&& page < total_page )
		{
			auto_down = 0;
			page++;
			memset( buff , 0x0 , 1000 );
			fseek( TXT , pagepos[page] , SEEK_SET );
			fread( buff , pagepos[ page +1 ] - pagepos[ page ] , 1 , TXT );
			if( cl_bg != 15 )
				AllFill_VRAM(VRAM_B,cl_bg);
			else
				AllClr_VRAM(VRAM_B);
			PrintChStr( VRAM_B , 0 , 0 , buff , cl_fg , cl_bg );
			if( config.show_page_slide == 1 )
				slide_down(statue_bar , VRAM_A , VRAM_B , 1 );
			else
			{
				Fill_Rect_VRAM( VRAM_B , 0 , 224 , 320 , 240 , 0 );
				PrintChStr( VRAM_B , 0 , 224 , statue_bar , 15 , 0 );
				PutDisp_DDVRAM(VRAM_B);
				for( i=0 ; i<=FIRST_KEY_WAIT ; i++ )
				{
					if((!isKeyPressed(KEY_NSPIRE_DOWN )|| isKeyPressed(KEY_NSPIRE_MINUS )))
						break;
					if(last_key_pressed)
						break;
					sleep(1);
					if( i == FIRST_KEY_WAIT )
						last_key_pressed = 1;
				}
			}
			memcpy(VRAM_A,VRAM_B,SCREEN_BYTES_SIZE);
		}
		else if( isKeyPressed(KEY_NSPIRE_UP )|| isKeyPressed(KEY_NSPIRE_PLUS ) )
		{
			if( page == 0 )
				continue;
			page --;
			memset( buff , 0x0 , 1000 );
			fseek( TXT , pagepos[page] , SEEK_SET );
			fread( buff , pagepos[ page +1 ] - pagepos[ page ], 1 , TXT );
			AllFill_VRAM(VRAM_B,cl_bg);
			PrintChStr( VRAM_B , 0 , 0 , buff , cl_fg , cl_bg );
			if( config.show_page_slide == 1 )
				slide_up( statue_bar , VRAM_B , VRAM_A , 1 );
			else
			{
				Fill_Rect_VRAM( VRAM_B , 0 , 224 , 320 , 240 , 0 );
				PrintChStr( VRAM_B , 0 , 224 , statue_bar , 15 , 0 );
				PutDisp_DDVRAM(VRAM_B);
				for( i=0 ; i<=FIRST_KEY_WAIT ; i++ )
				{
					if((!isKeyPressed(KEY_NSPIRE_UP )|| isKeyPressed(KEY_NSPIRE_PLUS )))
						break;
					if(last_key_pressed)
						break;
					sleep(1);
					if( i == FIRST_KEY_WAIT )
						last_key_pressed = 1;
				}
			}
			memcpy(VRAM_A,VRAM_B,SCREEN_BYTES_SIZE);
		}
		else
			last_key_pressed = 0;
		memcpy(VRAM,VRAM_A,SCREEN_BYTES_SIZE);
		Fill_Rect_VRAM( VRAM , 0 , 224 , 320 , 240 , 0 );
		if( config.show_time )
		{
			update_time();
			if( config.show_second )
				sprintf(statue_bar , " %d%d:%d%d:%d%d     %5d/%5d   %4s", time.hour/10 , time.hour%10 , time.minute/10, time.minute%10 , time.second/10 , time.second%10 , page +1 , total_page +1 , _auto == 0?"":"Auto");
			else
				sprintf(statue_bar , " %d%d:%d%d        %5d/%5d   %4s", time.hour/10 , time.hour%10 , time.minute/10, time.minute%10 , page +1 , total_page +1 ,_auto == 0?"":"Auto");
		}
		else 
		{
			sprintf(statue_bar , "              %5d/%5d", page +1 , total_page +1 );
		}
		PrintChStr( VRAM , 0 , 224 , statue_bar , 15 , 0 );
//		DrawMiniString_VRAM(VRAM , 0 , 228 , statue_bar , 15 , 0 );
		PutDisp_DDVRAM(VRAM);
			
		if( ! config.show_time )
		{
			if( !(isKeyPressed(KEY_NSPIRE_UP)||isKeyPressed(KEY_NSPIRE_DOWN)) )
				wait_key_pressed();
		}
		
		if( isKeyPressed(KEY_NSPIRE_ESC))
		{
			if(isKeyPressed(KEY_NSPIRE_CTRL))
				goto exit;
			switch(menu(VRAM,10,10,"    �˵�\nҳ����ת(Tab)\n�Զ���ҳ\n��ȡ��ǩ(menu)\n������ǩ(doc)\n����\nҹ��ģʽ(shift)\n�����ǩ��ҳ���˳�\n����API�鿴��ҳ(��ǩ��)\n�˳�(ctrl+esc)\n","\n������λ����ת������λ��\n����/�ر��Զ���ҳ\n����ǩ�ļ��ж�ȡ����\n����ǩ�ļ���д�����\n�鿴���޸�����\n����ҹ��ģʽ(��ɫ)\n��������ɵ�������ǩ�ͷ�ҳ�Ľ���ļ�\n����MsgBox����ʾ��ҳ����\n�˳�����\n") )
			{
				case 1://ҳ����ת
				{
					i = num_input(SCREEN_BASE_ADDRESS , 30 , 30 , "  ����ҳ��" );
					if( i == -1) break;
					else if( i == 0 ){ page = 0;}
					else if( i > total_page){ page = total_page;}
					else page = i - 1;
					memset( buff , 0x0 , 1000 );
					fseek( TXT , pagepos[page] , SEEK_SET );
					fread( buff , pagepos[ page +1 ] - pagepos[ page ] , 1 , TXT );
					AllFill_VRAM(VRAM_A,cl_bg);
					PrintChStr( VRAM_A , 0 , 0 , buff , cl_fg , cl_bg );
					PutDisp_DDVRAM( VRAM_A );
					break;
				}
				case 2://�Զ���ҳ
				{
					if( _auto != 0 )
					{
						_auto = 0 ;
						mark = 1;
						break;
					}
					_auto = num_input( SCREEN_BASE_ADDRESS , 30 , 30 , "��������" );
					if(_auto == -1)
						_auto = 0;
					auto_time = *p_RTC;
					
					break;
				}
				case 3: //��ȡ��ǩ
				{
					load_mark( &page , total_page , 0 );
					memset( buff , 0x0 , 1000 );
					fseek( TXT , pagepos[page] , SEEK_SET );
					fread( buff , pagepos[ page +1 ] - pagepos[ page ] , 1 , TXT );
					AllFill_VRAM(VRAM_B,cl_bg);
					PrintChStr( VRAM_A , 0 , 0 , buff , cl_fg , cl_bg );
					PutDisp_DDVRAM( VRAM_A );
					break;
				}
				case 4: save_mark( page , total_page , 0 );break;//������ǩ
				case 5:	//����
				{
					setup_config();
					memset( buff , 0x0 , 1000 );
					fseek( TXT , pagepos[page] , SEEK_SET );
					fread( buff , pagepos[ page +1 ] - pagepos[ page ] , 1 , TXT );
					AllFill_VRAM(VRAM_A,cl_bg);
					PrintChStr( VRAM_A , 0 , 0 , buff , cl_fg , cl_bg );
					PutDisp_DDVRAM( VRAM_A );
					break;
				}
				case 6: //��ɫ
				{
					if(cl_bg == 15)
					{
						cl_fg = 15;
						cl_bg = 0;
					}
					else
					{
						cl_fg = 0;
						cl_bg = 15;
					}
					memset( buff , 0x0 , 1000 );
					fseek( TXT , pagepos[page] , SEEK_SET );
					fread( buff , pagepos[ page +1 ] - pagepos[ page ] , 1 , TXT );
					AllFill_VRAM(VRAM_A,cl_bg);
					PrintChStr( VRAM_A , 0 , 0 , buff , cl_fg , cl_bg );
					PutDisp_DDVRAM( VRAM_A );
					break;
				}
				case 7: //�����ǩ
				{
					char path[ 50 ];
					for( i = 0 ; i < strlen(txtname) - 8 ; i ++ )
						path[i] = txtname[i] ;
					path[ i ] = 0;
					strcat( path , ".div" );
					remove(path);
					for( i = 0 ; i < strlen(txtname) - 8 ; i ++ )
						path[i] = txtname[i] ;
					path[ i ] = 0;
					strcat( path , ".sav" );
					remove(path);
					goto exit2;
				}
				case 8://API
				{
					wait_no_key_pressed();
					if(msg_utf8==0)
						i = show_msgbox_3b( "提示" , "调用API必须保证文本为UTF-8编码,也就是此时用nNovel无法正常阅读,要继续吗?" , "确定" , "取消" ,"确定且不再提示" );
					else
						i = 1;
					if( i == 3 )
					{
						i = 1;
						msg_utf8 = 1;
					}
					if( i == 1 )
					{
						wait_no_key_pressed();
						show_msgbox( "本页内容:" , buff );
						break;
					}
					else
						break;
				}
				case 9: goto exit;//�˳�
				default :wait_no_key_pressed();break;
			}
			wait_no_key_pressed();
		}
		if( config.auto_save )
			if( page%config.auto_save_time==0 )
				save_mark( page , total_page , 1 );
		if( (*p_RTC - auto_time)%(_auto) == 0 && mark == 0)
		{
			mark = 1 ;
			auto_down = 1 ;
			auto_time = *p_RTC;
		}
	}
	exit:
	save_mark( page , total_page , 1 );
	exit2:
	* p_contrast = user_contrast;
	fclose( TXT );
	close_VRAM( VRAM_A );close_VRAM( VRAM_B );
	close_VRAM( VRAM );
	return 0;
}
示例#11
0
文件: main.c 项目: nvanfleet/Coffeed
int main(int argc, char **argv)
{
	// Setup
	setup_config(argc,argv);
	setup_state();
	setup_sigs();

	// Objects	
	logger.setup();
	server.setup();
	sensor.setup();
	heater.setup();

    // Thread
	if(pthread_create( &serverThread, NULL, serverPoll, NULL) != 0)
		logger.fail("Server thread creation failure");

	float chosenSetPoint;
	int sensorResult = 0;
	clock_t nextLoop;
	
	conf.update = 1000/conf.update;
	
	timeTick();
	nextLoop = state.time + conf.update;
    
 	logger.info("Starting up Coffeed");
 	
	while(state.run)
	{
		// Time update
		timeTick();

		if(state.time >= nextLoop)
		{
			// Get sensor data
			sensorResult = sensor.update();
		
			// If active choose set point
			if(state.active)
			{
				if(state.brewmode == TRUE)
					chosenSetPoint = conf.brewPoint;
				else
					chosenSetPoint = conf.steamPoint;
								
				// Sensor fault
				if(!sensorResult)
				{
					logger.fail("Sensor read failure");
					state.active = FALSE;
					heater.off();
				}
				// Tuning mode
				else if(state.tuning != FALSE)
				{
					// Begin
					if(state.tuning == 2)
					{
						state.tuning = TRUE;
						
						heater.setPower(50);
						aTune.cancel();
						//aTune.setNoiseBand(1);
						//aTune.setOutputStep(50);
						//aTune.setLookbackSec(20);
						//aTune.setControlType(1);
						
						logger.info("Autotuning BEGIN %f %f %f",conf.pgain,conf.igain,conf.dgain);
					}
					// While (1) we want to tune + (2) update is not done
					else if(state.tuning == TRUE && aTune.update() == FALSE)
					{
						logger.info("tuning");
					}
					// Tuning complete because state.tuning = TRUE but aTune == TRUE
					else
					{
						logger.info("tuning successful am I right?");
						// Done
						conf.pgain = aTune.getKp();
						conf.igain = aTune.getKi();
						conf.dgain = aTune.getKd();
						
						logger.info("Autotuning FINISH %f %f %f",conf.pgain,conf.igain,conf.dgain);
						
						state.tuning = FALSE;
					}					
				}
				// Regular control
				else
				{					
					// PID result
					state.pidResult = pid.result(chosenSetPoint, state.tempPoint);
					
					// Set Heater Duty
					heater.setPower( state.pidResult );
					
					// Log
					logger.debug("PID Result %d temperature %f setP %f brewP %f steamP %f", state.pidResult, state.tempPoint, chosenSetPoint, conf.brewPoint, conf.steamPoint);
				}
			}
			// Inactive system
			else
			{
				// Tuning can't happen in a inactive system
				if(state.tuning != FALSE)
				{
					state.tuning = FALSE;
					aTune.cancel();
				}
				
				if(state.brewmode)
					state.brewmode = TRUE;
				
				chosenSetPoint = 0;
				heater.off();
			}

			// Get Heater Duty
			// This should be centralized to the heater
			state.power = heater.getPower();
			
			// Schedule the next loop after the whole operation	
			nextLoop = state.time + conf.update;
		}
		else
		{
			// Sleep until we need you
			usleep(1000 * (nextLoop - state.time));
		}
	}
	
    logger.info("Shutting down Coffeed");
}
示例#12
0
文件: main.c 项目: Ferritt1975/booth
static int do_status(int type)
{
	pid_t pid;
	int rv, lock_fd, ret;
	const char *reason = NULL;
	char lockfile_data[1024], *cp;


	ret = PCMK_OCF_NOT_RUNNING;
	/* TODO: query all, and return quit only if it's _cleanly_ not
	 * running, ie. _neither_ of port/lockfile/process is available?
	 *
	 * Currently a single failure says "not running", even if "only" the
	 * lockfile has been removed. */

	rv = setup_config(type);
	if (rv) {
		reason = "Error reading configuration.";
		ret = PCMK_OCF_UNKNOWN_ERROR;
		goto quit;
	}


	if (!local) {
		reason = "No Service IP active here.";
		goto quit;
	}


	rv = _lockfile(O_RDWR, &lock_fd, &pid);
	if (rv == 0) {
		reason = "PID file not locked.";
		goto quit;
	}
	if (lock_fd == -1) {
		reason = "No PID file.";
		goto quit;
	}

	if (pid) {
		fprintf(stdout, "booth_lockpid=%d ", pid);
		fflush(stdout);
	}


	rv = read(lock_fd, lockfile_data, sizeof(lockfile_data) - 1);
	if (rv < 4) {
		reason = "Cannot read lockfile data.";
		ret = PCMK_LSB_UNKNOWN_ERROR;
		goto quit;

	}
	lockfile_data[rv] = 0;

	if (lock_fd != -1)
		close(lock_fd);


	/* Make sure it's only a single line */
	cp = strchr(lockfile_data, '\r');
	if (cp)
		*cp = 0;
	cp = strchr(lockfile_data, '\n');
	if (cp)
		*cp = 0;



	rv = setup_tcp_listener(1);
	if (rv == 0) {
		reason = "TCP port not in use.";
		goto quit;
	}


	fprintf(stdout, "booth_lockfile='%s' %s\n",
			cl.lockfile, lockfile_data);
	if (daemonize)
		fprintf(stderr, "Booth at %s port %d seems to be running.\n",
				local->addr_string, booth_conf->port);
	return 0;


quit:
	log_debug("not running: %s", reason);
	/* Ie. "DEBUG" */
	if (daemonize)
		fprintf(stderr, "not running: %s\n", reason);
	return ret;
}
示例#13
0
文件: rtc_linux.c 项目: SuperQ/chrony
int
RTC_Linux_TimePreInit(time_t driftfile_time)
{
  int fd, status;
  struct rtc_time rtc_raw, rtc_raw_retry;
  struct tm rtc_tm;
  time_t rtc_t;
  double accumulated_error, sys_offset;
  struct timeval new_sys_time, old_sys_time;

  coefs_file_name = CNF_GetRtcFile();

  setup_config();
  read_coefs_from_file();

  fd = open(CNF_GetRtcDevice(), O_RDONLY);

  if (fd < 0) {
    return 0; /* Can't open it, and won't be able to later */
  }

  /* Retry reading the rtc until both read attempts give the same sec value.
     This way the race condition is prevented that the RTC has updated itself
     during the first read operation. */
  do {
    status = ioctl(fd, RTC_RD_TIME, &rtc_raw);
    if (status >= 0) {
      status = ioctl(fd, RTC_RD_TIME, &rtc_raw_retry);
    }
  } while (status >= 0 && rtc_raw.tm_sec != rtc_raw_retry.tm_sec);

  /* Read system clock */
  LCL_ReadCookedTime(&old_sys_time, NULL);

  close(fd);

  if (status >= 0) {
    /* Convert to seconds since 1970 */
    rtc_tm.tm_sec = rtc_raw.tm_sec;
    rtc_tm.tm_min = rtc_raw.tm_min;
    rtc_tm.tm_hour = rtc_raw.tm_hour;
    rtc_tm.tm_mday = rtc_raw.tm_mday;
    rtc_tm.tm_mon = rtc_raw.tm_mon;
    rtc_tm.tm_year = rtc_raw.tm_year;
    
    rtc_t = t_from_rtc(&rtc_tm);

    if (rtc_t != (time_t)(-1)) {

      /* Work out approximatation to correct time (to about the
         nearest second) */
      if (valid_coefs_from_file) {
        accumulated_error = file_ref_offset +
          (rtc_t - file_ref_time) * 1.0e-6 * file_rate_ppm;
      } else {
        accumulated_error = 0.0;
      }

      /* Correct time */

      new_sys_time.tv_sec = rtc_t;
      /* Average error in the RTC reading */
      new_sys_time.tv_usec = 500000;

      UTI_AddDoubleToTimeval(&new_sys_time, -accumulated_error, &new_sys_time);

      if (new_sys_time.tv_sec < driftfile_time) {
        LOG(LOGS_WARN, LOGF_RtcLinux, "RTC time before last driftfile modification (ignored)");
        return 0;
      }

      UTI_DiffTimevalsToDouble(&sys_offset, &old_sys_time, &new_sys_time);

      /* Set system time only if the step is larger than 1 second */
      if (fabs(sys_offset) >= 1.0) {
        if (LCL_ApplyStepOffset(sys_offset))
          LOG(LOGS_INFO, LOGF_RtcLinux, "System time set from RTC");
      }
    } else {
      return 0;
    }
  } else {
    return 0;
  }

  return 1;
}
示例#14
0
文件: main.c 项目: Ferritt1975/booth
static int do_server(int type)
{
	int rv = -1;
	static char log_ent[128] = DAEMON_NAME "-";

	rv = setup_config(type);
	if (rv < 0)
		return rv;


	if (!local) {
		log_error("Cannot find myself in the configuration.");
		exit(EXIT_FAILURE);
	}

	if (!daemonize) {
		if (daemon(0, 0) < 0) {
			perror("daemon error");
			exit(EXIT_FAILURE);
		}
	}

	/* The lockfile must be written to _after_ the call to daemon(), so
	 * that the lockfile contains the pid of the daemon, not the parent. */
	lock_fd = create_lockfile();
	if (lock_fd < 0)
		return lock_fd;

	atexit(server_exit);

	strcat(log_ent, type_to_string(local->type));
	cl_log_set_entity(log_ent);
	cl_log_enable_stderr(enable_stderr ? TRUE : FALSE);
	cl_log_set_facility(HA_LOG_FACILITY);
	cl_inherit_logging_environment(0);

	log_info("BOOTH %s %s daemon is starting",
			type_to_string(local->type), RELEASE_STR);

	signal(SIGUSR1, (__sighandler_t)tickets_log_info);
	signal(SIGTERM, (__sighandler_t)sig_exit_handler);
	signal(SIGINT, (__sighandler_t)sig_exit_handler);

	set_scheduler();
	set_oom_adj(-16);
	set_proc_title("%s %s %s for [%s]:%d",
			DAEMON_NAME,
			cl.configfile,
			type_to_string(local->type),
			local->addr_string,
			booth_conf->port);

	rv = limit_this_process();
	if (rv)
		return rv;

	if (cl_enable_coredumps(TRUE) < 0){
		cl_log(LOG_ERR, "enabling core dump failed");
	}
	cl_cdtocoredir();
	prctl(PR_SET_DUMPABLE, (unsigned long)TRUE, 0UL, 0UL, 0UL);

	rv = loop(lock_fd);

	return rv;
}
示例#15
0
void
RTC_Linux_TimePreInit(void)
{
  int fd, status;
  struct rtc_time rtc_raw;
  struct tm rtc_tm;
  time_t rtc_t, estimated_correct_rtc_t;
  long interval;
  double accumulated_error = 0.0;
  struct timeval new_sys_time, old_sys_time;

  coefs_file_name = CNF_GetRtcFile();

  setup_config();
  read_coefs_from_file();

  fd = open(CNF_GetRtcDevice(), O_RDONLY);

  if (fd < 0) {
    return; /* Can't open it, and won't be able to later */
  }

  status = ioctl(fd, RTC_RD_TIME, &rtc_raw);

  if (status >= 0) {
    /* Convert to seconds since 1970 */
    rtc_tm.tm_sec = rtc_raw.tm_sec;
    rtc_tm.tm_min = rtc_raw.tm_min;
    rtc_tm.tm_hour = rtc_raw.tm_hour;
    rtc_tm.tm_mday = rtc_raw.tm_mday;
    rtc_tm.tm_mon = rtc_raw.tm_mon;
    rtc_tm.tm_year = rtc_raw.tm_year;
    
    rtc_t = t_from_rtc(&rtc_tm);

    if (rtc_t != (time_t)(-1)) {

      /* Work out approximatation to correct time (to about the
         nearest second) */
      if (valid_coefs_from_file) {
        interval = rtc_t - file_ref_time;
        accumulated_error = file_ref_offset + (double)(interval) * 1.0e-6 * file_rate_ppm;

        /* Correct time */
        estimated_correct_rtc_t = rtc_t - (long)(0.5 + accumulated_error);
      } else {
        estimated_correct_rtc_t = rtc_t - (long)(0.5 + accumulated_error);
      }

      new_sys_time.tv_sec = estimated_correct_rtc_t;
      new_sys_time.tv_usec = 0;

      /* Set system time only if the step is larger than 1 second */
      if (!(gettimeofday(&old_sys_time, NULL) < 0) &&
          (old_sys_time.tv_sec - new_sys_time.tv_sec > 1 ||
           old_sys_time.tv_sec - new_sys_time.tv_sec < -1)) {

        LOG(LOGS_INFO, LOGF_RtcLinux, "Set system time, error in RTC = %f",
            accumulated_error);

        /* Tough luck if this fails */
        if (settimeofday(&new_sys_time, NULL) < 0) {
          LOG(LOGS_WARN, LOGF_RtcLinux, "Could not settimeofday");
        }
      }
    } else {
      LOG(LOGS_WARN, LOGF_RtcLinux, "Could not convert RTC reading to seconds since 1/1/1970");
    }
  }

  close(fd);
}
示例#16
0
int
main(int argc, char *argv[])
{
	/*
	 * options with no short version return a low integer, the rest return
	 * their short version value
	 */
	static struct option long_options[] = {
		{"pgdata", required_argument, NULL, 'D'},
		{"help", no_argument, NULL, '?'},
		{"version", no_argument, NULL, 'V'},
		{"debug", no_argument, NULL, 'd'},
		{"show", no_argument, NULL, 's'},
		{"noclean", no_argument, NULL, 'n'},
		{NULL, 0, NULL, 0}
	};

	int			c, ret;
	int			option_index;
	char	   *effective_user;
	char		bin_dir[MAXPGPATH];
	char	   *pg_data_native;
	bool		node_type_specified = false;

	progname = get_progname(argv[0]);
	set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("initgtm"));

	if (argc > 1)
	{
		if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
		{
		usage(progname);
			exit(0);
		}
		if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
		{
			puts("initgtm (Postgres-XL) " PGXC_VERSION);
			exit(0);
		}
	}

	/* process command-line options */

	while ((c = getopt_long(argc, argv, "dD:nsZ:", long_options, &option_index)) != -1)
	{
		switch (c)
		{
			case 'D':
				pg_data = xstrdup(optarg);
				break;
			case 'd':
				debug = true;
				printf(_("Running in debug mode.\n"));
				break;
			case 'n':
				noclean = true;
				printf(_("Running in noclean mode.  Mistakes will not be cleaned up.\n"));
				break;
			case 's':
				show_setting = true;
				break;
			case 'Z':
				if (strcmp(xstrdup(optarg), "gtm") == 0)
					is_gtm = true;
				else if (strcmp(xstrdup(optarg), "gtm_proxy") == 0)
					is_gtm = false;
				else
				{
					fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
							progname);
					exit(1);
				}
				node_type_specified = true;
				break;
			default:
				/* getopt_long already emitted a complaint */
				fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
						progname);
				exit(1);
		}
	}

	/* Non-option argument specifies data directory */
	if (optind < argc)
	{
		pg_data = xstrdup(argv[optind]);
		optind++;
	}

	if (optind < argc)
	{
		fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
				progname, argv[optind + 1]);
		fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
				progname);
		exit(1);
	}

	/* Check on definition of GTM data folder */
	if (strlen(pg_data) == 0)
	{
		fprintf(stderr,
				_("%s: no data directory specified\n"
				  "You must identify the directory where the data for this GTM system\n"
				  "will reside.  Do this with either the invocation option -D or the\n"
				  "environment variable PGDATA.\n"),
				progname);
		exit(1);
	}

	if (!node_type_specified)
	{
		fprintf(stderr,
				_("%s: no node type specified\n"
				  "You must identify the node type chosen for initialization.\n"
				  "Do this with the invocation option -Z by choosing \"gtm\" or"
				  "\"gtm_proxy\"\n"),
				progname);
		exit(1);
	}

	pg_data_native = pg_data;
	canonicalize_path(pg_data);

#ifdef WIN32

	/*
	 * Before we execute another program, make sure that we are running with a
	 * restricted token. If not, re-execute ourselves with one.
	 */

	if ((restrict_env = getenv("PG_RESTRICT_EXEC")) == NULL
		|| strcmp(restrict_env, "1") != 0)
	{
		PROCESS_INFORMATION pi;
		char	   *cmdline;

		ZeroMemory(&pi, sizeof(pi));

		cmdline = xstrdup(GetCommandLine());

		putenv("PG_RESTRICT_EXEC=1");

		if (!CreateRestrictedProcess(cmdline, &pi))
		{
			fprintf(stderr, "Failed to re-exec with restricted token: %lu.\n", GetLastError());
		}
		else
		{
			/*
			 * Successfully re-execed. Now wait for child process to capture
			 * exitcode.
			 */
			DWORD		x;

			CloseHandle(pi.hThread);
			WaitForSingleObject(pi.hProcess, INFINITE);

			if (!GetExitCodeProcess(pi.hProcess, &x))
			{
				fprintf(stderr, "Failed to get exit code from subprocess: %lu\n", GetLastError());
				exit(1);
			}
			exit(x);
		}
	}
#endif

	/* Like for initdb, check if a valid version of Postgres is running */
	if ((ret = find_other_exec(argv[0], "postgres", PG_BACKEND_VERSIONSTR,
							   backend_exec)) < 0)
	{
		char        full_path[MAXPGPATH];

		if (find_my_exec(argv[0], full_path) < 0)
			strlcpy(full_path, progname, sizeof(full_path));

		if (ret == -1)
			fprintf(stderr,
					_("The program \"postgres\" is needed by %s "
					  "but was not found in the\n"
					  "same directory as \"%s\".\n"
					  "Check your installation.\n"),
					progname, full_path);
		else
			fprintf(stderr,
					_("The program \"postgres\" was found by \"%s\"\n"
					  "but was not the same version as %s.\n"
					  "Check your installation.\n"),
					full_path, progname);
		exit(1);
	}

	/* store binary directory */
	strcpy(bin_path, backend_exec);
	*last_dir_separator(bin_path) = '\0';
	canonicalize_path(bin_path);

	if (!share_path)
	{
		share_path = pg_malloc(MAXPGPATH);
		get_share_path(backend_exec, share_path);
	}
	else if (!is_absolute_path(share_path))
	{
		fprintf(stderr, _("%s: input file location must be an absolute path\n"), progname);
		exit(1);
	}

	canonicalize_path(share_path);

	effective_user = get_id();

	/* Take into account GTM and GTM-proxy cases */
	if (is_gtm)
		set_input(&conf_file, "gtm.conf.sample");
	else
		set_input(&conf_file, "gtm_proxy.conf.sample");

	if (show_setting || debug)
	{
		fprintf(stderr,
				"VERSION=%s\n"
				"GTMDATA=%s\nshare_path=%s\nGTMPATH=%s\n"
				"GTM_CONF_SAMPLE=%s\n",
				PGXC_VERSION,
				pg_data, share_path, bin_path,
				conf_file);
		if (show_setting)
			exit(0);
	}

	check_input(conf_file);

	printf(_("The files belonging to this GTM system will be owned "
			 "by user \"%s\".\n"
			 "This user must also own the server process.\n\n"),
		   effective_user);

	printf("\n");

	umask(S_IRWXG | S_IRWXO);

	/*
	 * now we are starting to do real work, trap signals so we can clean up
	 */

	/* some of these are not valid on Windows */
#ifdef SIGHUP
	pqsignal(SIGHUP, trapsig);
#endif
#ifdef SIGINT
	pqsignal(SIGINT, trapsig);
#endif
#ifdef SIGQUIT
	pqsignal(SIGQUIT, trapsig);
#endif
#ifdef SIGTERM
	pqsignal(SIGTERM, trapsig);
#endif

	/* Ignore SIGPIPE when writing to backend, so we can clean up */
#ifdef SIGPIPE
	pqsignal(SIGPIPE, SIG_IGN);
#endif

	switch (pg_check_dir(pg_data))
	{
		case 0:
			/* PGDATA not there, must create it */
			printf(_("creating directory %s ... "),
				   pg_data);
			fflush(stdout);

			if (!mkdatadir(NULL))
				exit_nicely();
			else
				check_ok();

			made_new_pgdata = true;
			break;

		case 1:
			/* Present but empty, fix permissions and use it */
			printf(_("fixing permissions on existing directory %s ... "),
				   pg_data);
			fflush(stdout);

			if (chmod(pg_data, S_IRWXU) != 0)
			{
				fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
						progname, pg_data, strerror(errno));
				exit_nicely();
			}
			else
				check_ok();

			found_existing_pgdata = true;
			break;

		case 2:
			/* Present and not empty */
			fprintf(stderr,
					_("%s: directory \"%s\" exists but is not empty\n"),
					progname, pg_data);
			fprintf(stderr,
					_("If you want to create a new GTM system, either remove or empty\n"
					  "the directory \"%s\" or run %s\n"
					  "with an argument other than \"%s\".\n"),
					pg_data, progname, pg_data);
			exit(1);			/* no further message needed */

		default:
			/* Trouble accessing directory */
			fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
					progname, pg_data, strerror(errno));
			exit_nicely();
	}

	/* Select suitable configuration settings */
	set_null_conf();

	/* Now create all the text config files */
	setup_config();

	/* Get directory specification used to start this executable */
	strcpy(bin_dir, argv[0]);
	get_parent_directory(bin_dir);

	if (is_gtm)
		printf(_("\nSuccess. You can now start the GTM server using:\n\n"
				 "    %s%s%sgtm%s -D %s%s%s\n"
				 "or\n"
				 "    %s%s%sgtm_ctl%s -Z gtm -D %s%s%s -l logfile start\n\n"),
		   QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH,
			   QUOTE_PATH, pg_data_native, QUOTE_PATH,
		   QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH,
			   QUOTE_PATH, pg_data_native, QUOTE_PATH);
	else
		printf(_("\nSuccess. You can now start the GTM proxy server using:\n\n"
				 "    %s%s%sgtm_proxy%s -D %s%s%s\n"
				 "or\n"
				 "    %s%s%sgtm_ctl%s -Z gtm_proxy -D %s%s%s -l logfile start\n\n"),
		   QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH,
			   QUOTE_PATH, pg_data_native, QUOTE_PATH,
		   QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH,
			   QUOTE_PATH, pg_data_native, QUOTE_PATH);

	return 0;
}
示例#17
0
文件: main.c 项目: inouekazu/booth
static int do_status(int type)
{
	pid_t pid;
	int rv, status_lock_fd, ret;
	const char *reason = NULL;
	char lockfile_data[1024], *cp;


	ret = PCMK_OCF_NOT_RUNNING;

	rv = setup_config(type);
	if (rv) {
		reason = "Error reading configuration.";
		ret = PCMK_OCF_UNKNOWN_ERROR;
		goto quit;
	}


	if (!local) {
		reason = "No Service IP active here.";
		goto quit;
	}


	rv = _lockfile(O_RDWR, &status_lock_fd, &pid);
	if (status_lock_fd == -1) {
		reason = "No PID file.";
		goto quit;
	}
	if (rv == 0) {
		close(status_lock_fd);
		reason = "PID file not locked.";
		goto quit;
	}
	if (pid) {
		fprintf(stdout, "booth_lockpid=%d ", pid);
		fflush(stdout);
	}

	rv = read(status_lock_fd, lockfile_data, sizeof(lockfile_data) - 1);
	if (rv < 4) {
		close(status_lock_fd);
		reason = "Cannot read lockfile data.";
		ret = PCMK_LSB_UNKNOWN_ERROR;
		goto quit;
	}
	lockfile_data[rv] = 0;

	close(status_lock_fd);


	/* Make sure it's only a single line */
	cp = strchr(lockfile_data, '\r');
	if (cp)
		*cp = 0;
	cp = strchr(lockfile_data, '\n');
	if (cp)
		*cp = 0;



	rv = setup_tcp_listener(1);
	if (rv == 0) {
		reason = "TCP port not in use.";
		goto quit;
	}


	fprintf(stdout, "booth_lockfile='%s' %s\n",
			cl.lockfile, lockfile_data);
	if (!daemonize)
		fprintf(stderr, "Booth at %s port %d seems to be running.\n",
				local->addr_string, booth_conf->port);
	return 0;


quit:
	log_debug("not running: %s", reason);
	/* Ie. "DEBUG" */
	if (!daemonize)
		fprintf(stderr, "not running: %s\n", reason);
	return ret;
}
示例#18
0
文件: main.c 项目: inouekazu/booth
static int do_server(int type)
{
	int rv = -1;
	static char log_ent[128] = DAEMON_NAME "-";

	rv = setup_config(type);
	if (rv < 0)
		return rv;

	if (!local) {
		log_error("Cannot find myself in the configuration.");
		exit(EXIT_FAILURE);
	}

	if (daemonize) {
		if (daemon(0, 0) < 0) {
			perror("daemon error");
			exit(EXIT_FAILURE);
		}
	}

	/* The lockfile must be written to _after_ the call to daemon(), so
	 * that the lockfile contains the pid of the daemon, not the parent. */
	lock_fd = create_lockfile();
	if (lock_fd < 0)
		return lock_fd;

	atexit(server_exit);

	strcat(log_ent, type_to_string(local->type));
	cl_log_set_entity(log_ent);
	cl_log_enable_stderr(enable_stderr ? TRUE : FALSE);
	cl_log_set_facility(HA_LOG_FACILITY);
	cl_inherit_logging_environment(0);

	log_info("BOOTH %s %s daemon is starting",
			type_to_string(local->type), RELEASE_STR);

	signal(SIGUSR1, (__sighandler_t)tickets_log_info);
	signal(SIGTERM, (__sighandler_t)sig_exit_handler);
	signal(SIGINT, (__sighandler_t)sig_exit_handler);
	/* we'll handle errors there and then */
	signal(SIGPIPE, SIG_IGN);

	set_scheduler();
	/* we don't want to be killed by the OOM-killer */
	if (set_procfs_val("/proc/self/oom_score_adj", "-999"))
		(void)set_procfs_val("/proc/self/oom_adj", "-16");
	set_proc_title("%s %s %s for [%s]:%d",
			DAEMON_NAME,
			cl.configfile,
			type_to_string(local->type),
			local->addr_string,
			booth_conf->port);

	rv = limit_this_process();
	if (rv)
		return rv;

#ifdef COREDUMP_NURSING
	if (cl_enable_coredumps(TRUE) < 0){
		log_error("enabling core dump failed");
	}
	cl_cdtocoredir();
	prctl(PR_SET_DUMPABLE, (unsigned long)TRUE, 0UL, 0UL, 0UL);
#else
	if (chdir(BOOTH_CORE_DIR) < 0) {
		log_error("cannot change working directory to %s", BOOTH_CORE_DIR);
	}
#endif

	signal(SIGCHLD, (__sighandler_t)wait_child);
	rv = loop(lock_fd);

	return rv;
}
int main( int argc, char *argv[] ) {
    config conf;
    setup_config(&conf, argc, argv);
    init_progress(conf.progress_width, conf.nsteps, conf.progress_disabled);

    TYPE dx = 20.f;
    TYPE dt = 0.002f;

    // compute the pitch for perfect coalescing
    size_t dimx = conf.nx + 2*conf.radius;
    size_t dimy = conf.ny + 2*conf.radius;
    size_t nbytes = dimx * dimy * sizeof(TYPE);

    if (conf.verbose) {
        printf("x = %zu, y = %zu\n", dimx, dimy);
        printf("nsteps = %d\n", conf.nsteps);
        printf("radius = %d\n", conf.radius);
    }

    TYPE c_coeff[NUM_COEFF];
    TYPE *curr = (TYPE *)malloc(nbytes);
    TYPE *next = (TYPE *)malloc(nbytes);
    TYPE *vsq  = (TYPE *)malloc(nbytes);
    if (curr == NULL || next == NULL || vsq == NULL) {
        fprintf(stderr, "Allocations failed\n");
        return 1;
    }

    config_sources(&conf.srcs, &conf.nsrcs, conf.nx, conf.ny, conf.nsteps);
    TYPE **srcs = sample_sources(conf.srcs, conf.nsrcs, conf.nsteps, dt);

    init_data(curr, next, vsq, c_coeff, dimx, dimy, dx, dt);

    double start = seconds();
    for (int step = 0; step < conf.nsteps; step++) {
        for (int src = 0; src < conf.nsrcs; src++) {
            if (conf.srcs[src].t > step) continue;
            int src_offset = POINT_OFFSET(conf.srcs[src].x, conf.srcs[src].y,
                    dimx, conf.radius);
            curr[src_offset] = srcs[src][step];
        }

        fwd(next, curr, vsq, c_coeff, conf.nx, conf.ny, dimx, dimy,
                conf.radius);

        TYPE *tmp = next;
        next = curr;
        curr = tmp;

        update_progress(step + 1);
    }
    double elapsed_s = seconds() - start;

    finish_progress();

    float point_rate = (float)conf.nx * conf.ny / (elapsed_s / conf.nsteps);
    fprintf(stderr, "iso_r4_2x:   %8.10f s total, %8.10f s/step, %8.2f Mcells/s/step\n",
            elapsed_s, elapsed_s / conf.nsteps, point_rate / 1000000.f);

    if (conf.save_text) {
        save_text(curr, dimx, dimy, conf.ny, conf.nx, "snap.text", conf.radius);
    }

    free(curr);
    free(next);
    free(vsq);
    for (int i = 0; i < conf.nsrcs; i++) {
        free(srcs[i]);
    }
    free(srcs);
    
    return 0;
}
示例#20
0
int main(int argc, char **argv) {
	int rc;

	setup_config(argc, argv);

	if (config.pull == NULL) {
		usage(argv[0]);
		errx(EXIT_FAILURE, "no pull option set");
	}

	if (config.pub == NULL) {
		usage(argv[0]);
		errx(EXIT_FAILURE, "no pub option set");
	}

	void *context = zmq_init(1);
	assert(context);

	void *pull_socket = zmq_socket(context, ZMQ_PULL);
	assert(pull_socket);

	void *pub_socket = zmq_socket(context, ZMQ_PUB);
	assert(pub_socket);

	if (config.hwm > 0) {
		rc = zmq_setsockopt(pub_socket, ZMQ_HWM, &config.hwm, sizeof(config.hwm));
		if (rc != 0)
			errx(EXIT_FAILURE, "failed to set high-water mark \"%"PRIu64"\"", config.hwm);
	}

	if (config.swap > 0) {
		if (config.hwm < 1)
			errx(EXIT_FAILURE, "hwm must be set to use swap");

		rc = zmq_setsockopt(pub_socket, ZMQ_SWAP, &config.swap, sizeof(config.swap));

		if (rc != 0)
			errx(EXIT_FAILURE, "failed to set swap size \"%"PRIu64"\"", config.swap);
	}

	rc = zmq_bind(pull_socket, config.pull);
	if (rc != 0)
		errx(EXIT_FAILURE, "failed to bind pull endpoint \"%s\"", config.pull);

	rc = zmq_bind(pub_socket, config.pub);
	if (rc != 0)
		errx(EXIT_FAILURE, "failed to bind pub endpoint \"%s\"", config.pub);

	s_catch_signals();

	int64_t more;
	size_t more_size;

	zmq_msg_t msg;
	zmq_msg_init(&msg);

	while (!s_interrupted) {
		if (zmq_recv(pull_socket, &msg, 0)) continue;

		more_size = sizeof(more);
		zmq_getsockopt(pull_socket, ZMQ_RCVMORE, &more, &more_size);
		zmq_send(pub_socket, &msg, more ? ZMQ_SNDMORE : 0);
	}

	zmq_close(pull_socket);
	zmq_close(pub_socket);
	zmq_term(context);

	printf("\n");

	return EXIT_SUCCESS;
}