]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/close.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / BsdSocketLib / close.c
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
29 BslSocketCloseWork (
30 IN EFI_SOCKET_PROTOCOL * pSocketProtocol,
31 IN int * pErrno
32 )
33 {
34 EFI_SERVICE_BINDING_PROTOCOL * pServiceBinding;
35 EFI_STATUS Status;
36
37 //
38 // Start closing the socket
39 //
40 Status = pSocketProtocol->pfnCloseStart ( pSocketProtocol,
41 FALSE,
42 pErrno );
43
44 //
45 // Wait for the socket to close or an error
46 //
47 while ( EFI_NOT_READY == Status ) {
48 Status = pSocketProtocol->pfnClosePoll ( pSocketProtocol,
49 pErrno );
50 }
51 if ( !EFI_ERROR ( Status )) {
52 //
53 // Locate the socket protocol
54 //
55 Status = gBS->LocateProtocol ( &gEfiSocketServiceBindingProtocolGuid,
56 NULL,
57 (VOID **) &pServiceBinding );
58 if ( !EFI_ERROR ( Status )) {
59 //
60 // Release the handle
61 //
62 Status = pServiceBinding->DestroyChild ( pServiceBinding,
63 pSocketProtocol->SocketHandle );
64 }
65 if ( EFI_ERROR ( Status )) {
66 *pErrno = EIO;
67 }
68 }
69 else {
70 DEBUG (( DEBUG_ERROR,
71 "ERROR - Failed to close the socket: %r\r\n",
72 Status ));
73 *pErrno = EIO;
74 }
75
76 //
77 // Return the close status
78 //
79 return Status;
80 }
81
82
83 /**
84 Close the socket
85
86 @param [in] pDescriptor Descriptor address for the file
87
88 @returns This routine returns 0 upon success and -1 upon failure.
89 In the case of failure, errno contains more information.
90
91 **/
92 int
93 BslSocketClose (
94 struct __filedes * pDescriptor
95 )
96 {
97 int CloseStatus;
98 EFI_SOCKET_PROTOCOL * pSocketProtocol;
99
100 //
101 // Locate the socket protocol
102 //
103 pSocketProtocol = BslValidateSocketFd ( pDescriptor, &errno );
104 if ( NULL != pSocketProtocol ) {
105 //
106 // Close the socket
107 //
108 BslSocketCloseWork ( pSocketProtocol, &errno );
109 }
110
111 //
112 // Return the close status
113 //
114 CloseStatus = ( errno == 0 ) ? 0 : -1;
115 return CloseStatus;
116 }