Пример #1
0
main(int argc, char **argv)
{
    int ret;
    pq_session session;

    ret = pq_init(&session);
    if(ret)
        printf("pq_init failed.\n");

    char conn_str[] = "host=localhost dbname=pagila";
    ret = pq_connect(&session, conn_str, strlen(conn_str));
    if(ret)
        printf("pq_connect failed.\n");
    pq_check_connection(&session);

    ret = pq_execute(&session, "select current_date;");
    if(ret)
        printf("pq_execute failed.\n");

    ret = pq_fetch(&session);
    if(ret)
        printf("pq_finish failed.\n");

    pq_finish(&session);
    pq_end(&session);
}
Пример #2
0
int main(int argc, char** argv){
	CONFIG cfg;

	cfg_init(&cfg);

	//read commandline args
	if(!arg_parse(&cfg, argc-1, argv+1)){
		return -1;
	}

	//read config file
	if(!cfg.cfg_file){
		fprintf(stderr, "No config file supplied\n");
		return -1;
	}
	if(!cfg_read(&cfg, cfg.cfg_file)){
		cfg_free(&cfg);
		return -1;
	}

	//if not using pgpass, ask for database password
	if(!cfg.db.use_pgpass){
		cfg.db.pass=calloc(sizeof(char),MAX_PASSWORD_LENGTH+1);
		if(!cfg.db.pass){
			fprintf(stderr, "Failed to allocate memory\n");
			cfg_free(&cfg);
			return -1;
		}
		fprintf(stderr, "DB Password: "******"\fWaiting...");
		ask_password(cfg.db.pass, MAX_PASSWORD_LENGTH);
	}

	//check for sane config
	if(!cfg_sane(&cfg)){
		cfg_free(&cfg);
		return -1;
	}

	//connect to database if persistent
	if(cfg.db.persist_connection){
		if(!pq_connect(&(cfg.db))){
			cfg_free(&cfg);
			return -1;
		}
		if(cfg.verbosity>2){
			fprintf(stderr, "Database connection established\n");	
		}
	}

	//connect to remote devices
	if(!comms_open(&cfg)){
		comms_close(&cfg);
		pq_close(&(cfg.db));
		cfg_free(&cfg);
		return -1;
	}
	
	//set up signal handlers
	signal(SIGINT, sig_interrupt);
	signal(SIGTERM, sig_terminate);

	//run the state machine
	garfield_pos(&cfg);

	comms_close(&cfg);
	pq_close(&(cfg.db));
	cfg_free(&cfg);
	return 0;
}
Пример #3
0
/*----------------------------------------*/ int main(int argc, char **argv) {
int rc;
int ssmcr_tries=0;
infolog_SetFacility("CTP"); infolog_SetStream("",0);
#ifdef PQWAY
printf("main_ctp: opening rec/send queues...\n");
mq_rec= pq_open();
if(mq_rec==(mqd_t)-1) {
  infolog_trgboth(LOG_FATAL, "posix mq_rec not created");
  exit(8);
}
mq_sendmsg= pq_connect();
if(mq_sendmsg==(mqd_t)-1) {
  infolog_trgboth(LOG_FATAL, "posix mq_sendmsg not connected");
  exit(8);
}
#endif
signal(SIGUSR1, gotsignal); siginterrupt(SIGUSR1, 0);
signal(SIGQUIT, gotsignal); siginterrupt(SIGQUIT, 0);
signal(SIGKILL, gotsignal); siginterrupt(SIGKILL, 0); // -9
signal(SIGTERM, gotsignal); siginterrupt(SIGTERM, 0); // kill pid
signal(SIGINT, gotsignal); siginterrupt(SIGINT, 0);   // CTRL C   2
partmode[0]='\0';
printf("cshmInit i.e. initBakery(swtriggers/ccread/ssmcr ONLY once, when shm allocated)...\n");
/*printf("initBakery(swtriggers,4): 0:SOD/EOD 1:gcalib 2:ctp.exe 3:dims\n");
printf("initBakery(ccread,5): 0:proxy 1:dims 2:ctp+busytool 3:smaq 4:inputs\n");
*/
cshmInit();
setglobalflags(argc, argv);

/* changed in aug2016 (initBakery only once from now, when shm allocated)
printf("initBakery(swtriggers,4): 0:SOD/EOD 1:gcalib 2:ctp.exe 3:dims\n");
initBakery(&ctpshmbase->swtriggers, "swtriggers", swtriggers_N);
printf("initBakery(ccread,6): 0:proxy 1:dims 2:ctp+busytool 3:smaq 4:inputs 5:orbitddl2\n");
initBakery(&ctpshmbase->ccread, "ccread", ccread_N);
printf("initBakery(ssmcr,4): 0:smaq 1:orbitddl2 2:ctp 3:inputs\n");
initBakery(&ctpshmbase->ssmcr, "ssmcr", ssmcr_N);
*/
printBakery(&ctpshmbase->swtriggers);
printBakery(&ctpshmbase->ccread);
printBakery(&ctpshmbase->ssmcr);
unlockBakery(&ctpshmbase->swtriggers,swtriggers_ctpproxy);
unlockBakery(&ctpshmbase->ccread,ccread_ctpproxy);
unlockBakery(&ctpshmbase->ssmcr,ssmcr_ctpproxy);

/* Let' synchronise with smcr only, i.e. 
- ccread_dims,... can go in parallel with ctp_Initproxy, 
  orbitddl2: should use ccread_orbitddl2 customer when reading counters
  
- swtriggers_gcalib/_dims cannot appear (no global runs becasue ctpproxy being restarted)
*/
while(1) { // do not allow SSM usage (smaq) because ctp_Initproxy is initialising it
  char msg[100];
  rc= lockBakeryTimeout(&ctpshmbase->ssmcr,ssmcr_ctpproxy, 10);
  if(rc==1) {
    if(ssmcr_tries>0) {
      sprintf(msg, "Got ssmcr resource after %d attempts", ssmcr_tries);
      infolog_trgboth(LOG_INFO, msg);
    };
    break;  // ssmcr reserved for me now
  };
  ssmcr_tries++;
  sprintf(msg,"%d secs Waiting for ssmcr resource. Is smaq stopped?", ssmcr_tries*10);
  infolog_trgboth(LOG_WARNING, msg);
};
if(isArg(argc, argv, "configrunset")) {
  /* do we need before orbitddl2.py INT/L2 orbit in SYNC (automatic with L2a)?
  */
  int rc, reslen;
  char cmd[200];
  char result[1000]="";   // ~ 10 lines
  char orbchanged[]="Warning: orbitoffset changed:";
  if(envcmp("VMESITE", "PRIVATE")==0) {
    strcpy(cmd, "who");
  } else {
    strcpy(cmd, "orbitddl2.py configrunset");
  };
  infolog_trgboth(LOG_INFO, "Starting L0 orbit calibration (30s...)");
  rc= popenread(cmd, result, 1000);   // opens vme...
  reslen= strlen(result);
  if((rc==EXIT_FAILURE) || (reslen<=1)) { 
    infolog_trgboth(LOG_ERROR, "L0 orbit calibration problem");
  } else {
    int ixr= reslen;
    if(result[ixr-1]=='\n') {   //remove last NL character if present
      result[ixr-1]='\0';
      ixr= reslen-2;
    } else {
      ixr= reslen-1;   // pointer to the last non-NEWLINE character
    };
    while(ixr>=0) {    // find last line
      if(result[ixr]=='\n') {
        ixr++; break;
      };
      ixr--;
    };
    sprintf(cmd, "L0 orbit calibration: %s", &result[ixr]);
    if((strcmp(&result[ixr], "Everything ok")==0) ||
       (strncmp(&result[ixr], orbchanged, strlen(orbchanged))==0)
      ) {
      infolog_trgboth(LOG_INFO, cmd);
    } else {
      infolog_trgboth(LOG_ERROR, cmd);
      infolog_trgboth(LOG_ERROR, "ctpproxy not started"); 
      unlockBakery(&ctpshmbase->ssmcr,ssmcr_ctpproxy);
      rc=8; goto STP;
    };
  };
};
// init CTP after calibration (e.g. to repair modifications done by orbitddl2.py)
rc=ctp_Initproxy();
unlockBakery(&ctpshmbase->ssmcr,ssmcr_ctpproxy);
if(rc!=0) goto STP;

// DIM services not registered here (see ctpdims.c), they run in separae task:
// ds_register();
strcpy(obj,argv[1]);
smi_attach(obj, SMI_handle_command);
printf("CTP attached to: %s\n",obj);
/* if smi_volatile: stops the ctp_proxy in case TRIGGER domain is down
smi_volatile();   
*/
strcpy(errorReason,"not set"); smi_set_parER();
strcpy(ORBIT_NUMBER,""); smi_set_par("ORBIT_NUMBER",ORBIT_NUMBER,STRING);
smi_setState("RUNNING");
while(1) {
#ifdef PQWAY
  executectp("wait");
#else
  usleep(1000000);
#endif
  if(quit>9) break;
};
rc= ctp_Endproxy();
STP:
#ifdef PQWAY
pq_close(mq_sendmsg, 0);
pq_close(mq_rec, 1);
#endif
printf("Calling cshmDetach()...\n"); cshmDetach();
return (rc);
}