1/*
2 * Copyright (c) 2007, 2008, 2009, 2011, 2012, ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, CAB F.78, Universitaetstrasse 6, CH-8092 Zurich,
8 * Attn: Systems Group.
9 */
10
11#include <unistd.h>
12#include <barrelfish/barrelfish.h>
13#include <vfs/vfs_fd.h>
14#include <stdio.h>
15#include <lwip/sockets.h>
16#include <vfs/fdtab.h>
17#include <sys/epoll.h>
18#include "posixcompat.h"
19#include "pty.h"
20
21__weak_reference(close, _close);
22int close(int fd)
23{
24    int ret;
25    struct fdtab_entry *e = fdtab_get(fd);
26    if (e->type == FDTAB_TYPE_AVAILABLE) {
27        return -1;
28    }
29
30    // Might need to remove from epoll list
31    if(e->epoll_fd != -1) {
32        ret = epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
33        assert(ret == 0);
34    }
35
36    switch(e->type) {
37    case FDTAB_TYPE_LWIP_SOCKET:
38        if (e->inherited) {
39            // Perform shallow close on lwip so that it will not terminate
40            // the TCP session
41            printf("close: Inherited socket, not closing completely\n");
42            ret = 0;
43        } else {
44            ret = lwip_close(e->fd);
45            if(ret < 0) {
46                POSIXCOMPAT_DEBUG("[%d]error in lwip_close\n",
47                        disp_get_domain_id());
48                return -1;
49            }
50        }
51        fdtab_free(fd);
52        break;
53
54    case FDTAB_TYPE_PTM:
55        ret = ptm_close(fd);
56        break;
57
58    case FDTAB_TYPE_PTS:
59        ret = pts_close(fd);
60        break;
61
62    default:
63        ret = vfsfd_close(fd);
64    }
65
66    return ret;
67}
68