When call shutdown(), how is socket fd closed?

When call shutdown(), how is socket fd closed?

Post by Jianyong Zhan » Fri, 07 Sep 2001 06:08:01



I have read the source code about the system calls: close() and shutdown()
for closing a socket fd.  In sys_close(), __put_unused_fd() will be
called.  But sys_shutdown() function is :
                err=sock->ops->shutdown(sock, how);
                sockfd_put(sock);

socketfd_put() only calls fput(sock->file).

I can't find how to put unused fd.  I think that shutdown should do it.
May anyone tell me how shutdown() close the socket fd.

 
 
 

When call shutdown(), how is socket fd closed?

Post by r0gu » Fri, 07 Sep 2001 11:23:10




> I have read the source code about the system calls: close() and
> shutdown() for closing a socket fd.  In sys_close(), __put_unused_fd()
> will be called.  But sys_shutdown() function is :
>            err=sock->ops->shutdown(sock, how); sockfd_put(sock);

> socketfd_put() only calls fput(sock->file).

> I can't find how to put unused fd.  I think that shutdown should do it.
> May anyone tell me how shutdown() close the socket fd.

man 2 shutdown will explain this.

--
I will serve no algorithm before it's time.

 
 
 

When call shutdown(), how is socket fd closed?

Post by Jianyong Zhan » Sat, 08 Sep 2001 00:08:49


man 2 shutdown is too simple. In fact, my problem is about shutdown's
implementation in kernel source code, not this API's usage. In kernel
source code, I can't found how to put the socket's fd since it will be
closed.




> > I have read the source code about the system calls: close() and
> > shutdown() for closing a socket fd.  In sys_close(), __put_unused_fd()
> > will be called.  But sys_shutdown() function is :
> >               err=sock->ops->shutdown(sock, how); sockfd_put(sock);

> > socketfd_put() only calls fput(sock->file).

> > I can't find how to put unused fd.  I think that shutdown should do it.
> > May anyone tell me how shutdown() close the socket fd.

> man 2 shutdown will explain this.

> --
> I will serve no algorithm before it's time.

 
 
 

When call shutdown(), how is socket fd closed?

Post by Villy Kru » Sat, 08 Sep 2001 00:40:12


On Thu, 6 Sep 2001 11:08:49 -0400,

Quote:

>man 2 shutdown is too simple. In fact, my problem is about shutdown's
>implementation in kernel source code, not this API's usage. In kernel
>source code, I can't found how to put the socket's fd since it will be
>closed.

It should only be closed in the low level tcp/ip code and only in the
direction specified.  As far as the rest of the kernel and the user's
program is concerned the socket is still open until you close it with
the close() system call, or the process exit().  

Villy

 
 
 

When call shutdown(), how is socket fd closed?

Post by Jianyong Zhan » Sat, 08 Sep 2001 22:42:56


I have checked the low-level tcp/ip code, it seems as if there is no
__put_unused_fd() call. I think the implementation maybe is like the
following:

In sys_shutdown(), sockfd_lookup(fd, &err) will
atomic_inc(file->f_count), then sockfd_put will atomic_dec this count, so
these two function calls will have no effect for socket file close.

So after you call shutdown() in user application, you have to call close()
again in order to close the socket fd. Is it right?


> On Thu, 6 Sep 2001 11:08:49 -0400,

> >man 2 shutdown is too simple. In fact, my problem is about shutdown's
> >implementation in kernel source code, not this API's usage. In kernel
> >source code, I can't found how to put the socket's fd since it will be
> >closed.

> It should only be closed in the low level tcp/ip code and only in the
> direction specified.  As far as the rest of the kernel and the user's
> program is concerned the socket is still open until you close it with
> the close() system call, or the process exit().

> Villy

 
 
 

When call shutdown(), how is socket fd closed?

Post by phil-news-nos.. » Wed, 12 Sep 2001 07:45:37



| I have checked the low-level tcp/ip code, it seems as if there is no
| __put_unused_fd() call. I think the implementation maybe is like the
| following:
|
| In sys_shutdown(), sockfd_lookup(fd, &err) will
| atomic_inc(file->f_count), then sockfd_put will atomic_dec this count, so
| these two function calls will have no effect for socket file close.
|
| So after you call shutdown() in user application, you have to call close()
| again in order to close the socket fd. Is it right?

It would be quite simple to test this for yourself.  Do the shutdown calls
and the close calls in a program, and have that program display an error
message if close() returns an error.  If close() gets an error because
shutdown() caused the fd to no longer be valid, you'll find out quite
quickly.

--
-----------------------------------------------------------------
| Phil Howard - KA9WGN |   Dallas   | http://linuxhomepage.com/ |

-----------------------------------------------------------------

 
 
 

1. Reclaiming used socket fd or how to close sockets properly

Hi all,

I have a question on how to reclaim used socket fd's or possibly it is how
to correctly close socket fd's so that they become available to the system
again.

I am running a server and client programs on Intel based PCs running RH
Linux 6.1 (and couple 6.2)

I eventually run out of socket file descriptors on my server program at
about fd = 64, even though I have only a few sockets open on my server at
that time.  I believe I am correctly closing sockets when I am done with
them (which I assume means that the resource would be given back to the
operating system).

My server program has several socket file descriptors open at once ( I use
select() to see if any are available for reading, or if there is a new
connection waiting on the listen socket), and occasionally the clients will
go away for what ever reason, and also new clients can join at any time.  If
a client dies or goes away, either explicitly (read() =0) or due to long
periods of inactivity(I have a tunable keep alive function), the server
calls shutdown(2) on that socket fd.  The argument 2 to shutdown being that
neither sending or receiving is allowed. The reason for using shutdown()
rather than close() was either based on something I read in the Stevens book
or one of the Unix socket FAQ's, don't remember which, having to do with
close only referring to the current thread and shutdown means that everyone
is done with the fd (although my app is a single thread so theoretically
either should work)

If a new client attempts to connect, the socket call returns a new file
descriptor which is always incrementing.  So as clients leave, and new
clients appear, the socket numbers increase until 64 at which time I get an
error which is something like "invalid file descriptor".

I know there is a way to increase the resources available system wide,
although I cant seem to find that reference, so feel free to remind me what
the command/syntax to increase resources is.  But even that is a stop gap
measure, since eventually I will run out of socket file descriptors again.

Based on what I need to do, spawning new processes like an http server, will
not work for me.  The only other solution that I could think of would be to
go to UDP exclusively and that way only one socket would be needed.
Although I would much prefer to figure out a way to make the operating
system recycle the no longer used socket file descriptors.

Any thought, or suggestions would be appreciated.

Thanks,

Steve
--
reply to newsgroup and/or reply to my hotmail address tigerman67

2. Daimond S90 PCI Sound Card config

3. shutdown(s, 2) vs close(s) for closing unix domain sockets

4. Swap size

5. What's difference between close and shutdown(fd, 1)?

6. AutoPath 4 AIX

7. AXI TCPIP stack not not sending FIN when server program calls close(fd)

8. Apache & passwords

9. Closing socket fd

10. why parent close socket fd before child finish?

11. read(fd, buf, size) after shutdown(fd, SHUT_RD) implementation-defined

12. Select call not able to detect the change in socket fd

13. why does socket() call return 0 for FD