📄 slotmgr.c
字号:
void DumpSharedMemory ( void );/***************************************** * main() - * You know what main does. * Comment block for ease of spotting * it when paging through file * *****************************************/#pragma info(nopar,noret)int main ( int argc, char *argv[], char *envp[]) { int rc; setlocale(LC_ALL, ""); catd = catopen(MF_SLOTD,0); /**********************************/ /* Read in command-line arguments */ /**********************************/ /* FIXME: Argument for daemonizing or not */ /* FIXME: Argument for debug level */ /* FIXME: Arguments affecting the log files, whether to use syslog, etc. (Read conf file?) */ /* Report our debug level */ if ( GetDebugLevel() > DEBUG_NONE) { DbgLog(GetDebugLevel(), "Starting with debugging messages logged at level %d (%d = No messages; %d = few; %d = more, etc.)", GetDebugLevel(), DEBUG_NONE, DEBUG_LEVEL0, DEBUG_LEVEL1); } /* Save our startup directory */ SaveStartupDirectory( argv[0] ); /* Would help to do this before initializing the rest of the data structures */ // SAB XXX this needs to be modified to eliminate the need // for ODM... Currently for ODM based systems this is in odm.c // and the coresponding odm.h..... // for Non-ODM system create a no_odm.h and no_odm.c that // contains this function and does the proper work. if ( ! ReadSlotInfoDB() ) { ErrLog(SLOTD_MSG(SLOTFAIL, "Failed to read slot database.\n")); return 1; } else { DbgLog (DL0, "ReadSlotInfoDB succeeded.\n"); } /* Allocate and Attach the shared memory region */ if ( ! CreateSharedMemory() ) { /* CreateSharedMemory() does it's own error logging */ return 1; } DbgLog(DL0,"SHMID %d token %#X \n", shmid, tok); /* Now that we've created the shared memory segment, we attach to it */ if ( ! AttachToSharedMemeory() ) { /* AttachToSharedMemory() does it's own error logging */ DestroySharedMemory(); return 2; } /* Initialize the global shared memory mutex (and the attribute used to create the per-process mutexes */ if ( ! InitializeMutexes() ) { DetachFromSharedMemory(); DestroySharedMemory(); return 3; } /* Get the global shared memory mutex */#if 1 XProcLock(&(shmp->slt_mutex));#else#ifdef PKCS64 msem_lock(&(shmp->slt_mutex),0);#else pthread_mutex_lock(&(shmp->slt_mutex));#endif#endif /* Populate the Shared Memory Region */ if ( ! InitSharedMemory(shmp) ) {#if 1 XProcUnLock(&(shmp->slt_mutex));#else#ifdef PKCS64 msem_unlock(&(shmp->slt_mutex),0);#else pthread_mutex_unlock(&(shmp->slt_mutex));#endif#endif DetachFromSharedMemory(); DestroySharedMemory(); return 4; } /* Release the global shared memory mutex */#if 1 XProcUnLock(&(shmp->slt_mutex));#else#ifdef PKCS64 msem_unlock(&(shmp->slt_mutex),0);#else pthread_mutex_unlock(&(shmp->slt_mutex));#endif#endif #if TEST_MUTEXES DumpSharedMemory(); #endif /* TEST_MUTEXES */ /* * Become a Daemon, if called for */ if ( Daemon ) { // SAB XXX Here we need to remove the dependency on libdae // since we don't want to port that over to Linux... //#ifdef NODAE { pid_t pid; if ( (pid = fork()) < 0 ){ DestroyMutexes(); DetachFromSharedMemory(); DestroySharedMemory(); return 5; } else { if ( pid != 0) { exit(0); // Terminate the parent } else { setsid(); // Session leader#ifndef DEV fclose(stderr); fclose(stdout); fclose(stdin);#endif } } }#else dae_parent_t parent = DAE_P_OTHER; /* Something other than SRC or inetd */ dae_error_detail_t err_detail; int dae_error = DAE_E_OK; pid_t OldPid = getpid(); dae_init_prevent_zombies( parent, RESTART_SYS_CALLS ); if ( (dae_error = dae_init( &parent, &err_detail )) != DAE_E_OK ) { #ifdef DEV // Only log this information in development builds fprintf(stderr, "Failed to daemonize! dae_init returned %s (%d)\n", DAEConst(dae_error), dae_error); fprintf(stderr, "the routine \"%s\" returned the following information:\n", err_detail.dae_routine); fprintf(stderr, "\"%s\"\n", err_detail.dae_error_string);#endif DestroyMutexes(); DetachFromSharedMemory(); DestroySharedMemory(); return 5; } IveDaemonized = TRUE;#ifdef DEV // Log only on development builds LogLog("DAEMON: Process %d spawned daemon process %d", OldPid, getpid());#endif#endif } else {#ifdef DEV // Log only on development builds LogLog("Not becoming a daemon...\n");#endif } /***************************************** * * Register Signal Handlers * Daemon probably should ignore ALL signals possible, since termination * while active is a bad thing... however one could check for * any processes active in the shared memory, and destroy the shm if * the process wishes to terminate. * *****************************************/ /* * We have to set up the signal handlers after we daemonize because * the daemonization process redefines our handler for (at least) SIGTERM */ if ( ! SetupSignalHandlers() ) { DestroyMutexes(); DetachFromSharedMemory(); DestroySharedMemory(); return 6; } /* ultimatly we will create a couple of threads which monitor the slot db and handle the insertion and removal of tokens from the slot. */ /* For Testing the Garbage collection routines */ /* shmp->proc_table[3].inuse = TRUE; shmp->proc_table[3].proc_id = 24328; */#if !defined(NOGARBAGE)printf("Start garbage \n"); /* start garbage collection thread */ if ( ! StartGCThread(shmp) ) { DestroyMutexes(); DetachFromSharedMemory(); DestroySharedMemory(); return 7; }#endif #if TEST_MUTEXES /* Get the global shared memory mutex */ LogLog ("Grabbing the shared memory mutex"); pthread_mutex_lock(&(shmp->slt_mutex)); #endif /* TEST_MUTEXES */ #if TEST_COND_VARS DumpSharedMemory(); LogLog("Send this process a SIGUSR1 to get it to signal on the condition variable"); #endif /* TEST_COND_VARS */ // We've fully become a daemon. Now create the PID file { FILE *pidfile; pidfile = fopen(PID_FILE_PATH,"w"); if (pidfile) { fprintf(pidfile,"%d",getpid()); fclose(pidfile); } } #pragma info(none) while (1) {#if !(THREADED) && !(NOGARBAGE) GCMain(shmp);#else sleep(10);#endif } #pragma info(restore) /************************************************************* * * Here we need to actualy go through the processes and verify that thye * still exist. If not, then they terminated with out properly calling * C_Finalize and therefore need to be removed from the system. * Look for a system routine to determine if the shared memory is held by * the process to further verify that the proper processes are in the * table. * *************************************************************/} /* end main */#pragma info(restore)#if TEST_MUTEXES || TEST_COND_VARS void DumpSharedMemory ( void ) { u_int32 *p; char Buf[2048]; u_int32 i; #pragma info(none) p = (u_int32 *) shmp; #pragma info(restore) for ( i = 0; i < 15; i++ ) { sprintf(Buf, "%08X %08X %08X %08X", p[0+(i*4)], p[1+(i*4)], p[2+(i*4)], p[3+(i*4)]); LogLog(Buf); } return; }#endif /* TEST_MUTEXES || TEST_COND_VARS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -