[systemd-devel] [RFC] Initial libsystemd-asyncns commit
Marcel Holtmann
marcel at holtmann.org
Tue Dec 10 17:36:12 PST 2013
Hi Daniel,
> Reindentation is done to fit systemd
> ---
> Makefile.am | 23 +
> src/libsystemd-asyncns/asyncns.c | 1513 +++++++++++++++++++++++++++++++++
> src/libsystemd-asyncns/asyncns.h | 163 ++++
> src/libsystemd-asyncns/test-asyncns.c | 178 ++++
> 4 files changed, 1877 insertions(+)
> create mode 100644 src/libsystemd-asyncns/asyncns.c
> create mode 100644 src/libsystemd-asyncns/asyncns.h
> create mode 100644 src/libsystemd-asyncns/test-asyncns.c
>
> diff --git a/Makefile.am b/Makefile.am
> index 19da6ea..a0564b5 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -659,6 +659,29 @@ tests += test-rtnl
>
> # ------------------------------------------------------------------------------
> noinst_LTLIBRARIES += \
> + libsystemd-asyncns.la
> +
> +libsystemd_asyncns_la_SOURCES = \
> + src/libsystemd-asyncns/asyncns.c \
> + src/libsystemd-asyncns/asyncns.h
> +
> +libsystemd_asyncns_la_CFLAGS = \
> + -pthread
> +
> +test_asyncns_SOURCES = \
> + src/libsystemd-asyncns/test-asyncns.c
> +
> +test_asyncns_LDADD = \
> + libsystemd-asyncns.la
> +
> +test_asyncns_LDFLAGS = \
> + -lresolv \
> + -pthread
> +
> +tests += test-asyncns
> +
> +# ------------------------------------------------------------------------------
> +noinst_LTLIBRARIES += \
> libsystemd-shared.la
>
> libsystemd_shared_la_SOURCES = \
> diff --git a/src/libsystemd-asyncns/asyncns.c b/src/libsystemd-asyncns/asyncns.c
> new file mode 100644
> index 0000000..f1c2602
> --- /dev/null
> +++ b/src/libsystemd-asyncns/asyncns.c
> @@ -0,0 +1,1513 @@
> +/***
> + This file is part of libasyncns.
> +
> + Copyright 2005-2008 Lennart Poettering
> +
> + libasyncns is free software; you can redistribute it and/or modify
> + it under the terms of the GNU Lesser General Public License as
> + published by the Free Software Foundation, either version 2.1 of the
> + License, or (at your option) any later version.
> +
> + libasyncns is distributed in the hope that it will be useful, but
> + WITHOUT ANY WARRANTY; without even the implied warranty of
> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + Lesser General Public License for more details.
> +
> + You should have received a copy of the GNU Lesser General Public
> + License along with libasyncns. If not, see
> + <http://www.gnu.org/licenses/>.
> + ***/
> +
> +#ifdef HAVE_CONFIG_H
> +#include <config.h>
> +#endif
> +
> +/* #undef HAVE_PTHREAD */
> +
> +#include <assert.h>
> +#include <fcntl.h>
> +#include <signal.h>
> +#include <unistd.h>
> +#include <sys/select.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <stdlib.h>
> +#include <errno.h>
> +#include <sys/wait.h>
> +#include <sys/types.h>
> +#include <pwd.h>
> +#include <netinet/in.h>
> +#include <arpa/nameser.h>
> +#include <resolv.h>
> +#include <dirent.h>
> +#include <sys/time.h>
> +#include <sys/resource.h>
> +#include <stdint.h>
> +
> +#ifdef HAVE_SYS_PRCTL_H
> +#include <sys/prctl.h>
> +#endif
> +
> +#if HAVE_PTHREAD
> +#include <pthread.h>
> +#endif
> +
> +#include "asyncns.h"
> +
> +#ifndef MSG_NOSIGNAL
> +#define MSG_NOSIGNAL 0
> +#endif
> +
> +#define MAX_WORKERS 16
> +#define MAX_QUERIES 256
> +#define BUFSIZE (10240)
> +
> +typedef enum {
> + REQUEST_ADDRINFO,
> + RESPONSE_ADDRINFO,
> + REQUEST_NAMEINFO,
> + RESPONSE_NAMEINFO,
> + REQUEST_RES_QUERY,
> + REQUEST_RES_SEARCH,
> + RESPONSE_RES,
> + REQUEST_TERMINATE,
> + RESPONSE_DIED
> +} query_type_t;
> +
> +enum {
> + REQUEST_RECV_FD = 0,
> + REQUEST_SEND_FD = 1,
> + RESPONSE_RECV_FD = 2,
> + RESPONSE_SEND_FD = 3,
> + MESSAGE_FD_MAX = 4
> +};
> +
> +struct asyncns {
> + int fds[MESSAGE_FD_MAX];
> +
> +#ifndef HAVE_PTHREAD
> + pid_t workers[MAX_WORKERS];
> +#else
> + pthread_t workers[MAX_WORKERS];
> +#endif
> + unsigned valid_workers;
why do we have to spawn threads or do forks for DNS. This looks all pretty expensive. In ConnMan for example we just wrote our own async DNS using a mainloop. Works perfectly fine and is dirt cheap.
Regards
Marcel
More information about the systemd-devel
mailing list