577 Commits

Author SHA1 Message Date
Azat Khuzhin
9a9b92ed06 Add EVENT_BASE_FLAG_EPOLL_DISALLOW_TIMERFD flag (fixes: #958)
By default we are using CLOCK_MONOTONIC_COARSE, but if
EVENT_BASE_FLAG_PRECISE_TIMER isset, then CLOCK_MONOTONIC will be used,
however this will also enable timerfd, while this is not always what
someone wants, hence add a flag to control this (by default the old
behavior is preserved, set new flag to change it).
2020-03-01 18:03:46 +03:00
Azat Khuzhin
5a5fe67ee8 Fix typo in thread.h (s/event/evthread) 2020-03-01 13:52:32 +03:00
yangyongsheng
08e7d5136d misspelling of output in bufferevent_struct.h 2020-01-28 14:31:39 +08:00
nntrab
02905413fd Add callback support for error pages
The existing error pages are very basic and don't allow for
multi-lingual support or for conformity with other pages in a web site.
The aim of the callback functionality is to allow custom error pages to
be supported for calls to evhttp_send_error() by both calling
applications and Libevent itself.

A backward-incompatible change has been made to the title of error pages
sent by evhttp_send_error(). The original version of the function used
the reason argument as part of the title. That might have unforeseen
side-effects if it contains HTML tags. Therefore the title has been
changed to always use the standard status text.

An example of the error callback can be found in this
[version](https://github.com/libevent/libevent/files/123607/http-server.zip)
of the 'http-server' sample. It will output error pages with very bright
backgrounds, the error code using a very large font size and the reason.

Closes: #323 (cherr-picked from PR)
2020-01-13 00:50:14 +03:00
Andre Pereira Azevedo Pinto
f76456b0dc Add support for priority inheritance
Add support for posix mutex priority inheritance. This is important to
avoid priority inversion in systems running with threads with different
priorities.

Signed-off-by: Andre Azevedo <andre.azevedo@gmail.com>
2019-12-09 14:55:31 -08:00
Philip Homburg
9fecb59a94 Parse IPv6 scope IDs. 2019-11-06 21:07:16 +03:00
yuangongji
5aae74d7a6 improve the description of parameter to evbuffer_read() 2019-09-22 22:57:47 +08:00
yuangongji
620a3fa1d5 Doxygen documentation improvements
- Documentation for `bufferevent_compat.h` and `rpc.h` is not generated
  since the `@file` command is missing. It can be fixed by adding
  `@file` in file comment block.
- The briefs of buffer.h,bufferevent.h and some other files are missing.
  Adding `@brief` command can fix it.
- The parameters in the function declaration are different from the
  parameters following the `@param` command.We should change them to the
  same.
- Documentation of `watch.h` is not generated since `watch.h` has not
  been added to the Doxyfile `INPUT` tag.

- Add link to the watch.h in event.h
2019-08-29 23:08:12 +03:00
Azat Khuzhin
bdcade4722
buffer: fix possible NULL dereference in evbuffer_setcb() on ENOMEM
[ @azat:

  - add return heredoc for evbuffer_setcb()
  - add unit test with event_set_mem_functions()
  - look through the report from abi-compliance-checker/abi-dumper
]

Closes: #855
2019-07-31 10:43:57 +03:00
Azat Khuzhin
538141eb7e
evdns: add new options -- so-rcvbuf/so-sndbuf
This will allow to customize SO_RCVBUF/SO_SNDBUF for nameservers in this
evdns_base, you may want to adjust them if the kernel starts dropping
udp packages.
2019-06-15 23:32:39 +03:00
yuangongji
c03dabd76a typo error in header file 2019-05-31 02:12:55 +08:00
Joseph Spadavecchia
5e137f3776
Implement bufferevent_socket_connect_hostname_hints()
So that ai_flags (such as AI_ADDRCONFIG) can be specified.

Closes: #193 (cherry-picked with conflicts resolved)
2019-05-13 11:13:04 +03:00
Tobias Stoeckmann
176fd56655
Protect min_heap_push_ against integer overflow.
Converting unsigned to size_t for size of memory objects allows
proper handling of very large heaps on 64 bit systems.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Closes: #799 (cherry-picked)
2019-04-22 23:51:25 +03:00
Thomas Bernard
8dcb94a4ca
Added http method extending
User can define his own response method by calling
evhttp_set_ext_method_cmp() on the struct http, or
evhttp_connection_set_ext_method_cmp() on the connection.

We expose a new stucture `evhttp_ext_method` which is passed to the
callback if it's set. So any field can be modified, with some exceptions
(in evhttp_method_):

If the cmp function is set, it has the ability to modify method, and
flags. Other fields will be ignored. Flags returned are OR'd with the
current flags.

Based on changes to the #282 from: Mark Ellzey <socket@gmail.com>
2019-04-11 22:48:01 +03:00
Thomas Bernard
96e56beb94
http: Update allowed_methods field from 16 to 32bits. 2019-04-11 22:47:55 +03:00
Azat Khuzhin
55f9863b18
Remove experimental note for finalizers API 2019-04-07 04:57:32 +03:00
Dan Rosen
2f184f8bbf evwatch: Add "prepare" and "check" watchers.
Adds two new callbacks: "prepare" watchers, which fire immediately
before we poll for I/O, and "check" watchers, which fire immediately
after we finish polling and before we process events. This allows other
event loops to be embedded into libevent's, and enables certain
performance monitoring.

Closes: #710
2019-04-03 12:44:50 -04:00
Azat Khuzhin
e5b8f4c192
evdns: add DNS_OPTION_NAMESERVERS_NO_DEFAULT/EVDNS_BASE_NAMESERVERS_NO_DEFAULT
- DNS_OPTION_NAMESERVERS_NO_DEFAULT
  Do not "default" nameserver (i.e. "127.0.0.1:53") if there is no nameservers
  in resolv.conf, (iff DNS_OPTION_NAMESERVERS is set)

- EVDNS_BASE_NAMESERVERS_NO_DEFAULT
  If EVDNS_BASE_INITIALIZE_NAMESERVERS isset, do not add default
  nameserver if there are no nameservers in resolv.conf (just set
  DNS_OPTION_NAMESERVERS_NO_DEFAULT internally)

Fixes: #569
2019-04-01 02:18:59 +03:00
Azat Khuzhin
3e0dc1a6ab
evdns: add descriptions for DNS_OPTION_*/DNS_OPTIONS_ALL 2019-04-01 01:41:10 +03:00
Azat Khuzhin
5357c3d62a
Adjust evbuffer max read for bufferevents 2019-03-16 17:33:27 +03:00
Azat Khuzhin
8c2001e92a
Maximum evbuffer read configuration
Before this patch evbuffer always reads 4K at a time, while this is fine
most of time you can find an example when this will decrease throughput.

So add an API to change default limit:
- evbuffer_set_max_read()
- evbuffer_get_max_read()

And a notice that most of time default is sane.
2019-03-16 17:30:15 +03:00
Alexander Drozdov
68eb526d7b http: add WebDAV methods support
WebDAV introduced new HTTP methods (RFC4918):
PROPFIND, PROPPATCH, MKCOL, LOCK, UNLOCK, COPY, MOVE.

Add support of the methods.
2019-03-14 11:12:46 +03:00
Azat Khuzhin
5ee507c889
http: implement separate timeouts for read/write/connect phase
This patch allows to change timeout for next events read/write/connect
separatelly, using new API:

- client:
  evhttp_connection_set_connect_timeout_tv() -- for connect
  evhttp_connection_set_read_timeout_tv()    -- for read
  evhttp_connection_set_write_timeout_tv()   -- for write

- server:
  evhttp_set_read_timeout_tv()  -- for read
  evhttp_set_write_timeout_tv() -- for write

It also changes a logic a little, before there was next fallbacks which
does not handled in new API:
- HTTP_CONNECT_TIMEOUT
- HTTP_WRITE_TIMEOUT
- HTTP_READ_TIMEOUT

And introduce another internal flag (EVHTTP_CON_TIMEOUT_ADJUSTED) that
will be used in evrpc, which adjust evhttp_connection timeout only if it
is not default.

Fixes: #692
Fixes: #715
2019-03-05 00:33:46 +03:00
John Ohl
62df1301ca
Add support for EV_TIMEOUT to event_base_active_by_fd
Closes: #194 (cherry-pick)
2019-03-03 23:42:03 +03:00
Enji Cooper
5f87be42f0
Define _GNU_SOURCE properly/consistently per autoconf
Although `_GNU_SOURCE` can be defined as an arbitrary #define per the
glibc docs [1], it's best to define it in a manner consistent with the way
that autoconf defines it, i.e., `1`.

While this shouldn't matter in most cases, it does when the headers from
other projects follow the poorly defined GNU convention implemented by
autoconf and are included after the libevent's util.h header. An example
failure with clang, similar to the failure I encountered, is as follows:
```
$ printf "#define _GNU_SOURCE\n#define _GNU_SOURCE 1" | clang -c -x c -
<stdin>:2:9: warning: '_GNU_SOURCE' macro redefined [-Wmacro-redefined]
        ^
<stdin>:1:9: note: previous definition is here
        ^
1 warning generated.
```

This happened when compiling python [2] with a stale homebrew util.h file from
libevent (which admittedly would not happen in a correct libevent install, as the
header should be installed under /usr/local/include/event2/util.h). However, if
both headers had been combined (which is more likely), it would have failed as
shown above.

Removing the ad hoc definition unbreaks compiling python's pyconfig.h.in header
when included after util.h from libevent.

1. http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
2. https://github.com/python/cpython/blob/master/configure.ac#L126

Closes: #773 (cherry-picked)

Signed-off-by: Enji Cooper <yaneurabeya@gmail.com>
2019-02-27 23:18:12 +03:00
Azat Khuzhin
b29207dcee
Eliminate fd conversion warnings and introduce EVUTIL_INVALID_SOCKET (windows)
windows has intptr_t instead of regular int.

Also tt_fd_op() had been introduced, since we cannot use tt_int_op() for
comparing fd, since it is not always int.
2019-01-29 22:03:08 +03:00
Azat Khuzhin
99b231b0d8
rpc: use *_new_with_arg() to match function prototype
In 755fbf16c ("Add void* arguments to request_new and reply_new
evrpc hooks") this new functions had been introduced, but newer used,
what for? So let's use them.
2019-01-11 21:53:07 +03:00
Azat Khuzhin
9cba915ee8
Introduce EVENT_VISIBILITY_WANT_DLLIMPORT
And use it in places where event_debug() should be called (since it
requires access to "event_debug_logging_mask_" and in win32 it is
tricky).

One of this places that is covered by this patch is the test for
event_debug().
2018-10-28 19:45:57 +03:00
Azat Khuzhin
23e79fd764
Check existence of IPV6_V6ONLY in evutil_make_listen_socket_ipv6only() (mingw32)
MinGW 32-bit 5.3.0 does not defines it and our appveyour [1] build
reports this instantly:
    evutil.c: In function 'evutil_make_listen_socket_ipv6only':
    evutil.c:392:40: error: 'IPV6_V6ONLY' undeclared (first use in this function)
      return setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*) &one,

  [1]: https://www.appveyor.com/docs/windows-images-software/#mingw-msys-cygwin

Another solution will be to use mingw64 which has it, but I guess we do
want that #ifdef anyway.
2018-10-28 18:39:20 +03:00
Azat Khuzhin
26ef859aa7
Add evhttp_parse_query_str_flags()
And a set of flags:
- EVHTTP_URI_QUERY_LAST
- EVHTTP_URI_QUERY_NONCONFORMANT

Fixes: #15
2018-10-27 17:59:11 +03:00
Murat Demirten
387d91f9ab
listener: ipv6only socket bind support
According to RFC3493 and most Linux distributions, default value is to
work in IPv4-mapped mode. If there is a requirement to bind same port
on same ip addresses but different handlers for both IPv4 and IPv6,
it is required to set IPV6_V6ONLY socket option to be sure that the
code works as expected without affected by bindv6only sysctl setting
in system.

See an example working with this patch:
https://gist.github.com/demirten/023008a63cd966e48b0ebcf9af7fc113

Closes: #640 (cherry-pick)
2018-10-26 07:24:29 +03:00
Azat Khuzhin
5a455acd3b
Fix hangs due to watermarks overruns in bufferevents implementations
Some implementations of bufferevents (for example openssl) can overrun
read high watermark.
And after this if user callback will not drain enough data it will be
suspended (i.e. it will not be runned again anymore).
This is not the expecting behaviour as one may guess, since in this case
the data will never be read. Hence once we detected that the watermark
exceeded (even after calling user callback) we will schedule the
callback again.

This also can be fixed in bufferevent openssl implementation (by
strictly limiting how much data is added to the read buffer according to
read high watermark), but since this data is already available (and in
memory) there is no point in doing so.
2018-10-17 23:21:17 +03:00
Xiaozhou Liu
30d77f1b59
Fix typo
Closes: #658
2018-07-30 00:50:32 +03:00
Philip Prindeville
d2acf67ec3
Add convenience macros for user-triggered events
Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Closes: #647 (picked)
2018-06-18 03:13:12 +03:00
Azat Khuzhin
c57f5c3490 Make rpc headers self-compilable
Fixes: #633
2018-05-07 02:40:16 +03:00
Nathan French
1af8017648
[core] re-order fields in struct event for memory efficiency
The sizeof `struct event` can reduced on both 32 bit and 64 bit systems
by moving the 4 bytes that make up `ev_events` and `ev_res` below `ev_fd`,
before `struct event_base * ev_base;` since our compiler wouldn't dare do
such a thing (it instead will pad twice, whereas it only needs to be padded
once)

```C
struct event {
                                                         /*  OFFS |  SZ   Bytes  | Total Bytes | START - END  */
    struct event_callback ev_evcallback;                 /*   0x0 |  40          | 40          | 0x0   - 0x28 */

    union {                                              /*  0x28 |  ----------- | ----------- | ------------ */
        TAILQ_ENTRY(event) ev_next_with_common_timeout;  /*       |       ((16)) |             |              */
        int min_heap_idx;                                /*       |       ((04)) |             |              */
    }   ev_timeout_pos;                                  /*       |  16          | 56          | 0x28  - 0x38 */
    int ev_fd;                                           /*  0x38 |  04          | 60          | 0x38  - 0x3c */
```

Since the next field is 8 bytes in length, and we are up to 60 bytes, `ev_fd` ends up being padded (4 more bytes on 64b).

```C
                              /* --- 1 byte gap HERE --->            1           | <61>                      */
                              /* --- 1 byte gap HERE --->            1           | <62>                      */
                              /* --- 1 byte gap HERE --->            1           | <63>                      */
                              /* --- 1 byte gap HERE --->            1           | <64>                      */
    struct event_base * ev_base;                         /*  0x3c |  8           | 68         | 0x3c - 0x40  */
    union {                                              /*  0x40 | ------------ | ---------- | ------------ */
        struct {                                         /*       | ------------ |            |              */
            LIST_ENTRY (event) ev_io_next;               /*       |       ((16+  |            |              */
            struct timeval ev_timeout;                   /*       |         16)) |            |              */
        } ev_io;                                         /*       | ((32))       |            |              */
        struct {                                         /*       | ------------ |            |              */
            LIST_ENTRY (event) ev_signal_next;           /*       |      ((16+   |            |              */
            short   ev_ncalls;                           /*       |        02+   |            |              */
            short * ev_pncalls;                          /*       |        08))  |            |              */
        } ev_signal;                                     /*       | ((26))       |            |              */
    } ev_;                                               /*  0x60 | 32           | 100        | 0x40 - 0x60  */

    short ev_events;                                     /*  0x60 | 2            | 102        | 0x60 - 0x62  */
    short ev_res;                                        /*  0x62 | 2            | 104        | 0x62 - 0x64  */
```

We now hit another line, `struct timeval` is 16 bytes on 64b arch, so we have 4 more bytes
of padding on `ev_res`.

```C
    /* --- 1 byte gap HERE --- */
    /* --- 1 byte gap HERE --- */
    /* --- 1 byte gap HERE --- */
    /* --- 1 byte gap HERE --- */
    struct timeval ev_timeout;                           /*  0x64 | 16           | 120        | 0x64 - 0x74  */
};
```

After moving `ev_events` and `ev_res` below `ev_fd` we have something
a bit more optimal:

```C
struct event2 {
                                                         /*  OFFS |  SZ / Bytes  | RSUM Bytes | START - END  */
    struct event_callback ev_evcallback;                 /*   0x0 | 40           | 40         | 0x0   - 0x28 */
    union {                                              /*  0x28 | ------------ | ---------- | ------------ */
        TAILQ_ENTRY(event) ev_next_with_common_timeout;  /*       |       ((16)) |            |              */
        int min_heap_idx;                                /*       |       ((04)) |            |              */
    } ev_timeout_pos;                                    /*       | 16           | 56         | 0x28  - 0x38 */
    int ev_fd;                                           /*  0x38 | 4            | 60         | 0x38  - 0x3c */
    short ev_events;                                     /*  0x3c | 2            | 62         | 0x3c  - 0x3e */
    short ev_res;                                        /*  0x3e | 2            | 64         | 0x3e  - 0x40 */

    struct event_base * ev_base;                         /*  0x40 | 8            | 74         | 0x40  - 0x48 */
    union {                                              /*  0x48 | ------------ | ---------- | ------------ */
        struct {                                         /*       | ------------ |            |              */
            LIST_ENTRY (event) ev_io_next;               /*       |        ((16+ |            |              */
            struct timeval ev_timeout;                   /*       |         16)) |            |              */
        } ev_io;                                         /*       | ((32))       |            |              */
        struct {                                         /*       | ------------ |            |              */
            LIST_ENTRY (event) ev_signal_next;           /*       |       ((16+  |            |              */
            short   ev_ncalls;                           /*       |         02+  |            |              */
            short * ev_pncalls;                          /*       |         08)) |            |              */
        } ev_signal;                                     /*       | ((26))       |            |              */
    } ev_;                                               /*       | 32           | 106        | 0x48 - 0x68  */
    struct timeval ev_timeout;                           /* 0x68  | 16           | 120        | 0x68 - 0x78  */
};
```

We still have a gap here, but the first was removed.

Again, we can save 8 bytes on both 32 and 64 word sizes (32/64 byte cacheline).

Below are the results for testing v2.1.6 -> master -> master + this patch (Release/-O3)

Code:

```C
  #include <event2/event.h>

  int
  main(int argc, char ** argv) {
      printf("%zu\n", event_get_struct_event_size());
      return 0;
  }
```

Branch: `master` (2.2.x)

```
  $ gcc -O3 -Wall -Wl,-R/usr/local/lib bleh.c -L/usr/local/lib -o bleh  -levent
  $ ldd bleh
      linux-vdso.so.1 =>  (0x00007ffc3df50000)
      libevent.so.2.2.0 => /usr/local/lib/libevent.so.2.2.0 (0x00007f91fd781000)
      libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f91fd3a1000)
      libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f91fd182000)
      /lib64/ld-linux-x86-64.so.2 (0x00007f91fdbcc000)
  $ ./bleh
  128
```

Release: `2.1.6`

```
  $ gcc -O3 bleh.c -o bleh  -levent
  $ ldd bleh
      linux-vdso.so.1 =>  (0x00007ffd43773000)
      libevent-2.1.so.6 => /usr/lib/x86_64-linux-gnu/libevent-2.1.so.6 (0x00007feb3add6000)
      libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007feb3a9f6000)
      libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007feb3a7d7000)
      /lib64/ld-linux-x86-64.so.2 (0x00007feb3b22a000)
  $ ./bleh
  128
```

Branch: `this one`

```
  $ gcc -O3 -Wl,-R./lib bleh.c -o bleh -L./lib -levent
  $ ldd bleh
      linux-vdso.so.1 =>  (0x00007ffff55f7000)
      libevent.so.2.2.0 => ./lib/libevent.so.2.2.0 (0x00007ff8e5c82000)
      libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff8e58a2000)
      libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff8e5683000)
      /lib64/ld-linux-x86-64.so.2 (0x00007ff8e60cd000)
  $ ./bleh
  120
```
2018-04-30 18:13:45 -04:00
dpayne
791e3de0c3 Generating evdns_base_config_windows_nameservers docs on all platforms 2018-04-03 15:43:22 -07:00
dpayne
2c1562949c Fixing doxygen docs for evdns_base_search_clear when generated on non-windows machines 2018-04-03 15:17:51 -07:00
Dmitry Alimov
f24b28e4af
Fix typos in comments 2018-01-15 17:30:08 +03:00
John Fremlin
727bcea130 http: add callback to allow server to decline (and thereby close) incoming connections.
This is important, as otherwise clients can easily exhaust the file
descriptors available on a libevent HTTP server, which can cause
problems in other code which does not handle EMFILE well: for example,
see https://github.com/bitcoin/bitcoin/issues/11368

Closes: #578 (patch cherry picked)
2017-12-18 03:00:04 +03:00
ejurgensen
b49c70cc2e Fix incorrect ref to evhttp_get_decoded_uri in http.h
Replaces reference in the http.h include header file to evhttp_get_decoded_uri
with evhttp_uridecode. There is no function called evhttp_get_decoded_uri.
2017-11-05 12:18:49 +01:00
Carlo Marcelo Arenas Belón
5698cff73d always define EV_INT16_MIN
somehow missing from 043ae7481f4a73b0f48055a0260afa454f02d136
2017-08-17 12:16:56 -07:00
Azat Khuzhin
e83443ec67 Merge remote-tracking branch 'official/pr/527' -- documentation fixes
* official/pr/527:
  Fix a few trivial documentation typos
  Clarify event_free() documentation regarding pending/active events
2017-07-16 21:40:18 +03:00
Nikolay Edigaryev
c3a61a13d7 Fix a few trivial documentation typos 2017-07-07 01:24:26 +05:00
Nikolay Edigaryev
2137886d2f Clarify event_free() documentation regarding pending/active events
Currently it's not clear as to whether "first make it non-pending and
non-active" sentence requires user to take some action (e.g. call event_del(),
which event_free() already does internally) or just describes what this
function does from the developer point of view.
2017-07-07 01:22:43 +05:00
Nikolay Edigaryev
808524252f Document some obvious cases where a function might also return NULL
Closes: #525
2017-07-02 13:16:59 +03:00
Azat Khuzhin
cd285e425a Fix event_debug_logging_mask_ exporting on win32 2017-05-29 22:26:16 +03:00
Azat Khuzhin
ce3af533f1 Fix visibility issues under (mostly on win32)
Refs: #511
Fixes: 7182c2f561570cd9ceb704623ebe9ae3608c7b43 ("cmake: build SHARED and
STATIC libraries (like autoconf does)")
2017-05-29 22:01:33 +03:00
Azat Khuzhin
266f43af77 Fix arc4random_addrandom() detecting and fallback (regression)
But this is kind of hot-fix, we definitelly need more sane arc4random
compat layer.

Fixes: #488
Introduced-in: 6541168 ("Detect arch4random_addrandom() existence")
2017-03-27 17:04:33 +03:00
Azat Khuzhin
92cf234b0c log/win32: fix exporting extern variable
==> win:   C:\vagrant\log.c(73): error C2370: 'event_debug_logging_mask_' : redefinition; different storage class [C:\vagrant\.cmake-vagrant\event_core_shared.vcxproj]
2017-03-14 12:55:40 +03:00