Esempio n. 1
0
// --------------------------------------------------------------------------
//
// Function
//		Name:    StoreStructure::MakeWriteLockFilename(const std::string &, int, std::string &)
//		Purpose: Generate the on disc filename of the write lock file
//		Created: 15/12/03
//
// --------------------------------------------------------------------------
void StoreStructure::MakeWriteLockFilename(const std::string &rStoreRoot, int DiscSet, std::string &rFilenameOut)
{
	// Find the disc set
	RaidFileController &rcontroller(RaidFileController::GetController());
	RaidFileDiscSet &rdiscSet(rcontroller.GetDiscSet(DiscSet));
	
	// Make the filename
	std::string writeLockFile(rdiscSet[0] + DIRECTORY_SEPARATOR + rStoreRoot + "write.lock");

	// Return it to the caller
	rFilenameOut = writeLockFile;
}
Esempio n. 2
0
void LLPidLockFile::releaseLock()
{
	llifstream ifile(mLockName);
	LLSD in_pids;
	LLSD out_pids;
	bool write_file=FALSE;

	LLSDSerialize::fromXML(in_pids, ifile);	

	//Clean up this PID and any dead ones.
	for (LLSD::array_iterator i=in_pids.beginArray();
		i !=in_pids.endArray();
		++i)
	{
		U32 stored_pid=(*i).asInteger();

		if (stored_pid != mPID && isProcessAlive(stored_pid))
		{
			out_pids.append( (*i) );
			write_file=TRUE;
		}
	}
	ifile.close();

	if (write_file)
	{
		writeLockFile(out_pids);
	}
	else
	{
		unlink(mLockName.c_str());
	}

	mSaving=FALSE;
	mWaiting=FALSE;
}
Esempio n. 3
0
bool LLPidLockFile::requestLock(LLNameTable<void *> *name_table, bool autosave,
								bool force_immediate, F32 timeout)
{
	bool readyToSave = FALSE;

	if (mSaving) return FALSE;	//Bail out if we're currently saving.  Will not queue another save.
	
	if (!mWaiting){
		mNameTable=name_table;
		mAutosave = autosave;
	}

	LLSD out_pids;
	out_pids.append( (LLSD::Integer)mPID );

	llifstream ifile(mLockName);

	if (ifile.is_open()) 
	{									//If file exists, we need to decide whether or not to continue.
		if ( force_immediate
			|| mTimer.hasExpired() )	//Only deserialize if we REALLY need to.
		{

			LLSD in_pids;

			LLSDSerialize::fromXML(in_pids, ifile);	

			//Clean up any dead PIDS that might be in there.
			for (LLSD::array_iterator i=in_pids.beginArray();
				i !=in_pids.endArray();
				++i)
			{
				U32 stored_pid=(*i).asInteger();

				if (isProcessAlive(stored_pid))
				{
					out_pids.append( (*i) );
				}
			}

			readyToSave=TRUE;
		}
		ifile.close();
	}
	else
	{
		readyToSave=TRUE;
	}

	if (!mWaiting)				//Not presently waiting to save.  Queue up.
	{
		mTimer.resetWithExpiry(timeout);
		mWaiting=TRUE;
	}

	if (readyToSave)
	{	//Potential race condition won't kill us. Ignore it.
		writeLockFile(out_pids);
		mSaving=TRUE;
	}
	
	return readyToSave;
}
Esempio n. 4
0
int main(int argc,char** argv)
{   

/*===================================
0 - First start.
1 - Model menu.
2 - Enter process menu.
3 - Feynman diagrams menu; squaring.
4 - Squared diagram menu; symbolic calculation.                        
5 - Write results; new process.
10 -Restart symbolic calculations
==========================================================*/

/* 0-Start; 1-Restart; 2-Heap Error,3-Edit Model,4-UserBreak */

  void *pscr1=NULL,*pscr2=NULL,*pscr3=NULL,*pscr4=NULL,*pscr5=NULL;
  int   k1=1,k2=1,k3=1,k4=1,k5=1;

  int n;
  int pid=0;
  char LOCKtxt[]="Directory 'results/' contains the .lock file created by n_calchep.\n"
                 "To continue you may a)Close the n_calchep session, or b)Rename 'results/',\n";
  blind=0;
  strcpy(pathtocomphep,argv[0]);
  for(n=strlen(pathtocomphep)-1; n && pathtocomphep[n]!=f_slash; n--);
  pathtocomphep[n-3]=0;                                         
     
   for ( n=1;n<argc;n++) 
   { if (strcmp(argv[n],"-blind")==0 && n<argc-1 )
     {  blind=1;
        inkeyString=argv[++n];
     }
     if (strcmp(argv[n],"+blind")==0 )  blind=2;                                     
   }    

   if(!writeLockFile(".lock")) 
   { fprintf(stderr,"locked by other s_calchep. See .lock file\n");
      exit(100);
   }
   strcpy(pathtouser,"");  
   sprintf(pathtohelp,"%shelp%c",pathtocomphep,f_slash);
   outputDir="results/";        
   { char * icon=(char *) malloc(strlen(pathtocomphep)+10);
     int err;
     sprintf(icon,"%sicon",pathtocomphep);
     err=start1("CalcHEP/symb",icon,"calchep.ini",&xw_error);
     if(err && blind==0)
     { printf("Error:You have launched interactive session for version compiled  without X11 library.\n");
       printf("Options: a) Use blind session; b) Recompile CalcHEP with X11\n");
       exit(66);
     }      
     free(icon);
   }
   fillModelMenu();
   
   f3_key[0]=f3_key_prog;   f3_mess[0]="Model"; 
   f3_key[1]=f4_key_prog;   f3_mess[1]="Diagrams"; 
   f3_key[2]=f5_key_prog;   f3_mess[2]="Switches";
   f3_key[3]=f6_key_prog;   f3_mess[3]="Results"; 
                            f3_mess[4]="Del"; 
                            f3_mess[5]="UnDel";
   f3_key[6]=f9_key_prog;   f3_mess[6]="Ref";    
   f3_key[7]=f10_key_prog;  f3_mess[7]="Quit";

   restoreent(&menulevel);

   if(!blind && menulevel<2) cheplabel(); 

   switch (menulevel)
   {
      case 10: 
      case 6:
      case 5: 
      case 4:
      case 3: readModelFiles("./models",n_model); 
              modelinfo();
              loadModel(0,forceUG);
              processinfo();
              diagramsinfo();
      case 2: k1=n_model;

      case 1: break;
   }

   switch (menulevel)
   {
      case 2:  menuhelp();
               goto label_20;
      case 3:  goto label_31;
      case 4:  goto label_40;
      case 5:  goto label_50;
      case 6:  
      case 10: goto restart2;
   }
        
label_10:   /*   Menu2(ModelMenu): */
   f3_key[0]=NULL; /*models*/ 
   f3_key[1]=NULL; /*diagrams*/
   menulevel = 1;
   forceUG=0;
   menuhelp();
   for(;;)
   { 
      showheap();
      k1=n_model;
      menu1(56,4,"",modelmenu,"s_1",&pscr1,&k1);
      n_model=k1;
      if(n_model == 0)
      {
	if( mess_y_n(56,4,"Quit session")) {  saveent(menulevel); goto exi; }         
      }
      else  if(n_model == maxmodel+1)
      {
         clrbox(1,4,55,18);
         makenewmodel();
         menuhelp();
      }
      else if (n_model > 0)
      { 
	put_text(&pscr1);
	goto label_20;
      }
   }

label_20:   /*  Menu3:Enter Process  */
   f3_key[0]=NULL; 
   f3_key[1]=NULL; 

   menulevel = 2;
   saveent(menulevel);
   readModelFiles("./models",n_model);
   modelinfo();
   k2 = 1;
   do
   {  char strmen[]="\026"
        " Enter Process        "
        " Force Unit.Gauge OFF "
        " Edit model           "
        " Delete model         ";

      if(forceUG)improveStr(strmen,"OFF","ON");
      menu1(56,4,"",strmen,"s_2_*",&pscr2,&k2);
      switch (k2)
      {
         case 0:  goto_xy(1,1); clr_eol(); goto label_10;
         case 2:  forceUG=!forceUG;   modelinfo(); break;
	 case 3:  editModel(1); break;
         case 4: 
	    if ( deletemodel(n_model) )
            {
               goto_xy(1,1);
               clr_eol();
               n_model=1;
               fillModelMenu();
               goto label_10;
            }
            else   readModelFiles("./models",n_model);
      }
   }  while (k2 != 1);

   loadModel(0,forceUG);
label_21:

   f3_key[0]=NULL; 
   f3_key[1]=NULL; 

   menulevel=2;
   errorcode=enter();   /*  Enter a process  */
   newCodes=0;
   showheap();
   if (errorcode)   /*  'Esc' pressed  */ { menuhelp(); goto label_20;}
   errorcode=construct();          /*  unSquared diagrams  */
   if (errorcode) 
   {  if(blind)
      {  printf("Processes of this type are absent\n"); sortie(111); } 
      else 
      { messanykey(5,22,"Processes of this type are absent");  
        clrbox(1,19,80,24); 
        goto label_21;
      }
   }
   else if(!blind)
   { int dirStat=checkDir("results"); 
     if(dirStat!=0)
     {  messanykey( 10,10,"There are files in directory 'results/'.\n"  
                          "To continue you has to clean or rename this directory.");
        viewresults();
        if(checkDir("results")!=0)  goto label_21;                   
     }
     clr_scr(FGmain,BGmain);
     modelinfo();
     processinfo();
     diagramsinfo();
     goto label_31;	
   }
   
label_30: /*  Menu4: Squaring,...*/
   clr_scr(FGmain,BGmain);
   modelinfo();
   processinfo();
   diagramsinfo();
label_31: 

   f3_key[0]=f3_key_prog; 
   f3_key[1]=NULL; 

   menulevel=3;  
   k3 = 1;
   do
   {
      menu1(56,4,"","\026"
         " View diagrams        "
/*         " Amplitude calculation" */
         " Squaring technique   "
         " Write down processes ","s_squa_*",&pscr3,&k3);
      switch (k3)
      {
         case 0: clrbox(1,2,55,11); menuhelp(); goto label_20;
         case 1: viewfeyndiag(1);   break;
         case 3: { FILE*f=fopen("results/list_prc.txt","w");
                   int k,ndel,ncalc,nrest;
                   char process[100];
                   long recpos; 
                   menup=fopen(MENUP_NAME,"r");
                   for(k=1;;k++) 
                   { int err=rd_menu(1,k,process,&ndel,&ncalc,&nrest,&recpos);
                     if(!err) break;
                     trim(process);
                     fprintf(f,"%s\n",process);
                   } 
                   fclose(f);
                   fclose(menup);
                    messanykey(20,14,"See file 'results/list_prc.txt'");
 
                 } break; 

/*       case 2: messanykey(10,10,"Not implemented yet"); Amplitudes(); */
      }
   }  while (k3 != 2);      

   if (!squaring()) goto label_30;  /*  process is absent  */

   clear_tmp();

   saveent(menulevel);
   restoreent(&menulevel);  

label_40:   /*  Menu5: View squared diagrams.....   */

   f3_key[0]=f3_key_prog; 
   f3_key[1]=f4_key_prog; 

   menulevel=4;
   clr_scr(FGmain,BGmain);
   modelinfo();
   processinfo();
   diagramsinfo();
   sq_diagramsinfo(); /*   ????????   */

   k4=1;
   saveent(menulevel);
   pscr4=NULL;
   for(;;)   
   {  int res;
      menu1(56,4,"","\026"
         " View squared diagrams"
         " Symbolic calculations"
         " Make&Launch n_calchep"        
         " Make n_calchep       "
         " REDUCE program       "
         ,"s_calc_*",&pscr4,&k4);

      res=checkDir("results");
      if(res==1)
      {
        int  n_calchep_id=setLockFile("results/.lock");
        if(n_calchep_id) unLockFile(n_calchep_id); else res=2;                          
      }
      switch (k4)
      {  case 0:
            if (mess_y_n(50,3,"Return to previous menu?"))goto label_30;
         break;

         case 1:
            viewsqdiagr();  break;

         case 2:     /*  Compute all diagrams   */
restart2:
            f3_key[0]=f3_key_prog; 
            f3_key[1]=f4_key_prog; 

            menulevel=4;
            calcallproc();
            sq_diagramsinfo();

            if(!continuetest()) break; 
            showheap();  
            put_text(&pscr4);
         break;
         case 3:
            { static char keystr[12]="]{{[{";
              if(res==2) messanykey(3,10,LOCKtxt); else inkeyString=keystr;
            }
            break;
         case 4:
            if(res==2) messanykey(3,10,LOCKtxt); else
            { char command[100];
              FILE *f=fopen("results/EXTLIB","w");
              fprintf(f,"EXTLIB=\"%s\"\nexport EXTLIB\n",EXTLIB);
              fclose(f); 
              saveent(menulevel);
{ char command[100];
  sprintf(command,"cd results; $CALCHEP/bin/make_VandP  ../models  %d",n_model);
  system(command);
}                 
              finish();
              sortie(22);
            }
         case 5:  mk_reduceprograms();  break;
      }
      if(k4==2 && continuetest()) { put_text(&pscr4); break; }
   }

label_50:
   k5=1;
   pscr5=NULL;
   menulevel=5;
   saveent(menulevel);
   
   for(;;)  
   {  int n_calchep_id;   
      menu1(56,4,"","\026"
         " C code               "
         "     C-compiler       "
         "     Edit Linker      "
         " REDUCE code          "
         " MATHEMATICA code     "
         " FORM code            "
         " Enter new process    " ,"s_out_*",&pscr5,&k5);
         
      if((k5==1||k5==2)&&pid) 
      { int epid=waitpid(pid,NULL,WNOHANG);
        if(epid) pid=0; else
        { 
          messanykey(3,10,"This option is frozen while n_calchep runs or is compiled");
          continue;
        }
      }  

      switch (k5)
      {  case 0: goto label_40;  break;
         case 1:
           c_prog(); newCodes=0; saveent(menulevel);
           break;
         case 2:
           if(newCodes) { c_prog(); newCodes=0; saveent(menulevel);} 
           pid=fork();
           if(pid==0)
           { 
             char * command;
             n_calchep_id=setLockFile("results/.lock"); 
             if(n_calchep_id)
             {  
               command=(char*)malloc(strlen(pathtocomphep)+strlen(EXTLIB)+100);
               sprintf(command,"EXTLIB=\"%s\"\n"
                               "export EXTLIB\n"
                               "cd results\n"
                               "xterm -e %s/make__n_calchep %d",
                               EXTLIB,pathtocomphep,n_model);                                 
               system(command);
               sprintf(command,"cd results; ./n_calchep"); 
               unLockFile(n_calchep_id);
               system(command); 
               free(command);
             }
             exit(0);
           }
           break;
         case 3: if(edittable(1,1,&modelTab[4],1," ",0))
                 {  char fName[STRSIZ];
                    readEXTLIB();
                    sprintf(fName,"%smodels%c%s%d.mdl",pathtouser,f_slash,mdFls[4],n_model);
                    writetable( &modelTab[4],fName); 
                 }
                 break;
         case 4: makeReduceOutput(); break;
         case 5: makeMathOutput();   break; 
	 case 6: makeFormOutput();   break;
   
         case 7:
            put_text(&pscr5);
            clrbox(1,2,55,11);
            menuhelp();
            goto label_20;
      }
   }

exi:
   finish();
   sortie(0);
   return 0;
}
int main(int argc,char** argv)
{   

/*===================================
0 - First start.
1 - Model menu.
2 - Enter process menu.
3 - Feynman diagrams menu; squaring.
4 - Squared diagram menu; symbolic calculation.                        
5 - Write results; new process.
10 -Restart symbolic calculations
==========================================================*/

/* 0-Start; 1-Restart; 2-Heap Error,3-Edit Model,4-UserBreak */

  void *pscr1=NULL,*pscr2=NULL,*pscr3=NULL,*pscr4=NULL,*pscr5=NULL;
  int   k1=1,k2=1,k3=1,k4=1,k5=1;

  int n;
  int pid=0;
  char LOCKtxt[]="Directory 'results/' contains the .lock file created by n_calchep.\n"
                 "To continue you may a)Close the n_calchep session, or b)Rename 'results/',\n";


  blind=0;
  strcpy(pathtocalchep,argv[0]);
  for(n=strlen(pathtocalchep)-1; n && pathtocalchep[n]!=f_slash; n--);
  pathtocalchep[n-3]=0;                                         
     
   for ( n=1;n<argc;n++) 
   { if (strcmp(argv[n],"-blind")==0 && n<argc-1 )
     {  blind=1;
        inkeyString=argv[++n];
     }
     if (strcmp(argv[n],"+blind")==0 )  blind=2;                                     
     if (strcmp(argv[n],"--version")==0 ) { printf("%s\n", VERSION_); exit(0);}
   }    

   if(!writeLockFile(".lock")) 
   { fprintf(stderr,"locked by other s_calchep. See .lock file\n");
      exit(100);
   }
   strcpy(pathtouser,"");  
   sprintf(pathtohelp,"%shelp%c",pathtocalchep,f_slash);
   outputDir="results/";        
   { char * icon=(char *) malloc(strlen(pathtocalchep)+20);
     char  title[30];
     int err;
     sprintf(icon,"%s/include/icon",pathtocalchep);
     sprintf(title,"CalcHEP_%s/symb", VERSION);
     err=start1(title,icon,"calchep.ini",&xw_error);
     if(err && blind==0)
     { printf("Error:You have launched interactive session for version compiled  without X11 library.\n");
       printf(" Presumably X11 development package is not installed in your computer.\n");
       printf(" In this case directory /usr/include/X11/ is empty.\n");       
       printf("Options: a) Use blind session; b) Update Linux and recompile CalcHEP \n");
       printf("Name of needed package\n");
       printf("     libX11-devel      Fedora/Scientific Linux/CYGWIN/Darwin(MAC)\n");
       printf("     libX11-dev        Ubuntu/Debian\n");
       printf("     xorg-x11-devel    SUSE\n");

       exit(66);
     }      
     free(icon);
   }
   fillModelMenu();
   
   f3_key[0]=f3_key_prog;   f3_mess[0]="Model"; 
   f3_key[1]=f4_key_prog;   f3_mess[1]="Diagrams"; 
   f3_key[2]=f5_key_prog;   f3_mess[2]="Switches";
   f3_key[3]=f6_key_prog;   f3_mess[3]="Results"; 
                            f3_mess[4]="Del"; 
                            f3_mess[5]="UnDel";
   f3_key[6]=f9_key_prog;   f3_mess[6]="Ref";    
   f3_key[7]=f10_key_prog;  f3_mess[7]="Quit";

   restoreent(&menulevel);

   if(!blind && menulevel<2) cheplabel(); 

   switch (menulevel)
   {
      case 10: 
      case 6:
      case 5: 
      case 4:
      case 3: readModelFiles("./models",n_model); 
              modelinfo();
              loadModel(0,forceUG);
              processinfo();
              diagramsinfo();
              k1=n_model;
              break;
      case 2: k1=n_model;
              readModelFiles("./models",n_model);
              break; 
   }

   switch (menulevel)
   {
      case 2:  menuhelp();
               goto label_20;
      case 3:  goto label_31;
      case 4:  goto label_40;
      case 5:  goto label_50;
      case 6:  
      case 10: goto restart2;
   }
        
label_10:   /*   Menu2(ModelMenu): */
   f3_key[0]=NULL; /*models*/ 
   f3_key[1]=NULL; /*diagrams*/
   menulevel = 1;
   forceUG=0;
   menuhelp();
   for(;;)
   { 
      showheap();
      k1=n_model;
      menu1(56,4,"",modelmenu,"s_1",&pscr1,&k1);
      if(k1 == 0)
      {  
	if( mess_y_n(56,4,"Quit session")) {n_model=0;   saveent(menulevel); goto exi; }         
      }
      else  if(k1 == maxmodel+1)
      {
         clrbox(1,4,55,18);
         makenewmodel();
         menuhelp();
      }
      else if (k1 > 0)
      { int err=0;
        put_text(&pscr1);
        if(k1!=n_model || ldModelStatus==0) { err=readModelFiles("./models",k1);}
        n_model=k1;
        if(err){ if(blind) sortie(133); else  goto label_10;} else goto label_20;
      }
   }

label_20:   /*  Menu3:Enter Process  */
   f3_key[0]=NULL; 
   f3_key[1]=NULL; 

   menulevel = 2;
   saveent(menulevel);
   
   modelinfo();
   k2 = 1;
   
   do
   {  char strmen[]="\026"
        " Enter Process        "
        " Force Unit.Gauge= OFF"
        " Edit model           "
        " Numerical Evaluation "
        "======================"
        " Delete model         ";

      if(forceUG)improveStr(strmen,"OFF","ON");
      menu1(56,4,"",strmen,"s_2_*",&pscr2,&k2);
      switch (k2)
      {
         case 0:  goto_xy(1,1); clr_eol(); goto label_10;
         case 2:  forceUG=!forceUG;   modelinfo(); break;
	 case 3:  editModel(1); break;
	 case 4:  numcheck();   
	 case 5:  break;
         case 6: 
	    if(deletemodel(n_model))
            {
               goto_xy(1,1);
               clr_eol();
               n_model=1;
               ldModelStatus=0;
               fillModelMenu();
               goto label_10;
            }
      }
   }  while (k2 != 1);

   if(!loadModel(0,forceUG)) goto  label_20;
   
label_21:

   f3_key[0]=NULL; 
   f3_key[1]=NULL; 
   
   menulevel=2;
   checkAuxDir(n_model);
   errorcode=enter();   /*  Enter a process  */
   newCodes=0;
   showheap();
   if (errorcode)   /*  'Esc' pressed  */ { menuhelp(); goto label_20;}
   errorcode=construct();          /*  unSquared diagrams  */
   if (errorcode) 
   {  if(blind)
      {  printf("Processes of this type are absent\n"); sortie(111); } 
      else 
      { messanykey(5,22,"Processes of this type are absent");  
        clrbox(1,19,80,24); 
        goto label_21;
      }
   }
   else if(!blind)
   { int dirStat=checkDir("results"); 
     if(dirStat!=0)
     {  messanykey( 10,10,"There are files in directory 'results/'.\n"  
                          "To continue you has to clean or rename this directory.");
        viewresults();
        if(checkDir("results")!=0)  goto label_21;                   
     }
     clr_scr(FGmain,BGmain);
     modelinfo();
     processinfo();
     diagramsinfo();
     goto label_31;	
   }
   
label_30: /*  Menu4: Squaring,...*/
   clr_scr(FGmain,BGmain);
   modelinfo();
   processinfo();
   diagramsinfo();
label_31: 

   f3_key[0]=f3_key_prog; 
   f3_key[1]=NULL; 

   menulevel=3;  
   k3 = 1;
   do
   {
      menu1(56,4,"","\026"
         " View diagrams        "
/*         " Amplitude calculation" */
         " Square diagrams      "
         " Write down processes ","s_squa_*",&pscr3,&k3);
      switch (k3)
      {
         case 0: clrbox(1,2,55,11); menuhelp(); goto label_20;
         case 1: viewfeyndiag(1);   break;
         case 3: { FILE*f=fopen("results/list_prc.txt","w");
                   int k,ndel,ncalc,nrest;
                   char process[100];
                   long recpos; 
                   menup=fopen(MENUP_NAME,"r");
                   for(k=1;;k++) 
                   { int err=rd_menu(1,k,process,&ndel,&ncalc,&nrest,&recpos);
                     if(!err) break;
                     trim(process);
                     fprintf(f,"%s\n",process);
                   } 
                   fclose(f);
                   fclose(menup);
                    messanykey(20,14,"See file 'results/list_prc.txt'");
 
                 } break; 

/*       case 2: messanykey(10,10,"Not implemented yet"); Amplitudes(); */
      }
   }  while (k3 != 2);      

   if (!squaring()) goto label_30;  /*  process is absent  */

   clear_tmp();

   saveent(menulevel);
   restoreent(&menulevel);  

label_40:   /*  Menu5: View squared diagrams.....   */

   f3_key[0]=f3_key_prog; 
   f3_key[1]=f4_key_prog; 

   menulevel=4;
   clr_scr(FGmain,BGmain);
   modelinfo();
   processinfo();
   diagramsinfo();
   sq_diagramsinfo(); /*   ????????   */

   k4=1;
   saveent(menulevel);
   pscr4=NULL;
   for(;;)   
   {  int res;
      menu1(56,4,"","\026"
         " View squared diagrams"
         " Symbolic calculations"
         " Make&Launch n_calchep"        
         " Make n_calchep       "
         " REDUCE program       "
         ,"s_calc_*",&pscr4,&k4);

      res=checkDir("results");
      if(res==1)
      {
        int  n_calchep_id=setLockFile("results/.lock");
        if(n_calchep_id) unLockFile(n_calchep_id); else res=2;                          
      }
      switch (k4)
      {  case 0:
            if (mess_y_n(50,3,"Return to previous menu?"))goto label_30;
         break;

         case 1:
            viewsqdiagr();  break;

         case 2:     /*  Compute all diagrams   */
restart2:
            f3_key[0]=f3_key_prog; 
            f3_key[1]=f4_key_prog; 

            menulevel=4;
                        
            if(!nPROCSS ) calcallproc(); else 
            { int *pids=malloc(sizeof(int)*nPROCSS);
              int *pipes=malloc(2*sizeof(int)*nPROCSS);
              int **qd=malloc(sizeof(int*)*nPROCSS);
              int totD=sqDiagList(qd, nPROCSS);
              int totC,nProc;
              int k;
              
              fflush(NULL);
              for(k=0;k<nPROCSS;k++) 
              { int* kpipe=pipes+2*k;
                pipe(kpipe);
                pids[k]=fork();
                if(pids[k]==0)
                { 
                  close(kpipe[0]);
                  calcWithFork(k,qd[k],kpipe[1]);
                  exit(0);
                }
              }
              for(k=0;k<nPROCSS;k++) close(pipes[2*k+1]);
              infoLine(0.);
              for(nProc=nPROCSS,totC=0;nProc;)
              { int one;
                int err;
                nProc=0;
                for(k=0;k<nPROCSS;k++) if(pids[k])
                { if(waitpid(pids[k],NULL,WNOHANG)==0)
                  {  nProc++; 
                     if(read(pipes[2*k],&one,sizeof(int)))totC+=one;
                  } else 
                  { for(;read(pipes[2*k],&one,sizeof(int))>0;) totC+=one;
                     pids[k]=0;
                  }
                }  
                if(infoLine((double)totC/(double)totD)) for(k=0;k<nPROCSS;k++) if(pids[k]) kill(pids[k],SIGUSR1);   
              }  
              infoLine(2);
              for(k=0;k<nPROCSS;k++)
              { char ctlgName[100];
                char command[200];
                sprintf(ctlgName,"%s_%d",CATALOG_NAME,k);
                if(access(ctlgName,R_OK)==0)
                { sprintf(command," cat %s >> %s", ctlgName,CATALOG_NAME);
                  system(command);
                  unlink(ctlgName);   
                }
              }
              if(totC)  newCodes=1;  
              updateMenuQ();
              
              for(k=0;k<nPROCSS;k++) close(pipes[2*k]);
              free(pids);   
              free(pipes);
              for(k=0;k<nPROCSS;k++) free(qd[k]);
              free(qd);   
            }
            
            sq_diagramsinfo();

            if(!continuetest()) break; 
            showheap();  
            put_text(&pscr4);
         break;
         case 3:
            { static char keystr[12]="]{{[{";
              if(res==2) messanykey(3,10,LOCKtxt); else inkeyString=keystr;
            }
            break;
         case 4:
            if(res==2) messanykey(3,10,LOCKtxt); else
            { char command[100];
              saveent(menulevel);
              chdir("results");
              makeVandP(0,"../models",n_model,2,pathtocalchep);
              finish();
              sortie(22);
            }
         case 5:  mk_reduceprograms();  break;
      }
      if(k4==2 && continuetest()) { put_text(&pscr4); break; }
   }

label_50:
   k5=1;
   pscr5=NULL;
   menulevel=5;
   saveent(menulevel);
   sq_diagramsinfo();
      
   for(;;)  
   {  int n_calchep_id;   
      menu1(56,4,"","\026"
         " C code               "
         "     C-compiler       "
         "     Edit Linker      "
         " REDUCE code          "
         " MATHEMATICA code     "
         " FORM code            "
         " Enter new process    " ,"s_out_*",&pscr5,&k5);
          
      if((k5==1||k5==2)&&pid) 
      { int epid=waitpid(pid,NULL,WNOHANG);
        if(epid) pid=0; else
        { 
          messanykey(3,10,"This option is frozen while n_calchep runs or is compiled");
          continue;
        }
      }  

      switch (k5)
      {  case 0: goto label_40;  break;
         case 1:
           c_prog(); newCodes=0; saveent(menulevel);
           break;
         case 2:
           if(newCodes) { c_prog(); newCodes=0; saveent(menulevel);}
          n_calchep_id=setLockFile("results/.lock"); 
           if(n_calchep_id)
           {
             if(nPROCSS)
             { chdir("results"); 
                makeVandP(0,"../models",n_model,2,pathtocalchep);
               pCompile();
               if(access("./n_calchep",X_OK)==0)
               { 
                  fflush(NULL); 
                  pid=fork();   
                  if(pid==0) 
                  {
                    system("./n_calchep");
                    exit(0);
                  }    
               } else messanykey(15,15,"n_calchep is not generated");
               chdir("../");
             } else                       
             { fflush(NULL); 
               pid=fork();
               if(pid==0)
               {  if(chdir("results")==0)
                  {   char*command=malloc(100+strlen(pathtocalchep));
                      makeVandP(0,"../models",n_model,2,pathtocalchep); 
                      sprintf(command,"xterm -e %s/sbin/make__n_calchep %d", pathtocalchep,n_model);                                 
                      system(command);
                      free(command);
                      system("./n_calchep");                     
                   }
                   exit(0);    
               }
             }
             unLockFile(n_calchep_id); 
           } 
           break;
         case 3: if(edittable(1,1,&modelTab[4],1," ",0))
                 {  char fName[STRSIZ];
                    sprintf(fName,"%smodels%c%s%d.mdl",pathtouser,f_slash,mdFls[4],n_model);
                    writetable( &modelTab[4],fName); 
                 }
                 break;
         case 4: makeReduceOutput(); break;
         case 5: makeMathOutput();   break; 
	 case 6: makeFormOutput();   break;
   
         case 7:
            put_text(&pscr5);
            clrbox(1,2,55,11);
            menuhelp();
            goto label_20;
      }
   }

exi:
   finish();
   sortie(0);
   return 0;
}
Esempio n. 6
0
PtpClock *
ptpdStartup(int argc, char **argv, Integer16 * ret, RunTimeOpts * rtOpts)
{
    PtpClock * ptpClock;
    int i = 0;

    /*
     * Set the default mode for all newly created files - previously
     * this was not the case for log files. This adds consistency
     * and allows to use FILE* vs. fds everywhere
     */
    umask(~DEFAULT_FILE_PERMS);

    /**
     * If a required setting, such as interface name, or a setting
     * requiring a range check is to be set via getopts_long,
     * the respective currentConfig dictionary entry should be set,
     * instead of just setting the rtOpts field.
     *
     * Config parameter evaluation priority order:
     * 	1. Any dictionary keys set in the getopt_long loop
     * 	2. CLI long section:key type options
     * 	3. Config file (parsed last), merged with 2. and 3 - will be overwritten by CLI options
     * 	4. Defaults and any rtOpts fields set in the getopt_long loop
    **/

    /**
     * Load defaults. Any options set here and further inside loadCommandLineOptions()
     * by setting rtOpts fields, will be considered the defaults
     * for config file and section:key long options.
     */
    loadDefaultSettings(rtOpts);
    /* initialise the config dictionary */
    rtOpts->candidateConfig = dictionary_new(0);
    rtOpts->cliConfig = dictionary_new(0);
    /* parse all long section:key options and clean up argv for getopt */
    loadCommandLineKeys(rtOpts->cliConfig,argc,argv);
    /* parse the normal short and long option, exit on error */
    if (!loadCommandLineOptions(rtOpts, rtOpts->cliConfig, argc, argv, ret)) {
        goto fail;
    }

    /* Display startup info and argv if not called with -? or -H */
    NOTIFY("%s version %s starting\n",USER_DESCRIPTION, USER_VERSION);
    dump_command_line_parameters(argc, argv);
    /*
     * we try to catch as many error conditions as possible, but before we call daemon().
     * the exception is the lock file, as we get a new pid when we call daemon(),
     * so this is checked twice: once to read, second to read/write
     */
    if(geteuid() != 0)
    {
        printf("Error: "PTPD_PROGNAME" daemon can only be run as root\n");
        *ret = 1;
        goto fail;
    }

    /* Have we got a config file? */
    if(strlen(rtOpts->configFile) > 0) {
        /* config file settings overwrite all others, except for empty strings */
        INFO("Loading configuration file: %s\n",rtOpts->configFile);
        if(loadConfigFile(&rtOpts->candidateConfig, rtOpts)) {
            dictionary_merge(rtOpts->cliConfig, rtOpts->candidateConfig, 1, "from command line");
        } else {
            *ret = 1;
            dictionary_merge(rtOpts->cliConfig, rtOpts->candidateConfig, 1, "from command line");
            goto configcheck;
        }
    } else {
        dictionary_merge(rtOpts->cliConfig, rtOpts->candidateConfig, 1, "from command line");
    }
    /**
     * This is where the final checking  of the candidate settings container happens.
     * A dictionary is returned with only the known options, explicitly set to defaults
     * if not present. NULL is returned on any config error - parameters missing, out of range,
     * etc. The getopt loop in loadCommandLineOptions() only sets keys verified here.
     */
    if( ( rtOpts->currentConfig = parseConfig(rtOpts->candidateConfig,rtOpts)) == NULL ) {
        *ret = 1;
        dictionary_del(rtOpts->candidateConfig);
        goto configcheck;
    }

    /* we've been told to print the lock file and exit cleanly */
    if(rtOpts->printLockFile) {
        printf("%s\n", rtOpts->lockFile);
        *ret = 0;
        goto fail;
    }

    /* we don't need the candidate config any more */
    dictionary_del(rtOpts->candidateConfig);

    /* Check network before going into background */
    if(!testInterface(rtOpts->ifaceName, rtOpts)) {
        ERROR("Error: Cannot use %s interface\n",rtOpts->ifaceName);
        *ret = 1;
        goto configcheck;
    }

configcheck:
    /*
     * We've been told to check config only - clean exit before checking locks
     */
    if(rtOpts->checkConfigOnly) {
        if(*ret != 0) {
            printf("Configuration has errors\n");
            *ret = 1;
        }
        else
            printf("Configuration OK\n");
        return 0;
    }

    /* Previous errors - exit */
    if(*ret !=0)
        return 0;

    /* First lock check, just to be user-friendly to the operator */
    if(!rtOpts->ignore_daemon_lock) {
        if(!writeLockFile(rtOpts)) {
            /* check and create Lock */
            ERROR("Error: file lock failed (use -L or global:ignore_lock to ignore lock file)\n");
            *ret = 3;
            return 0;
        }
        /* check for potential conflicts when automatic lock files are used */
        if(!checkOtherLocks(rtOpts)) {
            *ret = 3;
            return 0;
        }
    }

    /* Manage log files: stats, log, status and quality file */
    restartLogging(rtOpts);

    /* Allocate memory after we're done with other checks but before going into daemon */
    ptpClock = (PtpClock *) calloc(1, sizeof(PtpClock));
    if (!ptpClock) {
        PERROR("Error: Failed to allocate memory for protocol engine data");
        *ret = 2;
        return 0;
    } else {
        DBG("allocated %d bytes for protocol engine data\n",
            (int)sizeof(PtpClock));
        ptpClock->foreign = (ForeignMasterRecord *)
                            calloc(rtOpts->max_foreign_records,
                                   sizeof(ForeignMasterRecord));
        if (!ptpClock->foreign) {
            PERROR("failed to allocate memory for foreign "
                   "master data");
            *ret = 2;
            free(ptpClock);
            return 0;
        } else {
            DBG("allocated %d bytes for foreign master data\n",
                (int)(rtOpts->max_foreign_records *
                      sizeof(ForeignMasterRecord)));
        }

        ptpClock->owd_filt = FilterCreate(FILTER_EXPONENTIAL_SMOOTH, "owd");
        ptpClock->ofm_filt = FilterCreate(FILTER_MOVING_AVERAGE, "ofm");
    }

    if(rtOpts->statisticsLog.logEnabled)
        ptpClock->resetStatisticsLog = TRUE;

    /* Init to 0 net buffer */
    memset(ptpClock->msgIbuf, 0, PACKET_SIZE);
    memset(ptpClock->msgObuf, 0, PACKET_SIZE);

    /* Init user_description */
    memset(ptpClock->user_description, 0, sizeof(ptpClock->user_description));
    memcpy(ptpClock->user_description, &USER_DESCRIPTION, sizeof(USER_DESCRIPTION));

    /* Init outgoing management message */
    ptpClock->outgoingManageTmp.tlv = NULL;


    /*  DAEMON */
#ifdef PTPD_NO_DAEMON
    if(!rtOpts->nonDaemon) {
        rtOpts->nonDaemon=TRUE;
    }
#endif

    if(!rtOpts->nonDaemon) {
        /*
         * fork to daemon - nochdir non-zero to preserve the working directory:
         * allows relative paths to be used for log files, config files etc.
         * Always redirect stdout/err to /dev/null
         */
        if (daemon(1,0) == -1) {
            PERROR("Failed to start as daemon");
            *ret = 3;
            return 0;
        }
        INFO("  Info:    Now running as a daemon\n");
        /*
         * Wait for the parent process to terminate, but not forever.
         * On some systems this happened after we tried re-acquiring
         * the lock, so the lock would fail. Hence, we wait.
         */
        for (i = 0; i < 1000000; i++) {
            /* Once we've been reaped by init, parent PID will be 1 */
            if(getppid() == 1)
                break;
            usleep(1);
        }
    }

    /* Second lock check, to replace the contents with our own new PID and re-acquire the advisory lock */
    if(!rtOpts->nonDaemon && !rtOpts->ignore_daemon_lock) {
        /* check and create Lock */
        if(!writeLockFile(rtOpts)) {
            ERROR("Error: file lock failed (use -L or global:ignore_lock to ignore lock file)\n");
            *ret = 3;
            return 0;
        }
    }

#if defined(linux) && defined(HAVE_SCHED_H)
    /* Try binding to a single CPU core if configured to do so */

    if(rtOpts->cpuNumber > -1) {

        cpu_set_t mask;
        CPU_ZERO(&mask);
        CPU_SET(rtOpts->cpuNumber,&mask);
        if(sched_setaffinity(0, sizeof(mask), &mask) < 0) {
            PERROR("Could not bind to CPU core %d", rtOpts->cpuNumber);
        } else {
            INFO("Successfully bound "PTPD_PROGNAME" to CPU core %d\n", rtOpts->cpuNumber);
        }
    }
#endif /* linux && HAVE_SCHED_H */

#ifdef HAVE_SYS_CPUSET_H

    /* Try binding to a single CPU core if configured to do so */

    if(rtOpts->cpuNumber > -1) {
        cpuset_t mask;
        CPU_ZERO(&mask);
        CPU_SET(rtOpts->cpuNumber,&mask);
        if(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID,
                              -1, sizeof(mask), &mask) < 0) {
            PERROR("Could not bind to CPU core %d",
                   rtOpts->cpuNumber);
        } else {
            INFO("Successfully bound "PTPD_PROGNAME" to CPU core %d\n",
                 rtOpts->cpuNumber);
        }
    }
