rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <errno.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/ipc.h> |
| 7 | #include <sys/shm.h> |
| 8 | #include "ftp/lynq_ftp.h" |
| 9 | |
| 10 | int lynq_ftpshm_create( void ) |
| 11 | { |
| 12 | key_t key; |
| 13 | int shm_id; |
| 14 | |
| 15 | key = ftok(FTPSHMPATH,FTPSHMID); |
| 16 | shm_id = shmget(key,sizeof(struct ftp_socket_info),0664 | IPC_CREAT | IPC_EXCL); |
| 17 | |
| 18 | if(shm_id < 0) |
| 19 | { |
| 20 | if(errno == EEXIST) |
| 21 | shm_id = shmget(key,sizeof(struct ftp_socket_info),0664 | IPC_CREAT ); |
| 22 | |
| 23 | else |
| 24 | { |
| 25 | LYDBGLOG("[%s-%d] ftp,shm_create_fail", __FUNCTION__, __LINE__); |
| 26 | return -1; |
| 27 | } |
| 28 | } |
| 29 | return shm_id; |
| 30 | } |
| 31 | |
| 32 | lynq_ftp_socker_info *ftpshm_act(int shmid) |
| 33 | { |
| 34 | lynq_ftp_socker_info *p = NULL; |
| 35 | |
| 36 | p = shmat(shmid,NULL,0); |
| 37 | |
| 38 | if(p == NULL) |
| 39 | { |
| 40 | LYDBGLOG("[%s-%d] ftp,shm_act_fail", __FUNCTION__, __LINE__); |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | return p; |
| 45 | } |
| 46 | |
| 47 | void lynq_ftpshm_deact(lynq_ftp_socker_info *shm) |
| 48 | { |
| 49 | int ret; |
| 50 | |
| 51 | ret = shmdt(shm); |
| 52 | |
| 53 | if(ret < 0) |
| 54 | { |
| 55 | LYDBGLOG("[%s-%d] ftp,shm_deact_fail", __FUNCTION__, __LINE__); |
| 56 | return ; |
| 57 | } |
| 58 | |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | void lynq_ftpshm_del(int shmid) |
| 63 | { |
| 64 | int ret; |
| 65 | |
| 66 | ret = shmctl(shmid, IPC_RMID, 0); |
| 67 | |
| 68 | if(ret == -1) |
| 69 | { |
| 70 | LYDBGLOG("[%s-%d] ftp,shm_del_fail", __FUNCTION__, __LINE__); |
| 71 | return ; |
| 72 | } |
| 73 | |
| 74 | return; |
| 75 | } |