;================================================================================= ; System V IPC using Semaphores ; ; Depending on the command-line argument either creates a new set of semaphores ; using unique key 0x3245435, locks/unlocks specific semaphore, or destroys a ; semaphore set. ; ; e.g. ; $ ./sysv_semaphore -c ; Creating new semaphore set ... ; $ ./sysv_semaphore -l 3 ; Semaphore value: 0x00000003 ; Locking the semaphore ... ; Semaphore value: 0x00000002 ; $ ./sysv_semaphore -u 3 ; Semaphore value: 0x00000002 ; Unlocking the semaphore ... ; Semaphore value: 0x00000003 ; $ ./sysv_semaphore -d ; ; ; The LSCR Project. ;================================================================================= format ELF include '../macros.inc' include '../../include/symbols.inc' include '../../include/structs.inc' extrn error_chk extrn dd2ascii_hex extrn dec_ascii2dd section '.text' executable public _start _start: pop eax cmp eax, 2 jb usage mov [argc], eax pop eax pop eax cmp [eax], word "-c" ; create semaphore set je create_semset cmp [eax], word "-d" ; destroy semaphore set je destroy_semset cmp [eax], word "-l" ; lock semaphore set je lock_semset cmp [eax], word "-u" ; unlock semaphore set je unlock_semset 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 create_semset:;-------------------------------------------------; mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, creating_s mov edx, creating_s_sz int 0x80 mov eax, SYS_IPC ; create new semaphore set mov ebx, SEMGET mov ecx, 0x3245437 ; we need an unique key. let's hope this one is. mov edx, 10 mov esi, IPC_CREAT or IPC_EXCL or S_IRUSR or S_IWUSR int 0x80 ccall error_chk, <"SEMGET has failed: ">, exit mov [semset_id], eax mov eax, SYS_IPC ; setup all semaphores within the set mov ebx, SEMCTL mov ecx, [semset_id] xor edx, edx mov esi, SETALL mov edi, semarrayptr int 0x80 ccall error_chk, <"SEMCTL has failed: ">, exit jmp exit destroy_semset:;------------------------------------------------; mov eax, SYS_IPC ; open the semaphore set mov ebx, SEMGET mov ecx, 0x3245437 xor edx, edx mov esi, S_IRUSR or S_IWUSR int 0x80 ccall error_chk, <"SEMGET has failed: ">, exit mov [semset_id], eax mov eax, SYS_IPC ; remove the semaphore set mov ebx, SEMCTL mov ecx, [semset_id] xor edx, edx mov esi, IPC_RMID mov edi, nullptr int 0x80 ccall error_chk, <"SEMCTL has failed: ">, exit jmp exit lock_semset:;---------------------------------------------------; cmp [argc], 3 jb usage call dec_ascii2dd ; save semaphore index add esp, 4 cmp eax, 10 jbe @f mov ax, 10 @@: mov [_sembuf.sem_num], ax mov eax, SYS_IPC ; open the semaphore set mov ebx, SEMGET mov ecx, 0x3245437 xor edx, edx mov esi, S_IRUSR or S_IWUSR int 0x80 ccall error_chk, <"SEMGET has failed: ">, exit mov [semset_id], eax mov eax, SYS_IPC ; print the semaphore value mov ebx, SEMCTL mov ecx, [semset_id] movzx edx, word [_sembuf.sem_num] mov esi, GETVAL mov edi, nullptr int 0x80 ccall error_chk, <"SEMCTL has failed: ">, exit push eax mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, semval_s mov edx, semval_s_sz int 0x80 pop eax ccall dd2ascii_hex, eax, hexout+2 mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, hexout mov edx, 11 int 0x80 mov eax, SYS_WRITE ; lock the the specified semaphore mov ebx, STDOUT mov ecx, locking_s mov edx, locking_s_sz int 0x80 mov [_sembuf.sem_op], -1 mov [_sembuf.sem_flg], IPC_NOWAIT mov eax, SYS_IPC mov ebx, SEMOP mov ecx, [semset_id] mov edx, 1 mov edi, _sembuf int 0x80 cmp eax, -EAGAIN ; IPC_NOWAIT failure je @f ccall error_chk, <"SEMOP has failed: ">, exit @@: mov eax, SYS_IPC ; print the semaphore value mov ebx, SEMCTL mov ecx, [semset_id] movzx edx, word [_sembuf.sem_num] mov esi, GETVAL mov edi, nullptr int 0x80 ccall error_chk, <"SEMCTL has failed: ">, exit push eax mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, semval_s mov edx, semval_s_sz int 0x80 pop eax ccall dd2ascii_hex, eax, hexout+2 mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, hexout mov edx, 11 int 0x80 jmp exit unlock_semset:;------------------------------------------------; cmp [argc], 3 jb usage call dec_ascii2dd ; save semaphore index add esp, 4 cmp eax, 10 jbe @f mov ax, 10 @@: mov [_sembuf.sem_num], ax mov eax, SYS_IPC ; open the semaphore set mov ebx, SEMGET mov ecx, 0x3245437 xor edx, edx mov esi, S_IRUSR or S_IWUSR int 0x80 ccall error_chk, <"SEMGET has failed: ">, exit mov [semset_id], eax mov eax, SYS_IPC ; print the semaphore value mov ebx, SEMCTL mov ecx, [semset_id] movzx edx, word [_sembuf.sem_num] mov esi, GETVAL mov edi, nullptr int 0x80 ccall error_chk, <"SEMCTL has failed: ">, exit push eax mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, semval_s mov edx, semval_s_sz int 0x80 pop eax ccall dd2ascii_hex, eax, hexout+2 mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, hexout mov edx, 11 int 0x80 mov eax, SYS_WRITE ; unlock the the specified semaphore mov ebx, STDOUT mov ecx, unlocking_s mov edx, unlocking_s_sz int 0x80 mov [_sembuf.sem_op], 1 mov [_sembuf.sem_flg], IPC_NOWAIT mov eax, SYS_IPC mov ebx, SEMOP mov ecx, [semset_id] mov edx, 1 mov edi, _sembuf int 0x80 cmp eax, -EAGAIN ; ignore IPC_NOWAIT failure je @f ccall error_chk, <"SEMOP has failed: ">, exit @@: mov eax, SYS_IPC ; print the semaphore value mov ebx, SEMCTL mov ecx, [semset_id] movzx edx, word [_sembuf.sem_num] mov esi, GETVAL mov edi, nullptr int 0x80 ccall error_chk, <"SEMCTL has failed: ">, exit push eax mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, semval_s mov edx, semval_s_sz int 0x80 pop eax ccall dd2ascii_hex, eax, hexout+2 mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, hexout mov edx, 11 int 0x80 jmp exit section '.data' writeable usage_s db "USAGE:",0xa db " ./sysv_semaphore -c create new semaphore set",0xa db " ./sysv_semaphore -d destroy semaphore set",0xa db " ./sysv_semaphore -l lock semaphore set",0xa db " ./sysv_semaphore -u unlock semaphore set",0xa usage_s_sz = $-usage_s creating_s db "Creating new semaphore set ...",0xa creating_s_sz = $-creating_s locking_s db "Locking the semaphore ...",0xa locking_s_sz = $-locking_s unlocking_s db "Unlocking the semaphore ...",0xa unlocking_s_sz = $-unlocking_s semval_s db "Semaphore value: " semval_s_sz = $-semval_s hexout db "0x00000000",0xa semarrayptr dd semarray semarray dw 0,1,2,3,4,5,6,7,8,9 nullptr dd 0 argc rd 1 semset_id rd 1 _sembuf sembuf