sys_fork
Hello list,
I have written a small echo program that listen for a TCP
incoming connection on a port, then send all data received.
This program can accept only a session at time: I would to handle multiple connections using fork(), but in assembly this way is little difficult!
How to hold the controll over the child genereted?
Can anyone help me?
Thanks for every suggestion.
Signature · 5 lines
With regards, HIToC [email protected]
Quoting HIToC <[email protected]>
Sent on Sat, 26 Nov 2005 18:06:03 +0100
Quoted 1 line
How to hold the controll over the child genereted?
with syscalls, like one would normally do with C/C++. :)
have a look under /usr/include/bits/syscall.h, and have fun!
Regards. :)
Signature · 5 lines
Ricardo Nabinger Sanchez GNU/Linux #140696 [http://counter.li.org] Slackware Linux + FreeBSD Left to themselves, things tend to go from bad to worse.
--- HIToC <[email protected]> ha scritto:
Quoted 9 lines
Hello list,
I have written a small echo program that listen for
a TCP
incoming connection on a port, then send all data received.This program can accept only a session at time: I
would to
handle multiple connections usingfork(),
also consider using select(2), which is syscall 82 or poll (kernel 2.2+) which is 168 for descriptor multiplexing to avoid process creation overhead.
Quoted 6 lines
but in assembly this
way is little difficult!
How to hold the controll over the child genereted?Can anyone help me?
Thanks for every suggestion.
Bye,
CLaudio
___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mail.yahoo.it
Using select(2) is a possible solution; but I am interested to understand how to use the fork() system call.. For example, I wish to code in assembly language the following C code [Beej's Guide to Network Programming]:
//...
while(1) { // main accept() loop
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
{
perror("accept");
continue;
}
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
// do something with new_fd socket descriptor
close(new_fd);
exit(0);
}
close(new_fd); // parent doesn't need this
}//...
My problem is to tell the OS to create a new child when a connection is established.
HIToC
Quoted 39 lines · Claudio Fontana
--- HIToC <[email protected]> ha scritto:
Quoted 9 lines
Hello list,
I have written a small echo program that listen for
a TCP
incoming connection on a port, then send all data received.This program can accept only a session at time: I
would to
handle multiple connections usingfork(),also consider using select(2), which is syscall 82 or poll (kernel 2.2+) which is 168 for descriptor multiplexing to avoid process creation overhead.
Quoted 6 lines
but in assembly this
way is little difficult!
How to hold the controll over the child genereted?Can anyone help me?
Thanks for every suggestion.Bye,
CLaudio
___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mail.yahoo.it
-
To unsubscribe from this list: send the line "unsubscribe linux-assembly" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 5 lines
With regards, HIToC [email protected]
Quoting HIToC <[email protected]>
Sent on Sun, 27 Nov 2005 19:22:35 +0100
Quoted 2 lines
My problem is to tell the OS to create a new child when a connection is established.
you'll have a loop blocking on accept(), with a call to fork() in the body. the child will handle the connection, the parent will block again on accept().
can you give more details on your problem? where are you stuck?
Signature · 5 lines
Ricardo Nabinger Sanchez GNU/Linux #140696 [http://counter.li.org] Slackware Linux + FreeBSD Left to themselves, things tend to go from bad to worse.
I have thi piece of code:
wait_new_con: mov eax, [sock_fd]
mov ebx, their_addr
mov ecx, sockaddr_size
mov [their_addr_len], ecx
mov ecx, their_addr_len
push eax
push ebx
push ecx
call accept
test eax, eax
jns st_connection
mov ecx, accept_error
mov edx, accept_error_len
call print_string
mov ebx, 0x01
jmp exit
st_connection: mov [new_sock], eax ; new socket saved!
mov eax, 0x02 ; sys_fork xor ebx, ebx int 0x80 mov eax, 0x06 ; sys_close mov ebx, [sock_fd] int 0x80
echo_loop: mov eax, [new_sock]
mov ebx, buffer
push eax
push ebx
push dword MAX_DATA
push dword 0
call recv
push eax
mov edi, val
call hex2string
mov ecx, bytes
mov edx, bytes_len
call print_string
pop eax
cmp eax, 0x00000000
jz close_conct
mov ecx, eax
mov eax, [new_sock]
mov ebx, buffer
push eax
push ebx
push ecx
push dword 0
call send_all
jmp echo_loop
close_conct: mov eax, 0x06 ; sys_close
mov ebx, [new_sock] int 0x80 mov eax, 0x01 ; sys_exit Child exit? xor ebx, ebx int 0x80 jmp wait_new_con xor ebx, ebx exit: mov eax, 0x01 ; sys_exit int 0x80
When the caller has closed the TCP connection, I want that the child exits, but happens that the entire program exits.
Also, if another client attemps to connect my server, the connecion is refued.
Quoted 11 lines · Ricardo Nabinger Sanchez
Quoting HIToC <[email protected]>
Sent on Sun, 27 Nov 2005 19:22:35 +0100Quoted 2 lines
My problem is to tell the OS to create a new child when a connection is established.
you'll have a loop blocking on
accept(), with a call tofork()in the body. the child will handle the connection, the parent will block again onaccept().can you give more details on your problem? where are you stuck?
Signature · 5 lines
With regards, HIToC [email protected]
HIToC wrote:
...
Quoted 3 lines
mov eax, 0x02 ; sys_fork xor ebx, ebx int 0x80
I don't know much about socket programming, but I got sys_fork to work, once... At this point, you're "two places at once". If eax=0, you're the child (and want to do the echo_loop stuff, I guess). If eax is non-zero - and positive, we hope! - you're the parent, and eax is the PID of the child. In this case, I guess you want to jump back to the "accept" call. By not checking eax at this point, *both* the parent and child fall through into the echo_loop... and the sys_exit. At least, I *think* that's what's happening...
or eax, eax ; or "test eax, eax"
js webefukt ; might be an error!
jnz wait_new_con ; parent
... ; child
See if something like that helps... There may be other things you need to do, besides just going back to "accept" (probably should clean up stack after the call?)...
Best,
Frank
Quoting HIToC <[email protected]>
Sent on Mon, 28 Nov 2005 11:10:51 +0100
Hello,
as Frank said, you're not handling fork() result.
Quoted 6 lines
st_connection: mov [new_sock], eax ; new socket
saved!mov eax, 0x02 ; sys_fork xor ebx, ebx int 0x80
you should test here whether eax is < 0 (error), 0 (child) or > 0 (parent) and act accordingly. but both processes are executing this right away:
Quoted 4 lines
mov eax, 0x06 ; sys_close mov ebx, [sock_fd] int 0x80
and closing their listening socket (the one which is blocked on accept()).
do the program exit with error (EINVAL)?
Signature · 5 lines
Ricardo Nabinger Sanchez GNU/Linux #140696 [http://counter.li.org] Slackware Linux + FreeBSD Left to themselves, things tend to go from bad to worse.
Yes, the problem was that I do not considered the value in EAX! A possible solution to free system resources used by chlid, is to call sys_waitpid with argument pid setted to 0 and waiting until the value returned in EAX is greater than 0...
Now I understand some more thing over the fork system call!!
Thanks a lot!!
HIToC
Quoted 1 line · Ricardo Nabinger Sanchez
you should test here whether eax is < 0 (error), 0 (child) or > 0 (parent)
Signature · 5 lines
With regards, HIToC [email protected]
Quoting HIToC <[email protected]>
Sent on Tue, 29 Nov 2005 19:59:56 +0100
Hello,
Quoted 5 lines
Yes, the problem was that I do not considered the value in EAX! A possible solution to free system resources used by chlid, is to call
sys_waitpidwith argument pid setted to 0 and waiting until the value returned in EAX is greater than 0...
Now I understand some more thing over the fork system call!!
that's nice, have you studied man 2 fork already? :)
I wouldn't recommend you to call wait4pid, because your process (the accept () one) will block, and thus fork() will not be an advantage (you can do that naturally without forking).
What would be a nice thing to do is to prepare a signal handler for SIG_CHILD, to clean up its children. It is very single to use, all you need is to tell the kernel to use your custom function for handling SIG_CHILD, instead of the default action (which is to ignore the signal, and you'll end with a bunch of zombies).
Btw, such a function is like this in C:
void handle_sigchild() {
wait();
}notice that the process will not block, as the signal is being delivered because of a terminating child. it will be cleaned even if the process is blocked in accept().
I'm almost sure that you'll also need to handle interrupted accept(), which will return -1 and errno will have EINTR (interrupted system call). in this case, call accept() again :)
have fun :)
Signature · 5 lines
Ricardo Nabinger Sanchez GNU/Linux #140696 [http://counter.li.org] Slackware Linux + FreeBSD Left to themselves, things tend to go from bad to worse.
If you're going to use signals (a good idea), you should get a good book, like Stevens or Robbins & Robbins. The signal coverage in Robbins & Robbins is more up to date than the orginial Stevens. I haven't seen the second edition of Stevens, but the descriptions say that signal coverage has been expanded.
Quoted 31 lines · Ricardo Nabinger Sanchez
Quoting HIToC <[email protected]>
Sent on Tue, 29 Nov 2005 19:59:56 +0100Hello,
I wouldn't recommend you to call wait4pid, because your process (the accept () one) will block, and thus
fork()will not be an advantage (you can do that naturally without forking).What would be a nice thing to do is to prepare a signal handler for
SIG_CHILD, to clean up its children. It is very single to use, all you need is to tell the kernel to use your custom function for handlingSIG_CHILD, instead of the default action (which is to ignore the signal, and you'll end with a bunch of zombies).Btw, such a function is like this in C:
void handle_sigchild() { wait(); }notice that the process will not block, as the signal is being delivered because of a terminating child. it will be cleaned even if the process is blocked in
accept().I'm almost sure that you'll also need to handle interrupted
accept(), which will return -1 and errno will have EINTR (interrupted system call). in this case, callaccept()again :)have fun :)
Quoting "Robert Plantz" <[email protected]>
Sent on Tue, 29 Nov 2005 20:00:21 -0800
Quoted 3 lines
I haven't seen the second edition
of Stevens, but the descriptions say that signal coverage has been expanded.
if you mean W. Richard Stevens, the second edition is up-to-date. and it's very nice, the new authors made an excellent job.
Signature · 5 lines
Ricardo Nabinger Sanchez GNU/Linux #140696 [http://counter.li.org] Slackware Linux + FreeBSD Left to themselves, things tend to go from bad to worse.