//取可用部门共享内存大小 int get_adv_memlen() { int *plen = NULL; int len = 0; //加锁 if (semlock() < 0) return -1; if (!g_pBusiShareMem && GetShm2((void**)&g_pBusiShareMem, DEP_SHM_ID, MAX_DEP_SHMEM_LEN , 0666) < 0) { semunlock(); return -1; } //取已用长度 plen = (int*)g_pBusiShareMem; if (plen == NULL) { semunlock(); return -1; } if (*plen < 0 || *plen > (int)MAX_DEP_VALUE_LEN) { semunlock(); return -1; } len = *plen; semunlock(); return MAX_DEP_VALUE_LEN - len; }
//初始化信号量,在agent启动时调用一次 int init_sem() { if (GetShm2((void**)&g_pBusiShareMem, DEP_SHM_ID , MAX_DEP_SHMEM_LEN , 0666 | IPC_CREAT) < 0) { fprintf(stderr, "Can't create business share memory\n"); return -1; } memset(g_pBusiShareMem , 0 , MAX_DEP_SHMEM_LEN); g_shmLockId = semget(DEP_SHM_LOCK_ID, 1, IPC_EXCL); if(g_shmLockId != -1) { semctl(g_shmLockId, 0, IPC_RMID); } g_shmLockId = semget(DEP_SHM_LOCK_ID, 1, 0666 | IPC_CREAT|IPC_EXCL); if(g_shmLockId < 0) { perror("create signal failed\n"); return -1; } if(semctl(g_shmLockId , 0 , SETVAL,1) < 0) { perror("create signal failed\n"); return -1; } return g_shmLockId; }
int Get_Attr_Value(int attr,int *iValue) { int AttrPos, iRet; // added by noradcai 2009-03-30 增加对共享内存的检查 if(needCheckShm()) { if(CheckShm()!=CHECK_SHM_SUCCESS) { fprintf(stderr, "check shm error!\n"); return -1; } setShmCheckFlag(); } if (!pstAttr&&GetShm2((void**)&pstAttr, ATTR_SHM_ID, getShmSize(), 0666) < 0) { fprintf(stderr, "can not get shm ...continue!\n"); return -1; } iRet = SearchAttrID(pstAttr,attr,&AttrPos); if(iRet == 1)//返回0 attr不存在,返回1 attr存在 { *iValue = pstAttr->astNode[AttrPos].iCurValue; } else { return -1; } return 0; }
/*----------------------------------------------------------------------------- * 初始化hash table *-----------------------------------------------------------------------------*/ int InitHashtable(HashTable *pstHashTable,key_t key, size_t uRowNum, size_t uNodeSize, size_t auNodeNums[], size_t auMods[], int iCreateFlags, Compare compare) { int ret = 0; volatile char *pTable = NULL; if(pstHashTable == NULL || uRowNum == 0) { return -1; } /*key_t uShmKey = CACHE_HASHTABLE_BASE_KEY + key;*/ /*printf("uRowNum = %d uNodeSize = %d \n", uRowNum, uNodeSize);*/ size_t dwTableSize = HashTableEvalTableSize(uRowNum, uNodeSize, auNodeNums); DD("HashTable size = %d\n", (int)dwTableSize); if(dwTableSize == 0) { return -2; } pTable = GetShm(key, dwTableSize, 0666); if(pTable == NULL) { if(iCreateFlags == 0) { printf("shm not existd.should create! shmkey = 0x%x\n", key); return -3; } else if(iCreateFlags == 1) { if((ret = GetShm2((volatile void **)&pTable, key, dwTableSize, 0666 | IPC_CREAT)) < 0) { printf("GetShm2 error. %s\n", strerror(errno)); return -4; } /*printf("first GetShm2 ret = %d\n", ret);*/ bzero((void *)pTable,dwTableSize); } else { return -5; } } /*printf("pTable = 0x%x\n", pTable);*/ /*g_pstHashTable = (HashTable *)pTable;*/ /*pTable += sizeof(HashTable);*/ if((ret = HashTableInit(pstHashTable,(void *)pTable, uNodeSize,uRowNum, auNodeNums, auMods, iCreateFlags, compare)) < 0) { return -5; } /**ppstHashTable = g_pstHashTable;*/ return 0; }
//设置业务部门的数据到共享内存 int adv_attr_set(int attr_id , size_t len , char* pvalue) { char *p = NULL; int *plen = NULL; if (attr_id < DEP_ATTTIBUTE_BASE || len == 0 || pvalue == NULL) return -1; //加锁 if (semlock() < 0) return -1; if (!g_pBusiShareMem && GetShm2((void**)&g_pBusiShareMem, DEP_SHM_ID, MAX_DEP_SHMEM_LEN , 0666) < 0) { semunlock(); return -1;// 修正无法获取共享内存问题,modified by arrowliu, 2006-03-29 } //取可用长度 plen = (int*)g_pBusiShareMem; if (plen == NULL) { semunlock(); return -1; } if (*plen > MAX_DEP_VALUE_LEN || (MAX_DEP_VALUE_LEN - *plen) < len + 8) // modified by arrowliu, 2006-08-10 { semunlock(); return -1; } //copy value p = g_pBusiShareMem; p += sizeof(int);//all the data length p += *plen; *plen += len + 2*sizeof(int);//increase length,一个整形是长度,一个是id //first 4byte is length of data *(int*)p = htonl(len); //second 4byte is attribute id of data p += sizeof(int); *(int*)p = htonl(attr_id); //other buffer for the data p += sizeof(int); memcpy(p , pvalue , len); semunlock(); return 0; }
int Attr_API_Set(int attr,int iValue) { int AttrPos, iRet; // added by noradcai 2009-03-30 增加对共享内存的检查 if(needCheckShm()) { if(CheckShm()!=CHECK_SHM_SUCCESS) { fprintf(stderr, "check shm error!\n"); return -1; } setShmCheckFlag(); } if (!pstAttr&&GetShm2((void**)&pstAttr, ATTR_SHM_ID, getShmSize(), 0666) < 0) { fprintf(stderr, "get share memory error\n"); return -1; } iRet = SearchAttrID(pstAttr,attr,&AttrPos); if(!iRet)//返回0 attr不存在,返回1 attr存在 { pstAttr->astNode[AttrPos].iUse=1; pstAttr->astNode[AttrPos].iAttrID=attr; pstAttr->astNode[AttrPos].iCurValue=iValue; } else if(iRet==-1) { fprintf(stderr, "%s %d: out of memory. maxAttrNode=%d\n", __FILE__, __LINE__,getMaxAttrNode()); return -1; } else { pstAttr->astNode[AttrPos].iCurValue=iValue; } return 0; }
static int _InitRouteTable(key_t uShmKey, int iCreateFlags) { int ret = 0; size_t dwTableSize = 0; char *pTable = NULL; dwTableSize = sizeof(RouteTable) + MAX_ENTRY_NUM * sizeof(RouteEntry); if((pTable = GetShm(uShmKey, dwTableSize, 0666)) == NULL) { if(iCreateFlags == 0) { printf("shm not existd.should create! shmkey = 0x%x\n", uShmKey); return -3; } else if(iCreateFlags == 1) { if((ret = GetShm2((volatile void **)&pTable, uShmKey, dwTableSize, 0666 | IPC_CREAT)) < 0) { printf("GetShm2 error. %s\n", strerror(errno)); return -4; } printf("first GetShm2 error ret = %d but Second Ok.\n", ret); bzero((void *)pTable,dwTableSize); } else { return -5; } } if((ret = RouteTableInit(&gstRouteTable, pTable)) < 0) { return -6; } return 0; }
int free_adv_mem() { int *plen = NULL; // int len = 0; //加锁 if (semlock() < 0) return -1; if (!g_pBusiShareMem && GetShm2((void**)&g_pBusiShareMem, DEP_SHM_ID, MAX_DEP_SHMEM_LEN , 0666) < 0) { semunlock(); return -1; } //取已用长度 plen = (int*)g_pBusiShareMem; if (plen == NULL) { semunlock(); return -1; } if (*plen < 0 || *plen > (int)MAX_DEP_VALUE_LEN) { semunlock(); return -1; } memset(g_pBusiShareMem + sizeof(int) , 0 , *plen); *plen = 0; semunlock(); return 0; }
//取部门共享内存的内容,注意pOut调用者分配,且大小一定要等于或大于len int get_adv_mem(size_t offset , size_t len , char* pOut) { int actual_size = len; int *plen = NULL; if (offset >= MAX_DEP_VALUE_LEN || len < 1 || pOut == NULL) return -1; memset(pOut , 0 , len); //加锁 if (semlock() < 0) return -1; if (!g_pBusiShareMem && GetShm2((void**)&g_pBusiShareMem, DEP_SHM_ID, MAX_DEP_SHMEM_LEN , 0666) < 0) { semunlock(); return -1; } //取已用长度 plen = (int*)g_pBusiShareMem; if (plen == NULL) { semunlock(); return -1; } if (actual_size > *plen) actual_size = *plen; memcpy(pOut , g_pBusiShareMem + sizeof(int) + offset ,actual_size); semunlock(); return actual_size; }