oceanbase

Форк
0
87 строк · 3.5 Кб
1
/**
2
 * Copyright (c) 2021 OceanBase
3
 * OceanBase CE is licensed under Mulan PubL v2.
4
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
5
 * You may obtain a copy of Mulan PubL v2 at:
6
 *          http://license.coscl.org.cn/MulanPubL-2.0
7
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10
 * See the Mulan PubL v2 for more details.
11
 */
12

13
static int pipefd_handle_event(pipefd_t *s)
14
{
15
  int ret = 0;
16
  // because edge trigger, a loop is needed.
17
  // Note, if an error occurs while processing a client_fd_info,
18
  // the returned result will still be 0.
19
  while (1) {
20
    client_fd_info_t client_fd_info;
21
    int rbytes = read(s->fd, &client_fd_info, sizeof(client_fd_info));
22
    if (0 == rbytes) {
23
      ret = EAGAIN;
24
      ussl_log_error("read EOF, pipe fd may be closed, pipefd:%d, errno:%d",
25
                     s->fd, errno);
26
      break;
27
    } else if (-1 == rbytes) {
28
      if (EAGAIN != errno && EWOULDBLOCK != errno) {
29
        ussl_log_error("call read failed, pipefd:%d, errno:%d", s->fd, errno);
30
      }
31
      ret = EAGAIN;
32
      break;
33
    } else {
34
      clientfd_sk_t *clientfd_sk = (clientfd_sk_t *)s->fty->create((ussl_sf_t *)s->fty);
35
      if (!clientfd_sk) {
36
        ussl_log_error("create clientfd sock failed, the clientfd will be closed. clientfd:%d, "
37
                       "gid:%lu, errno:%d",
38
                       client_fd_info.client_fd, client_fd_info.client_gid, errno);
39
        if (client_fd_info.client_fd >= 0) {
40
          shutdown(client_fd_info.client_fd, SHUT_WR);
41
          if (0 != libc_epoll_ctl(client_fd_info.org_epfd, EPOLL_CTL_ADD,
42
                                  client_fd_info.client_fd, &client_fd_info.event)) {
43
            ussl_log_error("give back fd to origin epoll failed, fd:%d, errno:%d",
44
                           client_fd_info.client_fd, errno);
45
          }
46
        }
47
      } else {
48
        memcpy(&clientfd_sk->fd_info, &client_fd_info, sizeof(client_fd_info));
49
        clientfd_sk->fd = client_fd_info.client_fd;
50
        clientfd_sk->ep = s->ep;
51
        if (0 != ussl_eloop_regist(clientfd_sk->ep, (ussl_sock_t *)clientfd_sk, EPOLLOUT)) {
52
          ussl_log_warn("[pipefd_handle_event] call eloop_regist failed, the clientfd will be "
53
                         "closed. clientfd:%d, gid:0x%lx, errno:%d",
54
                         client_fd_info.client_fd, client_fd_info.client_gid, errno);
55
          if (client_fd_info.client_fd >= 0) {
56
            shutdown(client_fd_info.client_fd, SHUT_WR);
57
            if (0 != libc_epoll_ctl(client_fd_info.org_epfd, EPOLL_CTL_ADD,
58
                                    client_fd_info.client_fd, &client_fd_info.event)) {
59
              ussl_log_warn("give back fd to origin epoll failed, fd:%d, errno:%d",
60
                             client_fd_info.client_fd, errno);
61
            }
62
          }
63
          s->fty->destroy(s->fty, (ussl_sock_t *)clientfd_sk);
64
        }
65
      }
66
    }
67
  }
68
  return ret;
69
}
70

71
int pipefd_init(ussl_eloop_t *ep, pipefd_t *s, ussl_sf_t *sf, int fd)
72
{
73
  int ret = 0;
74
  ussl_sk_init((ussl_sock_t *)s, sf, (void *)pipefd_handle_event, fd);
75
  s->ep = ep;
76
  if (s->fd < 0) {
77
    ret = -EIO;
78
    errno = EINVAL;
79
    ussl_log_error("pipefd is initialized with an invalid fd, errno:%d", errno);
80
  } else if (0 != (ret = ussl_eloop_regist(ep, (ussl_sock_t *)s, EPOLLIN))) {
81
    ussl_log_error("regist pipefd failed, ret:%d", ret);
82
    if (s->fd >= 0) {
83
      close(s->fd);
84
    }
85
  }
86
  return ret;
87
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.