1 /** @file
2   Implement the close API.
3 
4   Copyright (c) 2011, Intel Corporation
5   All rights reserved. This program and the accompanying materials
6   are licensed and made available under the terms and conditions of the BSD License
7   which accompanies this distribution.  The full text of the license may be found at
8   http://opensource.org/licenses/bsd-license.php
9 
10   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 #include <SocketInternals.h>
16 
17 
18 /**
19   Worker routine to close the socket.
20 
21   @param[in] pSocketProtocol   Socket protocol structure address
22 
23   @param[in] pErrno            Address of the ::errno variable
24 
25   @retval EFI_SUCCESS   Successfully closed the socket
26 
27 **/
28 EFI_STATUS
BslSocketCloseWork(IN EFI_SOCKET_PROTOCOL * pSocketProtocol,IN int * pErrno)29 BslSocketCloseWork (
30   IN EFI_SOCKET_PROTOCOL * pSocketProtocol,
31   IN int * pErrno
32   )
33 {
34   EFI_STATUS Status;
35 
36   //
37   //  Start closing the socket
38   //
39   Status = pSocketProtocol->pfnCloseStart ( pSocketProtocol,
40                                             FALSE,
41                                             pErrno );
42 
43   //
44   //  Wait for the socket to close or an error
45   //
46   while ( EFI_NOT_READY == Status ) {
47     Status = pSocketProtocol->pfnClosePoll ( pSocketProtocol,
48                                              pErrno );
49   }
50   if ( !EFI_ERROR ( Status )) {
51     //
52     //  Release the socket resources
53     //
54     *pErrno = EslServiceFreeProtocol ( pSocketProtocol );
55   }
56   else {
57     DEBUG (( DEBUG_ERROR,
58               "ERROR - Failed to close the socket: %r\r\n",
59               Status ));
60     *pErrno = EIO;
61   }
62 
63   //
64   //  Return the close status
65   //
66   return Status;
67 }
68 
69 
70 /**
71   Close the socket
72 
73   The BslSocketClose routine is called indirectly from the close file
74   system routine.  This routine closes the socket and returns the
75   status to the caller.
76 
77   @param[in] pDescriptor Descriptor address for the file
78 
79   @return   This routine returns 0 upon success and -1 upon failure.
80             In the case of failure, ::errno contains more information.
81 
82 **/
83 int
84 EFIAPI
BslSocketClose(struct __filedes * pDescriptor)85 BslSocketClose (
86   struct __filedes * pDescriptor
87   )
88 {
89   int CloseStatus;
90   EFI_SOCKET_PROTOCOL * pSocketProtocol;
91 
92   //
93   //  Locate the socket protocol
94   //
95   pSocketProtocol = BslValidateSocketFd ( pDescriptor, &errno );
96   if ( NULL != pSocketProtocol ) {
97     //
98     //  Close the socket
99     //
100     BslSocketCloseWork ( pSocketProtocol, &errno );
101   }
102 
103   //
104   //  Return the close status
105   //
106   CloseStatus = ( errno == 0 ) ? 0 : -1;
107   return CloseStatus;
108 }
109