This is a discussion on Shutdown fails with errno zero within the HP-UX Operating System forums, part of the Unix Operating Systems category; --> Hi all, I am puzzled to see that shutdown of a socket sometimes succeeds and sometimes fails with errno ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hi all, I am puzzled to see that shutdown of a socket sometimes succeeds and sometimes fails with errno = 0. In an application that creates and closes a lot of sockets, the following simple code int rc = shutdown( fd, 2 ); if ( rc != 0 ) { printf("Could not shutdown socket %d, errno = %d\n", fd, errno); } sometimes reports Could not shutdown socket 9, errno = 0 The fd is always in the range between 9 and 13 (probably irrelevant). The obligatory close(fd) following the shutdown never reports an error. I could not find any clues on Google, www,hp.com, or the man page. Any ideas would be appreciated ! Thanks, Chris |
| |||
| cbr <chris_breemer@nl.compuware.com> wrote: > I am puzzled to see that shutdown of a socket sometimes succeeds and > sometimes fails with errno = 0. In an application that creates and > closes a lot of sockets, the following simple code > int rc = shutdown( fd, 2 ); First, "never" use magic constants. Use the mnemonic - in this case SHUT_RDWR. > if ( rc != 0 ) > { > printf("Could not shutdown socket %d, errno = %d\n", fd, > errno); > } > sometimes reports > Could not shutdown socket 9, errno = 0 > The fd is always in the range between 9 and 13 (probably > irrelevant). The obligatory close(fd) following the shutdown never > reports an error. I could not find any clues on Google, www,hp.com, > or the man page. Could be a bug - what version of HP-UX, and have you installed the latest patches?-) BTW, what is the value of rc? Also, why are you using SHUT_RDWR? Doing a shutdown(SHUT_RDWR) might just as well be an immediate call to close(). If you are trying to handshake with the remote to see that it got all your data, typically that would be: shutdown(SHUT_WR) poll/select/read/recv to get either a final reply from the remote, or a return of zero indicating remote close (or a timeout or an error etc) close() rick jones -- oxymoron n, commuter in a gas-guzzling luxury SUV with an American flag these opinions are mine, all mine; HP might not want them anyway... feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH... |
| |||
| On 2006-04-06, cbr <chris_breemer@nl.compuware.com> wrote: > I am puzzled to see that shutdown of a socket sometimes succeeds and > sometimes fails with errno = 0. In an application that creates and > closes a lot of sockets, the > following simple code > > int rc = shutdown( fd, 2 ); > if ( rc != 0 ) > { > printf("Could not shutdown socket %d, errno = %d\n", fd, > errno); > } > > sometimes reports > > Could not shutdown socket 9, errno = 0 I see Rick's already given some good advice. Does errno work in any other part of the app? Is errno.h included? Did you compile on the same box you're running it on? -- Elvis Notargiacomo master AT barefaced DOT cheek http://www.notatla.org.uk/goen/ Powergen write "Why not stay with us" - let me count the ways! |
| |||
| all mail refused <elvis-85383@notatla.org.uk> wrote: > Is errno.h included? That's a very good point. And it may matter more if the application is threaded. If it is, then one hopes it was compiled with -D_REENTRANT and linked against -lpthread. rick jones -- No need to believe in either side, or any side. There is no cause. There's only yourself. The belief is in your own precision. - Jobert these opinions are mine, all mine; HP might not want them anyway... feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH... |
| |||
| Thanks guys. Some good tips here ! Yes, <errno.h> has been included. Indeed, we should not use the hardcoded 2 (*blush*). We use the command as part of the connection cleanup, the close() is executed afterwards. Er, dunno what the value of rc is... the man page says it can only be zero or -1 so I test for zero only. That thread thing could be something, I know errno is reliable only in a threadsafe environment. And I'll check the HPUX release of the customer (not the box we compiled on) and look for possible patches. Thanks again! Chris |
| |||
| cbr <chris_breemer@nl.compuware.com> wrote: > Thanks guys. Some good tips here ! > Yes, <errno.h> has been included. > Indeed, we should not use the hardcoded 2 (*blush*). > We use the command as part of the connection cleanup, the close() > is executed afterwards. If there are no socket calls between the shutdown(SHUT_RDWR) and the close(), there is no point in the shutdown call in the first place The only time it makes any sense to call shutdown() before close() is if you use SHUT_RD or SHUT_WR. rick jones -- oxymoron n, Hummer H2 with California Save Our Coasts and Oceans plates these opinions are mine, all mine; HP might not want them anyway... feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH... |
| |||
| Yes, I know close() will get rid of the socket for sure. It may be paranoid to do shutdown and close. Yet it seems like the right thing to do.... Shutdown without close does not clean up the filehandle, and close without shutdown - well I have to trust that close is clever, recognizes the handle as a socket, and does whatever is necessary to clean up the TCP connection. Presumably close() does that on HP-UX, but are we sure it works the same on other UNIX platforms ? Perhaps more a question for a tcp-dedicated group. |
| |||
| cbr <chris_breemer@nl.compuware.com> wrote: > Yes, I know close() will get rid of the socket for sure. It may be > paranoid to do shutdown and close. Yet it seems like the right thing > to do.... Well, it won't _break_ anything, but being a performance guy, I always tweak at unnecessary work being done > Shutdown without close does not clean up the filehandle, Correct. > and close without shutdown - well I have to trust that close is > clever, recognizes the handle as a socket, and does whatever is > necessary to clean up the TCP connection. Presumably close() does > that on HP-UX, but are we sure it works the same on other UNIX > platforms ? Yes. Quite sure. It is functionally equivalent to do shutdown(SHU_RDWR); close() or close(); Where things differ is when you use SHUT_WR or SHUT_RD instead of SHUT_RDWR. rick jones -- The glass is neither half-empty nor half-full. The glass has a leak. The real question is "Can it be patched?" these opinions are mine, all mine; HP might not want them anyway... feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH... |
| |||
| Rick, A belated thanks for your help. I don't know why we do a 2-way shutdown as well as a close. Probably goes back to the dim early days of Bsd/SysV Unix'es. Ah well I guess it is not so important as long as the resource get cleaned up (which they are). Cheers Chris |
| ||||
| cbr <chris_breemer@nl.compuware.com> wrote: > A belated thanks for your help. I don't know why we do a 2-way > shutdown as well as a close. Probably goes back to the dim early > days of Bsd/SysV Unix'es. Ah well I guess it is not so important as > long as the resource get cleaned up (which they are). Did you ever track-down the errno 0 bit? BTW, I noticed you used the term "handle" in a previous post - I take it you do a lot of Windows? Indeed, under Windows there is a difference between a file and a socket, and one cannot call close() on a SOCKET, one has to call closesocket() instead. rick jones ah the things one learns maintaining and porting netperf... http://www.netperf.org/ ftp://ftp.netperf.org/ http://www.netperf.org/svn/netperf2/trunk/ http://www.netperf.org/svn/netperf4/...lib_migration/ -- Wisdom Teeth are impacted, people are affected by the effects of events. these opinions are mine, all mine; HP might not want them anyway... feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH... |