Forking version works
This commit is contained in:
parent
a7fd2b390c
commit
81e488e287
78
main.S
78
main.S
|
|
@ -13,16 +13,22 @@
|
|||
#define PORT 20335
|
||||
|
||||
.data
|
||||
greeting: .ascii "Socket test!\n"
|
||||
greeting:
|
||||
.ascii "Welcome to the minimalistic echo server!\n"
|
||||
.ascii "It is written in pure assembly, and it don't use the standard library, only raw system calls.\n"
|
||||
.ascii "\n"
|
||||
.ascii "To exit, press Ctrl+C, as usual\n"
|
||||
.ascii "\n"
|
||||
.set greeting_len, .-greeting
|
||||
|
||||
bye: .ascii "Sockets tested!\n"
|
||||
.set bye_len, .-bye
|
||||
message: .ascii "Client connected\n"
|
||||
.set message_len, .-message
|
||||
|
||||
errstr: .ascii "Error happened\n"
|
||||
.set errstr_len, .-errstr
|
||||
|
||||
s: .quad 0
|
||||
s2: .quad 0
|
||||
|
||||
true_val: .quad 1
|
||||
.set true_val_len, .-true_val
|
||||
|
|
@ -39,7 +45,7 @@ myaddr:
|
|||
.text
|
||||
.global _start
|
||||
_start:
|
||||
movq $STDERR, %rdi
|
||||
movq $STDOUT, %rdi
|
||||
movq $greeting, %rsi
|
||||
movq $greeting_len, %rdx
|
||||
call write_buf
|
||||
|
|
@ -60,7 +66,7 @@ _start:
|
|||
movq $true_val, %r10
|
||||
movq $true_val_len, %r8
|
||||
syscall
|
||||
test %rax, %rax
|
||||
test %rax, %rax
|
||||
jnz error
|
||||
|
||||
movq $__NR_bind, %rax
|
||||
|
|
@ -68,16 +74,17 @@ _start:
|
|||
movq $myaddr, %rsi
|
||||
movq $myaddr_len, %rdx
|
||||
syscall
|
||||
test %rax, %rax
|
||||
test %rax, %rax
|
||||
jnz error
|
||||
|
||||
movq $__NR_listen, %rax
|
||||
movq s, %rdi
|
||||
movq $8, %rsi
|
||||
syscall
|
||||
test %rax, %rax
|
||||
test %rax, %rax
|
||||
jnz error
|
||||
|
||||
loop:
|
||||
movq $__NR_accept, %rax
|
||||
movq s, %rdi
|
||||
movq $0, %rsi
|
||||
|
|
@ -86,34 +93,63 @@ _start:
|
|||
syscall
|
||||
cmp $-1, %rax
|
||||
jz error
|
||||
movq %rax, s2
|
||||
|
||||
test %rax, %rax
|
||||
jz error
|
||||
|
||||
movq %rax, %rdi
|
||||
movq $STDOUT, %rsi
|
||||
call passthru
|
||||
|
||||
movq $STDERR, %rdi
|
||||
movq $bye, %rsi
|
||||
movq $bye_len, %rdx
|
||||
movq $STDOUT, %rdi
|
||||
movq $message, %rsi
|
||||
movq $message_len, %rdx
|
||||
call write_buf
|
||||
|
||||
movq $__NR_fork, %rax
|
||||
syscall /* fork! */
|
||||
cmp $-1, %rax
|
||||
jz error
|
||||
test %rax, %rax
|
||||
jz child
|
||||
/* we're in the parent if we reach this line */
|
||||
|
||||
movq $__NR_close, %rax
|
||||
movq s2, %rdi
|
||||
syscall
|
||||
test %rax, %rax
|
||||
jnz error
|
||||
jmp loop
|
||||
|
||||
exit:
|
||||
movq $__NR_close, %rax
|
||||
movq s, %rdi
|
||||
syscall
|
||||
test %rax, %rax
|
||||
jnz error
|
||||
|
||||
_exit:
|
||||
movq $__NR_exit, %rax
|
||||
movq $0, %rdi
|
||||
syscall
|
||||
movq $0, 0
|
||||
/* this line is unreachable */
|
||||
|
||||
error:
|
||||
movq $__NR_write, %rax
|
||||
movq $STDERR, %rdi
|
||||
movq $STDOUT, %rdi
|
||||
movq $errstr, %rsi
|
||||
movq $errstr_len, %rdx
|
||||
syscall
|
||||
jmp _exit
|
||||
|
||||
movq $__NR_exit, %rax
|
||||
movq $0, %rdi
|
||||
syscall
|
||||
/* this line is unreachable */
|
||||
|
||||
child:
|
||||
/* we're in the child if we reach this line */
|
||||
movq s2, %rdi
|
||||
movq s2, %rsi
|
||||
call passthru
|
||||
|
||||
movq $__NR_close, %rax
|
||||
movq s2, %rdi
|
||||
syscall
|
||||
|
||||
movq $__NR_exit, %rax
|
||||
movq $0, %rdi
|
||||
syscall
|
||||
/* this line is unreachable */
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user