Forking version works
This commit is contained in:
parent
a7fd2b390c
commit
81e488e287
72
main.S
72
main.S
|
|
@ -13,16 +13,22 @@
|
||||||
#define PORT 20335
|
#define PORT 20335
|
||||||
|
|
||||||
.data
|
.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
|
.set greeting_len, .-greeting
|
||||||
|
|
||||||
bye: .ascii "Sockets tested!\n"
|
message: .ascii "Client connected\n"
|
||||||
.set bye_len, .-bye
|
.set message_len, .-message
|
||||||
|
|
||||||
errstr: .ascii "Error happened\n"
|
errstr: .ascii "Error happened\n"
|
||||||
.set errstr_len, .-errstr
|
.set errstr_len, .-errstr
|
||||||
|
|
||||||
s: .quad 0
|
s: .quad 0
|
||||||
|
s2: .quad 0
|
||||||
|
|
||||||
true_val: .quad 1
|
true_val: .quad 1
|
||||||
.set true_val_len, .-true_val
|
.set true_val_len, .-true_val
|
||||||
|
|
@ -39,7 +45,7 @@ myaddr:
|
||||||
.text
|
.text
|
||||||
.global _start
|
.global _start
|
||||||
_start:
|
_start:
|
||||||
movq $STDERR, %rdi
|
movq $STDOUT, %rdi
|
||||||
movq $greeting, %rsi
|
movq $greeting, %rsi
|
||||||
movq $greeting_len, %rdx
|
movq $greeting_len, %rdx
|
||||||
call write_buf
|
call write_buf
|
||||||
|
|
@ -78,6 +84,7 @@ _start:
|
||||||
test %rax, %rax
|
test %rax, %rax
|
||||||
jnz error
|
jnz error
|
||||||
|
|
||||||
|
loop:
|
||||||
movq $__NR_accept, %rax
|
movq $__NR_accept, %rax
|
||||||
movq s, %rdi
|
movq s, %rdi
|
||||||
movq $0, %rsi
|
movq $0, %rsi
|
||||||
|
|
@ -86,34 +93,63 @@ _start:
|
||||||
syscall
|
syscall
|
||||||
cmp $-1, %rax
|
cmp $-1, %rax
|
||||||
jz error
|
jz error
|
||||||
|
movq %rax, s2
|
||||||
|
|
||||||
test %rax, %rax
|
movq $STDOUT, %rdi
|
||||||
jz error
|
movq $message, %rsi
|
||||||
|
movq $message_len, %rdx
|
||||||
movq %rax, %rdi
|
|
||||||
movq $STDOUT, %rsi
|
|
||||||
call passthru
|
|
||||||
|
|
||||||
movq $STDERR, %rdi
|
|
||||||
movq $bye, %rsi
|
|
||||||
movq $bye_len, %rdx
|
|
||||||
call write_buf
|
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:
|
exit:
|
||||||
movq $__NR_close, %rax
|
movq $__NR_close, %rax
|
||||||
movq s, %rdi
|
movq s, %rdi
|
||||||
syscall
|
syscall
|
||||||
|
test %rax, %rax
|
||||||
|
jnz error
|
||||||
|
|
||||||
_exit:
|
|
||||||
movq $__NR_exit, %rax
|
movq $__NR_exit, %rax
|
||||||
movq $0, %rdi
|
movq $0, %rdi
|
||||||
syscall
|
syscall
|
||||||
movq $0, 0
|
/* this line is unreachable */
|
||||||
|
|
||||||
error:
|
error:
|
||||||
movq $__NR_write, %rax
|
movq $__NR_write, %rax
|
||||||
movq $STDERR, %rdi
|
movq $STDOUT, %rdi
|
||||||
movq $errstr, %rsi
|
movq $errstr, %rsi
|
||||||
movq $errstr_len, %rdx
|
movq $errstr_len, %rdx
|
||||||
syscall
|
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