;================================================================================= ; System V IPC using Shared Memory ; ; Creates a new or retrieves handle of an available shared memory segment using ; unique key 0x3245490. Then depending on the command-line argument either writes ; a string to the segment, reads segment's content, or destroys the segment. ; ; e.g. ; $ ./sysv_shmem -w hello ; Writing to the shared memory segment... ; $ ./sysv_shmem -r ; Shared memory segment content: hello ; $ ./sysv_shmem -d ; ; ; The LSCR Project. ;================================================================================= format ELF include '../macros.inc' include '../../include/symbols.inc' include '../../include/structs.inc' extrn error_chk section '.text' executable public _start _start: pop eax cmp eax, 2 jb usage mov eax, SYS_IPC ; create new shared mem segment mov ebx, SHMGET mov ecx, 0x3245490 ; we need an unique key. let's hope this one is. mov edx, PAGE_SIZE mov esi, IPC_CREAT or IPC_EXCL int 0x80 test eax, eax jns @f mov eax, SYS_IPC ; segment already exists.. try opening it. mov ebx, SHMGET mov ecx, 0x3245490 mov edx, PAGE_SIZE xor esi, esi int 0x80 ccall error_chk, <"SHMGET has failed: ">, exit @@: mov [shm_id], eax mov eax, SYS_IPC ; attach shared mem segment to the current process mov ebx, SHMAT mov ecx, [shm_id] xor edx, edx mov esi, segptr int 0x80 ccall error_chk, <"SHMAT has failed: ">, exit pop eax pop eax cmp [eax], word "-w" ; write shared mem je write_shm cmp [eax], word "-r" ; read shared mem je read_shm cmp [eax], word "-d" ; delete shared mem segment je delete_shm usage: mov eax, SYS_WRITE ; print usage info mov ebx, STDOUT mov ecx, usage_s mov edx, usage_s_sz int 0x80 exit: mov eax, SYS_EXIT xor ebx, ebx int 0x80 write_shm:;-----------------------------------------------------; mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, writing_s mov edx, writing_s_sz int 0x80 pop esi ; copy string to the shm segment test esi, esi je usage mov edi, [segptr] @@: lodsb stosb test al, al jne @b jmp exit read_shm:;------------------------------------------------------; mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, reading_s mov edx, reading_s_sz int 0x80 mov edi, [segptr] xor edx, edx xor eax, eax @@: inc edx scasb jne @b mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, [segptr] int 0x80 mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, writing_s+writing_s_sz-1 mov edx, 1 int 0x80 jmp exit jmp exit delete_shm:;----------------------------------------------------; mov eax, SYS_IPC ; mark the shm segment for removal mov ebx, SHMCTL mov ecx, [shm_id] mov edx, IPC_RMID xor esi, esi int 0x80 ccall error_chk, <"SHMCTL has failed: ">, exit jmp exit section 'data' writeable usage_s db "USAGE:",0xa db " ./sysv_shmem -w write string to a shared memory segment",0xa db " ./sysv_shmem -r display content of a shared memory segment",0xa db " ./sysv_shmem -d destroy shared memory segment",0xa usage_s_sz = $-usage_s writing_s db "Writing to the shared memory segment...",0xa writing_s_sz = $-writing_s reading_s db "Shared memory segment content: " reading_s_sz = $-reading_s segptr dd 0 shm_id rd 1