#endif /* HAVE_SYS_CPUSET_H */

    /* use new synchronous signal handlers */
    signal(SIGINT,  catchSignals);
    signal(SIGTERM, catchSignals);
    signal(SIGHUP,  catchSignals);

    signal(SIGUSR1, catchSignals);
    signal(SIGUSR2, catchSignals);

#if defined PTPD_SNMP
    /* Start SNMP subsystem */
    if (rtOpts->snmp_enabled)
        snmpInit(rtOpts, ptpClock);
#endif



    NOTICE(USER_DESCRIPTION" started successfully on %s using \"%s\" preset (PID %d)\n",
           rtOpts->ifaceName,
           (getPtpPreset(rtOpts->selectedPreset,rtOpts)).presetName,
           getpid());
    ptpClock->resetStatisticsLog = TRUE;

#ifdef PTPD_STATISTICS
    if (rtOpts->delayMSOutlierFilterEnabled) {
        ptpClock->delayMSRawStats = createDoubleMovingStdDev(rtOpts->delayMSOutlierFilterCapacity);
        strncpy(ptpClock->delayMSRawStats->identifier, "delayMS", 10);
        ptpClock->delayMSFiltered = createDoubleMovingMean(rtOpts->delayMSOutlierFilterCapacity);
    } else {
        ptpClock->delayMSRawStats = NULL;
        ptpClock->delayMSFiltered = NULL;
    }

    if (rtOpts->delaySMOutlierFilterEnabled) {
        ptpClock->delaySMRawStats = createDoubleMovingStdDev(rtOpts->delaySMOutlierFilterCapacity);
        strncpy(ptpClock->delaySMRawStats->identifier, "delaySM", 10);
        ptpClock->delaySMFiltered = createDoubleMovingMean(rtOpts->delaySMOutlierFilterCapacity);
    } else {
        ptpClock->delaySMRawStats = NULL;
        ptpClock->delaySMFiltered = NULL;
    }
