修复多进程调用时程序崩溃的bug

修改:      Makefile
	修改:      nicinfo_shm.c

Signed-off-by: 张家岭 <jlzhang@linx-info.com>
This commit is contained in:
张家岭
2019-06-26 13:08:18 +08:00
parent 34e11538ec
commit bd18ca0e9e
2 changed files with 17 additions and 12 deletions

View File

@@ -6,12 +6,15 @@ include $(DEFAULTS)
all:sys_nicmonitor libnic_shm.so test
CFLAGS += -Wall -Wformat=2 -Wno-format-extra-args -Wformat-security -Wformat-nonliteral #-g
#CFLAGS += -D TESTINTERVAL -Wall -Wformat=2 -Wno-format-extra-args -Wformat-security -Wformat-nonliteral -O0 -g
CFLAGS += -Wall -Wformat=2 -Wno-format-extra-args -Wformat-security -Wformat-nonliteral -O0 -g
sys_nicmonitor:mnic.o read_netcard.o send_alarm.o
$(CC) -lman -ldotconf -lpthread -o $@ $^
libnic_shm.so:nicinfo_shm.c
libnic_shm.so:nicinfo_shm.o
$(CC) -fpic -shared -o $@ $^
nicinfo_shm.o:nicinfo_shm.c
$(CC) $(CFLAGS) -fPIC -c $^
test:test.c
gcc -o test test.c -lnic_shm -L=./ -lpthread
runtest:

View File

@@ -33,6 +33,7 @@ SHM *ptr = NULL;
static char process_name[32] = "get_nic_info";
static void linx_free_shm( void *addr, int size );
static int nic_init_flag = 0;
#if 0
static pthread_mutex_t lock_record_log = PTHREAD_MUTEX_INITIALIZER;
@@ -181,8 +182,6 @@ static void *global_shmptr = NULL;
int init_nic_info(void)
{
int fd = -1;
int sem_val;
char err_str[200];
key_t key;
@@ -234,10 +233,6 @@ int init_nic_info(void)
void* shmptr = NULL;
int size = 0;
key_t share_key;
int share_id = 0;
char *ref_count = NULL;
char *flag_str = NULL;
char *path = shm_path;
size = sizeof(SHM);
size = size + 16;
@@ -294,7 +289,6 @@ int get_nic_info(char *nic_name, NETCARD_INFO *net_info)
char *s;
char buf[128];
char err_str[200];
struct passwd *user;
int shm_id;
int shm_key = 0x1d6010;
char *shm_buf;
@@ -339,10 +333,18 @@ int get_nic_info(char *nic_name, NETCARD_INFO *net_info)
strcat(log_path, buf);
LOGPATH_UNLOCK;
if((ret = init_nic_info()) == -1){
return -1;
if (nic_init_flag != 1){
ret = init_nic_info();
if (ret != 0){
return -1;
}
nic_init_flag = 1;
}
get_sem(semid);
// ret = init_nic_info()
// if((ret = init_nic_info()) == -1){
// return -1;
// }
for(i = 0; ptr->info[i].charname[0] != 0; i++){
if(!strcmp(ptr->info[i].charname, nic_name)){
// get_sem(semid);
@@ -369,9 +371,9 @@ int get_nic_info(char *nic_name, NETCARD_INFO *net_info)
static void linx_free_shm( void *addr, int size )
{
return ;
void *shmptr = addr;
char err_str[200];
#if 0
snprintf(err_str, sizeof(err_str), "NOTICE: free shm 0x%x, size:%d, glo:0x%x\n", addr, size, global_shmptr);