;================================================================================= ; POSIX IPC using Message Queues ; ; Creates new or opens an available message queue named "myqueue". Then depending ; on the command-line argument either sends a new message, receives a message, or ; destroys the queue. ; ; e.g. ; $ ./posix_msg -s Hello ; Sending a message ... ; $ ./posix_msg -r ; Receiving a message ... ; Message: Hello ; $ ./posix_msg -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 [argc], eax mov [mqattr+mq_attr.mq_flags], 0 ; setup queue attributes mov [mqattr+mq_attr.mq_maxmsg], 10 mov [mqattr+mq_attr.mq_msgsize], 256 mov [mqattr+mq_attr.mq_curmsgs], 0 mov eax, SYS_MQ_OPEN ; open/create the queue mov ebx, queue_name mov ecx, O_RDWR or O_CREAT mov edx, S_IWUSR or S_IRUSR mov esi, mqattr int 0x80 ccall error_chk, <"SYS_MQ_OPEN has failed: ">, exit mov [msgqueue_id], eax pop eax pop eax cmp [eax], word "-s" ; send message je send_message cmp [eax], word "-r" ; receive message je receive_message cmp [eax], word "-d" ; delete queue je delete_queue 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 send_message:;--------------------------------------------------; cmp [argc], 3 jb usage mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, sending_s mov edx, sending_s_sz int 0x80 pop ecx ; calculate message length mov edi, ecx xor eax, eax xor edx, edx @@: inc edx scasb jne @b mov eax, SYS_MQ_TIMEDSEND ; send the message mov ebx, [msgqueue_id] xor esi, esi xor edi, edi int 0x80 ccall error_chk, <"SYS_MQ_TIMEDSEND has failed: ">, exit mov eax, SYS_CLOSE mov ebx, [msgqueue_id] int 0x80 ccall error_chk, <"SYS_CLOSE has failed: ">, exit jmp exit receive_message:;-----------------------------------------------; mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, receiving_s mov edx, receiving_s_sz int 0x80 mov eax, SYS_MQ_TIMEDRECEIVE ; receive any message on the given queue ID mov ebx, [msgqueue_id] mov ecx, _msgbuf mov edx, 256 xor esi, esi xor edi, edi int 0x80 ccall error_chk, <"SYS_MQ_TIMEDRECEIVE has failed: ">, exit mov eax, SYS_WRITE ; print message data mov ebx, STDOUT mov ecx, msg_data_s mov edx, msg_data_s_sz int 0x80 mov edi, _msgbuf ; calculate message data size xor eax, eax xor edx, edx @@: inc edx scasb jne @b mov [edi-1], byte 0xa mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, _msgbuf int 0x80 mov eax, SYS_CLOSE mov ebx, [msgqueue_id] int 0x80 ccall error_chk, <"SYS_CLOSE has failed: ">, exit jmp exit delete_queue:;--------------------------------------------------; mov eax, SYS_MQ_UNLINK ; delete message queue mov ebx, queue_name int 0x80 ccall error_chk, <"SYS_MQ_UNLINK has failed: ">, exit jmp exit section '.data' writeable usage_s db "USAGE:",0xa db " ./posix_msg -s send message",0xa db " ./posix_msg -r receive message",0xa db " ./posix_msg -d destroy the message queue",0xa usage_s_sz = $-usage_s queue_name db "myqueue",0 sending_s db "Sending a message ...",0xa sending_s_sz = $-sending_s receiving_s db "Receiving a message ...",0xa receiving_s_sz = $-receiving_s msg_data_s db "Message: " msg_data_s_sz = $-msg_data_s argc rd 1 msgqueue_id rd 1 mqattr mq_attr _msgbuf rb 256