#endif

    *ret = 0;
    return ptpClock;

fail:
    dictionary_del(rtOpts->candidateConfig);
    return 0;
}
Esempio n. 7
0
int main(int argc,char** argv)
{
  int n;
  char icon[200];
  char title[30];

  blind=0;
  for( n=1;n<argc;n++) 
  {
    if(strcmp(argv[n],"--version")==0)  {  printf("%s\n",VERSION_); exit(0); }
    if(strcmp(argv[n],"-blind")==0&& n<argc-1 )
    { blind=1;     
      inkeyString=argv[++n];
    } else  if (strcmp(argv[n],"+blind")==0 ) blind=2;
  }

  if(!writeLockFile(".lock"))
  { fprintf(stderr,"locked by other n_calchep. See .lock\n");
    exit(100);
  }
                 
  setenv("CALCHEP",rootDir,0);
  sprintf(pathtocalchep,"%s%c",rootDir,f_slash);
  sprintf(pathtohelp,"%shelp%c",pathtocalchep,f_slash);
  sprintf(icon,"%s/include/icon",pathtocalchep);
  sprintf(title,"CalcHEP_%s/num", VERSION);

  f3_key[2]=f5_key_prog;   f3_mess[2]="Options";
  f3_key[3]=f6_key_prog;   f3_mess[3]="Results";
  f3_key[5]=f8_key_prog;   f3_mess[5]="Calc";
  f3_key[6]=f9_key_prog;   f3_mess[6]="Ref";
  f3_key[7]=f10_key_prog;  f3_mess[7]="Quit";
  
  { int size=100;
     for(;;)
     {  compDir=realloc(compDir,size+20);
        if(getcwd(compDir,size)) break; else size*=2;
     }
     strcat(compDir,"/aux");
     libDir=malloc(strlen(compDir)+20);
     sprintf(libDir,"%s/so_generated",compDir);
     modelNum=1;
     calchepDir=getenv("CALCHEP");
     if(!calchepDir) calchepDir=interface_ext.CALCHEP;
     ForceUG=interface_ext.forceUG;
  }
  

/* **  initialization of the session */
  link_process(PtrInterface_ext);  


  start1(title,icon,"calchep.ini;../calchep.ini",&xw_error);
  nPROCSS=sysconf(_SC_NPROCESSORS_ONLN); 

  if(r_sess__(NULL)==-1)
  { 
     char buff[200];
     int pdg[11]={21,1,-1,2,-2,3,-3,4,-4,5,-5};  
     int k,ok=0;
     strcpy(buff,"{p*\\09");
     for(k=0;k<11;k++)
     {  char*ch=pdg2name(pdg[k]);
        if(ch)
        {  if(ok) strcat(buff,",");
           strcat(buff,ch);
           ok=1;
        }   
     }
     strcat(buff,"}");
     if(ok)
     {  int  blind0=blind;
        char*inkeyString0=inkeyString;    
        inkeyString=buff;
        blind=1;
        edittable(1,4,&compTab,1,"n_comp",0);
        blind=blind0;
        inkeyString=inkeyString0;
        fillCompositeArray();
        
        strcpy(buff,"{%\\09T(p*)\\09 50{%\\09J(p*,p*)\\09 0.5}");
        inkeyString=buff;
        blind=1;
        edittable(1,4,&cutTab,1,"n_cut",0);
        blind=blind0;
        inkeyString=inkeyString0;
     }   
  }
  { char *ch=getenv("nParProc");
    if(ch) sscanf(ch,"%d",&nPROCSS);
  }  
  goto_xy(10,10); print("Calculation of constraints.  Please, be patient.");
  escpressed();
  n_comphep();
  finish();  
  sortie(0);
}