From patchwork Fri Jan 15 22:29:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?RWR3aW4gVMO2csO2aw==?= X-Patchwork-Id: 12024341 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-24.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA7CFC433E0 for ; Fri, 15 Jan 2021 22:59:11 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 839C723128 for ; Fri, 15 Jan 2021 22:59:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 839C723128 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=citrix.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from list by lists.xenproject.org with outflank-mailman.68775.123244 (Exim 4.92) (envelope-from ) id 1l0Y3U-0003CV-OG; Fri, 15 Jan 2021 22:58:56 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 68775.123244; Fri, 15 Jan 2021 22:58:56 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l0Y3U-0003CB-FO; Fri, 15 Jan 2021 22:58:56 +0000 Received: by outflank-mailman (input) for mailman id 68775; Fri, 15 Jan 2021 22:58:55 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l0XyM-0001Wj-1N for xen-devel@lists.xenproject.org; Fri, 15 Jan 2021 22:53:38 +0000 Received: from esa4.hc3370-68.iphmx.com (unknown [216.71.155.144]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 2b941d5d-5a37-4e75-91b0-a24787a7b669; Fri, 15 Jan 2021 22:52:40 +0000 (UTC) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 2b941d5d-5a37-4e75-91b0-a24787a7b669 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1610751160; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=z+ioT6wmOyjk9o8JoO7XIs7aXHObUF73mKxWy5PYTjA=; b=IRYlWZzS61/ifoPWwExZMUb/QPquUH/r6aVL2NO4E3iF+nJLjTqRLHw0 0gxjRJEqTDhK80ftCyVWWXI712Xc6/7qnXlOdBIKV/AmDL/SU5k06YTgZ p7WJqyz5gvQMPPosAJdOTeMvkr9HHGGLs6QOBLSK6no16/ZV2qluh46Yf 4=; Authentication-Results: esa4.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: 3Zax/+P+JvMivlVNzkYK/wwIwFeJfn+UrK7Yc5WIMfMQCPW2nNaCUYMFd7wCopdRFBMU5yjBat 8MwtxgNHVpUygeKQ5doC9IV9o+F45WMAkY7vY/sriRV8A8zD1EVUTYLs2VgcqMGU+gFeZYRffM I1kG/uoHMhql03SqBqsYcaPpjS6Bdt0QqsZ/0joLqSx6GBU1NISXgQo3kNfwtBkEEjyN2Wiy8f wqKAlYsjljMF14zJXXum0gvre7XH5UwfnUryf3clb63AuAdEOsU0MRdm3/vwYkAguWopiGakQs u2o= X-SBRS: 5.1 X-MesageID: 36512262 X-Ironport-Server: esa4.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.79,350,1602561600"; d="scan'208";a="36512262" From: =?utf-8?b?RWR3aW4gVMO2csO2aw==?= To: CC: =?utf-8?b?RWR3aW4gVMO2csO2aw==?= , "Christian Lindig" , David Scott , "Ian Jackson" , Wei Liu Subject: [PATCH v1 1/5] tools/ocaml: add unit test skeleton with Dune build system Date: Fri, 15 Jan 2021 22:29:00 +0000 Message-ID: <58c08503e1f2c26aaba7297bf65caec25929564c.1610748224.git.edvin.torok@citrix.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 Based on initial work by Christian Lindig Doing oxenstored development, especially fuzzing/unit tests requires an incremental and fast build system. Dune is the preferred upstream build system for OCaml, and has been in use by the XAPI project for years. Is is incremental and also generates editor integration files (.merlin). Usage: ./xs-reconfigure.sh cd tools/ocaml make clean make check There are some other convenience targets as well: make dune-clean make dune-syntax-check make dune-build-oxenstored There are some files that are generated by Make, these are created by a 'dune-pre' target, they are too closely tied to make and cannot yet be generated by Dune itself. The various Makefile targets are used as entrypoints into Dune that set the needed env vars (for C include files and libraries) and ensure that the generated files are available. The unit tests do not require Xen to be available, so add mock eventchn and xenctrl libraries for the unit test to use, and copy the non-system specific modules from xenstored/ to xenstored/test/. Xenstored had to be split into Xenstored and Xenstored_main, so that we can use the functions defined in Xenstored without actually starting up the daemon in a unit test. Similarly argument parsing had to be delayed until after daemon startup. Also had to disable setrlimit when running as non-root in poll.ml. Signed-off-by: Edwin Török --- tools/ocaml/.gitignore | 2 + tools/ocaml/Makefile | 33 +++++++++++++ tools/ocaml/dune-project | 5 ++ tools/ocaml/libs/eventchn/dune | 8 ++++ tools/ocaml/libs/mmap/dune | 8 ++++ tools/ocaml/libs/xb/dune | 7 +++ tools/ocaml/libs/xc/dune | 9 ++++ tools/ocaml/libs/xs/dune | 4 ++ tools/ocaml/xen.opam | 0 tools/ocaml/xenstore.opam | 0 tools/ocaml/xenstored.opam | 18 +++++++ tools/ocaml/xenstored/Makefile | 3 +- tools/ocaml/xenstored/dune | 19 ++++++++ tools/ocaml/xenstored/parse_arg.ml | 2 +- tools/ocaml/xenstored/poll.ml | 3 +- tools/ocaml/xenstored/test/dune | 11 +++++ tools/ocaml/xenstored/test/xenctrl.ml | 48 +++++++++++++++++++ tools/ocaml/xenstored/test/xeneventchn.ml | 50 ++++++++++++++++++++ tools/ocaml/xenstored/test/xenstored_test.ml | 2 + tools/ocaml/xenstored/xenstored.ml | 4 +- 20 files changed, 231 insertions(+), 5 deletions(-) create mode 100644 tools/ocaml/.gitignore create mode 100644 tools/ocaml/dune-project create mode 100644 tools/ocaml/libs/eventchn/dune create mode 100644 tools/ocaml/libs/mmap/dune create mode 100644 tools/ocaml/libs/xb/dune create mode 100644 tools/ocaml/libs/xc/dune create mode 100644 tools/ocaml/libs/xs/dune create mode 100644 tools/ocaml/xen.opam create mode 100644 tools/ocaml/xenstore.opam create mode 100644 tools/ocaml/xenstored.opam create mode 100644 tools/ocaml/xenstored/dune create mode 100644 tools/ocaml/xenstored/test/dune create mode 100644 tools/ocaml/xenstored/test/xenctrl.ml create mode 100644 tools/ocaml/xenstored/test/xeneventchn.ml create mode 100644 tools/ocaml/xenstored/test/xenstored_test.ml diff --git a/tools/ocaml/.gitignore b/tools/ocaml/.gitignore new file mode 100644 index 0000000000..655e32b07c --- /dev/null +++ b/tools/ocaml/.gitignore @@ -0,0 +1,2 @@ +_build +.merlin diff --git a/tools/ocaml/Makefile b/tools/ocaml/Makefile index a7c04b6546..53dd0a0f0d 100644 --- a/tools/ocaml/Makefile +++ b/tools/ocaml/Makefile @@ -34,3 +34,36 @@ build-tools-oxenstored: $(MAKE) -s -C libs/xb $(MAKE) -s -C libs/xc $(MAKE) -C xenstored + +LIBRARY_PATH=$(XEN_libxenctrl):$(XEN_libxenguest):$(XEN_libxentoollog):$(XEN_libxencall):$(XEN_libxenevtchn):$(XEN_libxenforeignmemory):$(XEN_libxengnttab):$(XEN_libxendevicemodel):$(XEN_libxentoolcore) +C_INCLUDE_PATH=$(XEN_libxenctrl)/include:$(XEN_libxengnttab)/include:$(XEN_libxenevtchn)/include:$(XEN_libxentoollog)/include:$(XEN_INCLUDE) + +# Files generated by the Makefile +# These cannot be generated from dune, because dune cannot refer to files +# in the parent directory (so it couldn't copy/use Config.mk) +.PHONY: dune-pre +dune-pre: + $(MAKE) -s -C ../../ build-tools-public-headers + $(MAKE) -s -C libs/xs paths.ml + $(MAKE) -s -C libs/xc xenctrl_abi_check.h + $(MAKE) -s -C xenstored paths.ml _paths.h + +.PHONY: check +check: dune-pre + # --force isn't necessary here if the test is deterministic + OCAMLRUNPARAM=b C_INCLUDE_PATH=$(C_INCLUDE_PATH) dune runtest --profile=release --no-buffer --force + +# Convenience targets for development + +.PHONY: dune-clean +dune-clean: + $(MAKE) clean + dune clean + +.PHONY: dune-syntax-check +dune-syntax-check: dune-pre + LIBRARY_PATH=$(LIBRARY_PATH) C_INCLUDE_PATH=$(C_INCLUDE_PATH) dune build --profile=release @check + +.PHONY: build-oxenstored-dune +dune-build-oxenstored: dune-pre + LD_LIBRARY_PATH=$(LIBRARY_PATH) LIBRARY_PATH=$(LIBRARY_PATH) C_INCLUDE_PATH=$(C_INCLUDE_PATH) dune build --profile=release @all diff --git a/tools/ocaml/dune-project b/tools/ocaml/dune-project new file mode 100644 index 0000000000..b41cfae68b --- /dev/null +++ b/tools/ocaml/dune-project @@ -0,0 +1,5 @@ +(lang dune 2.0) + +(name xen) + +(formatting disabled) diff --git a/tools/ocaml/libs/eventchn/dune b/tools/ocaml/libs/eventchn/dune new file mode 100644 index 0000000000..e08bc76fdf --- /dev/null +++ b/tools/ocaml/libs/eventchn/dune @@ -0,0 +1,8 @@ +(library + (foreign_stubs + (language c) + (names xeneventchn_stubs)) + (name xeneventchn) + (public_name xen.eventchn) + (libraries unix) + (c_library_flags -lxenevtchn)) diff --git a/tools/ocaml/libs/mmap/dune b/tools/ocaml/libs/mmap/dune new file mode 100644 index 0000000000..a47de44e47 --- /dev/null +++ b/tools/ocaml/libs/mmap/dune @@ -0,0 +1,8 @@ +(library + (foreign_stubs + (language c) + (names xenmmap_stubs)) + (name xenmmap) + (public_name xen.mmap) + (libraries unix) + (install_c_headers mmap_stubs)) diff --git a/tools/ocaml/libs/xb/dune b/tools/ocaml/libs/xb/dune new file mode 100644 index 0000000000..feb30adc01 --- /dev/null +++ b/tools/ocaml/libs/xb/dune @@ -0,0 +1,7 @@ +(library + (foreign_stubs + (language c) + (names xenbus_stubs xs_ring_stubs)) + (name xenbus) + (public_name xen.bus) + (libraries unix xenmmap)) diff --git a/tools/ocaml/libs/xc/dune b/tools/ocaml/libs/xc/dune new file mode 100644 index 0000000000..fb75ee8ff7 --- /dev/null +++ b/tools/ocaml/libs/xc/dune @@ -0,0 +1,9 @@ +(library + (foreign_stubs + (language c) + (names xenctrl_stubs)) + (name xenctrl) + (public_name xen.ctrl) + (libraries unix xenmmap) + (c_library_flags -lxenctrl -lxenguest -lxencall -lxenforeignmemory + -lxengnttab)) diff --git a/tools/ocaml/libs/xs/dune b/tools/ocaml/libs/xs/dune new file mode 100644 index 0000000000..c79ea75775 --- /dev/null +++ b/tools/ocaml/libs/xs/dune @@ -0,0 +1,4 @@ +(library + (name xenstore) + (public_name xen.store) + (libraries unix xenbus)) diff --git a/tools/ocaml/xen.opam b/tools/ocaml/xen.opam new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tools/ocaml/xenstore.opam b/tools/ocaml/xenstore.opam new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tools/ocaml/xenstored.opam b/tools/ocaml/xenstored.opam new file mode 100644 index 0000000000..31775a3033 --- /dev/null +++ b/tools/ocaml/xenstored.opam @@ -0,0 +1,18 @@ +opam-version: "2.0" +synopsis: "In-memory key-value store for the Xen hypervisor" +maintainer: "lindig@gmail.com" +authors: "lindig@gmail.com" +license: "LGPL" +homepage: "https://github.com/lindig/xen-ocaml-tools" +bug-reports: "https://github.com/lindig/xen-ocaml-tools/issues" +depends: [ + "ocaml" + "dune" {build} + "base-unix" + "crowbar" {with-test} + "qcheck-core" {with-test} + "qcstm" {with-test} +] +build: ["dune" "build" "-p" name "-j" jobs] +depexts: ["m4" "libxen-dev" "libsystemd-dev"] {os-distribution = "debian"} +dev-repo: "git+https://github.com/lindig/xen-ocaml-tools.git" diff --git a/tools/ocaml/xenstored/Makefile b/tools/ocaml/xenstored/Makefile index 89ec3ec76a..9d2da206d8 100644 --- a/tools/ocaml/xenstored/Makefile +++ b/tools/ocaml/xenstored/Makefile @@ -56,7 +56,8 @@ OBJS = paths \ history \ parse_arg \ process \ - xenstored + xenstored \ + xenstored_main INTF = symbol.cmi trie.cmi syslog.cmi systemd.cmi poll.cmi diff --git a/tools/ocaml/xenstored/dune b/tools/ocaml/xenstored/dune new file mode 100644 index 0000000000..e59eb22638 --- /dev/null +++ b/tools/ocaml/xenstored/dune @@ -0,0 +1,19 @@ +(executable + (modes byte exe) + (name xenstored) + (modules (:standard \ syslog systemd)) + (public_name xenstored) + (package xenstored) + (flags (:standard -w -52)) + (libraries unix xen.bus xen.mmap xen.ctrl xen.eventchn xenstubs)) + +(library + (foreign_stubs + (language c) + (names syslog_stubs systemd_stubs select_stubs) + (flags (-DHAVE_SYSTEMD))) + (modules syslog systemd) + (name xenstubs) + (wrapped false) + (libraries unix) + (c_library_flags -lsystemd)) diff --git a/tools/ocaml/xenstored/parse_arg.ml b/tools/ocaml/xenstored/parse_arg.ml index 7c0478e76a..965cb9ebeb 100644 --- a/tools/ocaml/xenstored/parse_arg.ml +++ b/tools/ocaml/xenstored/parse_arg.ml @@ -28,7 +28,7 @@ type config = disable_socket: bool; } -let do_argv = +let do_argv () = let pidfile = ref "" and tracefile = ref "" (* old xenstored compatibility *) and domain_init = ref true and activate_access_log = ref true diff --git a/tools/ocaml/xenstored/poll.ml b/tools/ocaml/xenstored/poll.ml index 26f8620dfc..92e0717ed2 100644 --- a/tools/ocaml/xenstored/poll.ml +++ b/tools/ocaml/xenstored/poll.ml @@ -64,4 +64,5 @@ let poll_select in_fds out_fds exc_fds timeout = a r let () = - set_fd_limit (get_sys_fs_nr_open ()) + if Unix.geteuid () = 0 then + set_fd_limit (get_sys_fs_nr_open ()) diff --git a/tools/ocaml/xenstored/test/dune b/tools/ocaml/xenstored/test/dune new file mode 100644 index 0000000000..2a3eb2b7df --- /dev/null +++ b/tools/ocaml/xenstored/test/dune @@ -0,0 +1,11 @@ +(copy_files# ../*.ml{,i}) + +(test + (modes native) + (ocamlopt_flags -afl-instrument) + (name xenstored_test) + (modules (:standard \ syslog systemd)) + (package xenstored) + (flags (:standard -w -52)) + ;;(action (run %{test} -v --seed 364172147)) + (libraries unix xen.bus xen.mmap xenstubs crowbar xen.store fmt fmt.tty)) diff --git a/tools/ocaml/xenstored/test/xenctrl.ml b/tools/ocaml/xenstored/test/xenctrl.ml new file mode 100644 index 0000000000..37d6da0a47 --- /dev/null +++ b/tools/ocaml/xenstored/test/xenctrl.ml @@ -0,0 +1,48 @@ +(* + * Copyright (C) 2006-2007 XenSource Ltd. + * Copyright (C) 2008 Citrix Ltd. + * Author Vincent Hanquez + * + * This program 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; version 2.1 only. with the special + * exception on linking described in file LICENSE. + * + * This program 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. + *) + +(** *) +type domid = int + +(* ** xenctrl.h ** *) + + +type domaininfo = +{ + domid : domid; + dying : bool; + shutdown : bool; + shutdown_code : int; +} + +exception Error of string + +type handle = unit + +let interface_open () = () +let interface_close () = () + +let domain_getinfo () domid = { + domid = domid; + dying = false; + shutdown = false; + shutdown_code = 0; +} + +let devzero = Unix.openfile "/dev/zero" [] 0 +let nullmap () = Xenmmap.mmap devzero Xenmmap.RDWR Xenmmap.PRIVATE 4096 0 + +let map_foreign_range _ _ _ _ = nullmap () diff --git a/tools/ocaml/xenstored/test/xeneventchn.ml b/tools/ocaml/xenstored/test/xeneventchn.ml new file mode 100644 index 0000000000..6612722dc2 --- /dev/null +++ b/tools/ocaml/xenstored/test/xeneventchn.ml @@ -0,0 +1,50 @@ +(* + * Copyright (C) 2006-2007 XenSource Ltd. + * Copyright (C) 2008 Citrix Ltd. + * Author Vincent Hanquez + * + * This program 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; version 2.1 only. with the special + * exception on linking described in file LICENSE. + * + * This program 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. + *) + +type handle = Unix.file_descr * int ref + +let devnull = Unix.openfile "/dev/null" [] 0 +let init () = devnull, ref 0 +let fd (h, _) = h + +type t = int + +type virq_t = + | Timer (* #define VIRQ_TIMER 0 *) + | Debug (* #define VIRQ_DEBUG 1 *) + | Console (* #define VIRQ_CONSOLE 2 *) + | Dom_exc (* #define VIRQ_DOM_EXC 3 *) + | Tbuf (* #define VIRQ_TBUF 4 *) + | Reserved_5 (* Do not use this value as it's not defined *) + | Debugger (* #define VIRQ_DEBUGGER 6 *) + | Xenoprof (* #define VIRQ_XENOPROF 7 *) + | Con_ring (* #define VIRQ_CON_RING 8 *) + | Pcpu_state (* #define VIRQ_PCPU_STATE 9 *) + | Mem_event (* #define VIRQ_MEM_EVENT 10 *) + | Xc_reserved (* #define VIRQ_XC_RESERVED 11 *) + | Enomem (* #define VIRQ_ENOMEM 12 *) + | Xenpmu (* #define VIRQ_XENPMU 13 *) + +let notify _h _ = () +let bind_interdomain (_h, port) domid remote_port = incr port; !port +let bind_virq (_h, port) _ = incr port; !port +let bind_dom_exc_virq handle = bind_virq handle Dom_exc +let unbind _ _ = () +let pending (_h, port) = !port +let unmask _ _ = () + +let to_int x = x +let of_int x = x diff --git a/tools/ocaml/xenstored/test/xenstored_test.ml b/tools/ocaml/xenstored/test/xenstored_test.ml new file mode 100644 index 0000000000..e86b68e867 --- /dev/null +++ b/tools/ocaml/xenstored/test/xenstored_test.ml @@ -0,0 +1,2 @@ +open Xenstored +let () = () diff --git a/tools/ocaml/xenstored/xenstored.ml b/tools/ocaml/xenstored/xenstored.ml index 885b397d71..e25b407303 100644 --- a/tools/ocaml/xenstored/xenstored.ml +++ b/tools/ocaml/xenstored/xenstored.ml @@ -265,8 +265,8 @@ let to_file store cons fds file = (fun () -> close_out channel) end -let _ = - let cf = do_argv in +let main () = + let cf = do_argv () in let pidfile = if Sys.file_exists (config_filename cf) then parse_config (config_filename cf)