공유 메모리를 사용하여 Linux에서 관련없는 프로세스간에 통신하고 있습니다. struct ipc_perm에서 지정한 프로세스 만 공유 메모리에 액세스하기를 원합니다. 그러나 코드가 효과가없는 것 같습니다.
프로세스 A : 공유 메모리 생성
int main (int argc, char* argv[]){
int segment_id;
key_t key;
key = 56789;
char* shared_memory;
int shm_size = 512;
segment_id = shmget(key, shm_size, IPC_CREAT | 0666);
if (segment_id < 0){
perror("shmget");
exit(1);
}else {
struct shmid_ds shmbuf;
struct ipc_perm perms;
//here i specified the process whose
//uid is 1234 has the read/write access
//to this shared memory
perms.uid = 1234;
perms.gid = 2000;
perms.mode = 0660;
shmctl(segment_id, IPC_STAT, &shmbuf);
shmbuf.shm_perm = perms;
int ret = shmctl(segment_id, IPC_SET, &shmbuf);
if (ret < 0){
perror("shmctl IPC_SET");
exit(1);
}
}
shared_memory = (char*)shmat(segment_id, NULL, 0);
if (shared_memory == (char*) -1){
perror("shmat");
exit(1);
}
sprintf(shared_memory, "Server Updated The Memory -PID- %lu", getpid());
while(*shared_memory != '*')
sleep(1);
printf("The memory has been updated: \n %s\n", shared_memory);
sleep(5);
shmdt(shared_memory);
shmctl(segment_id, IPC_RMID, 0);
return 0;
}
프로세스 B : 프로세스 A가 생성 한 공유 메모리에 액세스
int main(){
int segment_id;
key_t key;
key = 56789;
char* shared_memory, *s;
int shm_size = 512;
segment_id = shmget(key, shm_size, 0666);
if (segment_id < 0){
perror("shmget");
exit(1);
}
shared_memory = (char*)shmat(segment_id, NULL, 0);
if (shared_memory == (char*) -1){
perror("shmat");
exit(1);
}
for (s = shared_memory; *s != NULL; s++)
putchar(*s);
putchar('\n');
sprintf(shared_memory, "*Client Updated The Memory - pid-%lu", getpid());
return 0;
}
테스트 중에 프로세스 B는 항상 프로세스 A가 만든 공유 메모리에 대한 읽기 / 쓰기 액세스 권한을 가지고 있습니다. 왜 이런 일이 발생합니까? (저는 우분투에서 실행 중이며 두 개의 콘솔을 열어 위의 프로세스를 각각 시작합니다.)
두 프로세스에 UID 1234 또는 GID 2000이 있으면 둘 다 공유 메모리 세그먼트에 액세스 할 수 있어야합니다. 소스에 대한 귀하의 의견 : "uid가 1234 인 프로세스"는 UID (사용자 식별자)라는 용어를 PID (프로세스 식별자)와 혼동하고 있음을 나타냅니다.
내가 아는 한 공유 메모리 세그먼트에 대한 액세스를 PID에 의해 특정 프로세스 세트로 제한하는 방법은 없습니다. 특정 사용자가 실행하는 프로세스로 제한하는 것은 일반적으로 shm_perm.uid
호출 할 때 해당 사용자의 ID를 지정하여 shm_ctl(...IPC_SET...)
충분합니다. 세그먼트에 액세스 할 수있는 프로세스를 제한하려면 세그먼트에 액세스하는 실행 프로세스를 제한하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다