From 7ce9cabbd1421d59ad075f172c947797f11404ba Mon Sep 17 00:00:00 2001 From: Open XT Date: Thu, 12 Jun 2014 19:34:20 +0100 Subject: [PATCH] Initial commit --- COPYING | 280 +++++++++++++++++ Makefile.am | 34 +++ configure.ac | 246 +++++++++++++++ src/Makefile.am | 52 ++++ src/arguments.c | 62 ++++ src/arguments.h | 40 +++ src/dmbus.c | 347 +++++++++++++++++++++ src/qemu-wrapper.c | 732 +++++++++++++++++++++++++++++++++++++++++++++ src/qemu-wrapper.h | 105 +++++++ src/util.c | 115 +++++++ src/util.h | 45 +++ 11 files changed, 2058 insertions(+) create mode 100644 COPYING create mode 100644 Makefile.am create mode 100644 configure.ac create mode 100644 src/Makefile.am create mode 100644 src/arguments.c create mode 100644 src/arguments.h create mode 100644 src/dmbus.c create mode 100644 src/qemu-wrapper.c create mode 100644 src/qemu-wrapper.h create mode 100644 src/util.c create mode 100644 src/util.h diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..d8cf7d4 --- /dev/null +++ b/COPYING @@ -0,0 +1,280 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..3d52bfc --- /dev/null +++ b/Makefile.am @@ -0,0 +1,34 @@ +# +# +# Makefile.am: +# +# Copyright (c) 2010 Julian Pidancet , +# All rights reserved. +# +# $Id:$ +# +# $Log:$ +# +# +# + +# +# Copyright (c) 2012 Citrix Systems, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + + +SUBDIRS = src diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..904ef3c --- /dev/null +++ b/configure.ac @@ -0,0 +1,246 @@ +# +# Copyright (c) 2012 Citrix Systems, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +# Prelude. +AC_PREREQ(2.13) +AC_INIT() + +LT_INIT + +PACKAGE=qemu-wrapper + +AM_INIT_AUTOMAKE($PACKAGE,$VERSION) + +# Checks for programs. +AC_PROG_CC +AM_PROG_CC_C_O +PKG_PROG_PKG_CONFIG([0.22]) +AC_PROG_AWK +AC_CHECK_PROG(MD5SUM, md5sum, md5sum) +AC_CHECK_PROG(GREP, grep, grep) +AC_CHECK_PROG(SED, sed, sed) + +# Checks for header files. +AC_CHECK_HEADERS([unistd.h fcntl.h errno.h stdlib.h stdint.h stropts.h syslog.h string.h stdio.h stdarg.h]) +AC_CHECK_HEADERS([sys/types.h sys/stat.h sys/mman.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_C_INLINE +AC_C_CONST +AC_HEADER_TIME +AC_STRUCT_TM + +CPPFLAGS="-D_GNU_SOURCE ${CPPFLAGS}" + +# Add option to compile for stubdomain +AC_ARG_ENABLE([syslog], + AC_HELP_STRING([--disable-syslog], [Write all log on standard output])) + +case "x$enable_syslog" in + x|xtrue) + CPPFLAGS="$CPPFLAGS -DSYSLOG" + ;; + *) + ;; +esac + +# Checks for libraries. + +# Check for libevent + +AC_ARG_WITH([libevent], + AC_HELP_STRING([--with-libevent=PATH], [Path to prefix where libevent (or libev) is installed]), + [LIBEVENT_PREFIX=$with_libevent], []) + +case "x$LIBEVENT_PREFIX" in + x|xno|xyes) + LIBEVENT_INC="" + LIBEVENT_LIB="-levent" + ;; + *) + LIBEVENT_INC="-I${LIBEVENT_PREFIX}/include" + LIBEVENT_LIB="-L${LIBEVENT_PREFIX}/lib -levent" + ;; +esac + +AC_SUBST(LIBEVENT_INC) +AC_SUBST(LIBEVENT_LIB) + +have_libevent=true + +ORIG_LIBS="${LIBS}" +ORIG_CPPFLAGS="${CPPFLAGS}" + LIBS="${LIBS} ${LIBEVENT_LIB}" + CPPFLAGS="${CPPFLAGS} ${LIBEVENT_INC}" + AC_CHECK_HEADERS([event.h], [], [have_libevent=false]) + AC_CHECK_FUNC([event_init], [], [have_libevent=false]) +LIBS="${ORIG_LIBS}" +CPPFLAGS="${ORIG_CPPFLAGS}" + +if test "x$have_libevent" = "xfalse"; then + AC_MSG_ERROR([ +*** At least libev or libevent is required. +]) +fi + +# libxenstore + +AC_ARG_WITH([libxenstore], + AC_HELP_STRING([--with-libxenstore=PATH], [Path to prefix where libxenstore was installed.]), + [LIBXENSTORE_PREFIX=$with_libxenstore], []) + +case "x$LIBXENSTORE_PREFIX" in + x|xno|xyes) + LIBXENSTORE_INC="" + LIBXENSTORE_LIB="-lxenstore" + ;; + *) + LIBXENSTORE_INC="-I${LIBXENSTORE_PREFIX}/include" + LIBXENSTORE_LIB="-L${LIBXENSTORE_PREFIX}/lib -lxenstore" + ;; +esac + +AC_SUBST(LIBXENSTORE_INC) +AC_SUBST(LIBXENSTORE_LIB) + +have_libxenstore=true + +ORIG_LIBS="${LIBS}" +ORIG_CPPFLAGS="${CPPFLAGS}" + LIBS="${LIBS} ${LIBXENSTORE_LIB}" + CPPFLAGS="${CPPFLAGS} ${LIBXENSTORE_INC}" + AC_CHECK_HEADERS([xs.h], [], [have_libxenstore=false]) + AC_CHECK_FUNC(xs_domain_open, [], [have_libxenstore=false]) +LIBS="${ORIG_LIBS}" +CPPFLAGS="${ORIG_CPPFLAGS}" + +if test "x$have_libxenstore" = "xfalse"; then + AC_MSG_ERROR([where is libxenstore dude]) +fi + +# libv4v + +AC_ARG_WITH(libv4v, + AC_HELP_STRING([--with-libv4v=PATH], [Path to prefix where libv4v was installed.]), + [LIBV4V_PREFIX=$with_libv4v], []) + +case "x$LIBV4V_PREFIX" in + x|xno|xyes) + LIBV4V_INC="" + LIBV4V_LIB="-lv4v_nointerposer" + ;; + *) + LIBV4V_INC="-I${LIBV4V_PREFIX}/include" + LIBV4V_LIB="-L${LIBV4V_PREFIX}/lib -lv4v_nointerposer" + ;; +esac + +AC_SUBST(LIBV4V_INC) +AC_SUBST(LIBV4V_LIB) + +have_libv4=true +ORIG_LIBS="${LIBS}" +ORIG_CPPFLAGS="${CPPFLAGS}" + LIBS="${LIBS} ${LIBV4V_LIB}" + CPPFLAGS="${CPPFLAGS} ${LIBV4V_INC}" + AC_CHECK_HEADERS([libv4v.h], [], [have_libv4v=false]) + AC_CHECK_FUNC(v4v_socket, [], [have_libv4v=false]) +LIBS="${ORIG_LIBS}" +CPPFLAGS="${ORIG_CPPFLAGS}" + +if test "x$have_libv4v" = "xfalse"; then + AC_MSG_ERROR([where is libv4v dude]) +fi + +# libdmbus + +AC_ARG_WITH([libdmbus], + AC_HELP_STRING([--with-libdmbus=PATH], [Path to prefix where libdmbus was installed.]), + [LIBDMBUS_PREFIX=$with_libdmbus], []) + +case "x$LIBDMBUS_PREFIX" in + x|xno|xyes) + LIBDMBUS_INC="" + LIBDMBUS_LIB="-ldmbus" + ;; + *) + LIBDMBUS_INC="-I${LIBDMBUS_PREFIX}/include" + LIBDMBUS_LIB="-L${LIBDMBUS_PREFIX}/lib -ldmbus" + ;; +esac + +AC_SUBST(LIBDMBUS_INC) +AC_SUBST(LIBDMBUS_LIB) + +have_libdmbus=true + +ORIG_LIBS="${LIBS}" +ORIG_CPPFLAGS="${CPPFLAGS}" + LIBS="${LIBS} ${LIBDMBUS_LIB} ${LIBV4V_LIB}" + CPPFLAGS="${CPPFLAGS} ${LIBV4V_INC} ${LIBDMBUS_INC}" + AC_CHECK_HEADERS([libdmbus.h], [], [have_libdmbus=false]) + AC_CHECK_FUNC(dmbus_init, [], [have_libdmbus=false]) +LIBS="${ORIG_LIBS}" +CPPFLAGS="${ORIG_CPPFLAGS}" + +if test "x$have_libdmbus" = "xfalse"; then + AC_MSG_ERROR([where is libdmbus dude]) +fi + +# libpthread + +AC_ARG_WITH([libpthread], + AC_HELP_STRING([--with-libpthread=PATH], [Path to prefix where libpthread was installed.]), + [LIBPTHREAD_PREFIX=$with_libpthread], []) + +case "x$LIBPTHREAD_PREFIX" in + x|xno|xyes) + LIBPTHREAD_INC="" + LIBPTHREAD_LIB="-lpthread" + ;; + *) + LIBPTHREAD_INC="-I${LIBPTHREAD_PREFIX}/include" + LIBPTHREAD_LIB="-L${LIBPTHREAD_PREFIX}/lib -lpthread" + ;; +esac + +AC_SUBST(LIBPTHREAD_INC) +AC_SUBST(LIBPTHREAD_LIB) + +have_libpthread=true + +ORIG_LIBS="${LIBS}" +ORIG_CPPFLAGS="${CPPFLAGS}" + LIBS="${LIBS} ${LIBPTHREAD_LIB}" + CPPFLAGS="${CPPFLAGS} ${LIBPTHREAD_INC}" + AC_CHECK_HEADERS([pthread.h], [], [have_libpthread=false]) + AC_CHECK_FUNC(pthread_create, [], [have_libpthread=false]) +LIBS="${ORIG_LIBS}" +CPPFLAGS="${ORIG_CPPFLAGS}" + +if test "x$have_libpthread" = "xfalse"; then + AC_MSG_ERROR([where is libpthread dude]) +fi + +# Output files. +AC_CONFIG_MACRO_DIR([m4]) +AM_CONFIG_HEADER(src/config.h) + +AC_CONFIG_FILES([Makefile + src/Makefile]) +AC_OUTPUT diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..53e5434 --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,52 @@ +# +# +# Makefile.am: +# +# Copyright (c) 2010 Julian Pidancet , +# All rights reserved. +# +# $Id:$ +# +# $Log:$ +# +# +# + +# +# Copyright (c) 2012 Citrix Systems, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + + +INCLUDES = ${LIBXENSTORE_INC} ${LIBDMBUS_INC} ${LIBV4V_INC} ${LIBPTHREAD_INC} \ + ${LIBEVENT_INC} + +SRCS = \ + arguments.c \ + dmbus.c \ + qemu-wrapper.c \ + util.c + +noinst_HEADERS = \ + arguments.h \ + qemu-wrapper.h \ + util.h + +bin_PROGRAMS = qemu-dm-wrapper +qemu_dm_wrapper_SOURCES = ${SRCS} +qemu_dm_wrapper_CFLAGS = -W -Werror -Wno-unused -g -Wall +qemu_dm_wrapper_LDADD = ${LIBXENSTORE_LIB} ${LIBDMBUS_LIB} ${LIBV4V_LIB} \ + ${LIBEVENT_LIB} ${LIBPTHREAD_LIB} diff --git a/src/arguments.c b/src/arguments.c new file mode 100644 index 0000000..2edfd27 --- /dev/null +++ b/src/arguments.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2012 Citrix Systems, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include "arguments.h" +#include "util.h" + +#define ARGS_BATCH 10 + +void arguments_init (s_arguments *args) +{ + args->pos = 1; + args->args = xmalloc (ARGS_BATCH * sizeof (*args->args)); + args->size = ARGS_BATCH; + args->args[0] = NULL; +} + +char * const * arguments_get (s_arguments *args) +{ + return args->args; +} + +void arguments_add (s_arguments *args, const char *arg) +{ + if (args->pos == args->size) + { + args->size += ARGS_BATCH; + args->args = xrealloc (args->args, args->size * sizeof (*args->args)); + } + + args->args[args->pos - 1] = xstrdup (arg); + args->args[args->pos++] = NULL; +} + +char * const *find_arg (char * const *argv, const char *arg) +{ + int i = 0; + + for (i = 0; argv[i]; i++) + { + if (!strcmp (arg, argv[i])) + return &argv[i]; + } + + return NULL; +} diff --git a/src/arguments.h b/src/arguments.h new file mode 100644 index 0000000..7bf199a --- /dev/null +++ b/src/arguments.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2012 Citrix Systems, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef ARGUMENTS_H_ +# define ARGUMENTS_H_ + +typedef struct +{ + size_t size; /* Array size */ + size_t pos; + char **args; +} s_arguments; + +void arguments_init (s_arguments *args); +void arguments_add (s_arguments *args, const char *arg); +char * const * arguments_get (s_arguments *args); + + +/** + * This function returns NULL if arg is not found otherwise + * a pointer to the first entry of argv which is equal to arg + */ +char * const *find_arg (char * const *argv, const char *arg); + +#endif /* !ARGUMENTS_H_ */ diff --git a/src/dmbus.c b/src/dmbus.c new file mode 100644 index 0000000..13e0fce --- /dev/null +++ b/src/dmbus.c @@ -0,0 +1,347 @@ +/* + * Copyright (c) 2012 Citrix Systems, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "qemu-wrapper.h" +#include "util.h" + +/* Time to wait between 2 reconnection in ms */ +#define DMBUS_TIMEOUT 1000 + +struct service +{ + int fd; + v4v_addr_t peer; + struct dmbus_conn_prologue prologue; + + char buff[DMBUS_MAX_MSG_LEN]; + uint32_t len; + struct event ev; + struct event ev_reconnect; + s_spawn_dm *dm; + int first_connection; +}; + +static void handle_message (struct service *s, union dmbus_msg *m) +{ + e_dmbus_state state; + + (void) s; + + switch (m->hdr.msg_type) { + case DMBUS_MSG_DEVICE_MODEL_READY: + dm_ready_dmbus (s->dm, !s->first_connection); + state = s->first_connection ? DMBUS_CONNECT : DMBUS_RECONNECT; + if (s->dm->dmbus_state) + s->dm->dmbus_state (s->dm, state); + s->first_connection = 0; + break; + default: + warning ("Unrecognized message ID: %d", m->hdr.msg_type); + } +} + +static void pop_message (struct service *s) +{ + union dmbus_msg *m = (union dmbus_msg *)s->buff; + uint32_t len = m->hdr.msg_len; + + if ((s->len < sizeof (struct dmbus_msg_hdr)) || + (s->len < len)) + return; + + memmove(s->buff, s->buff + len, s->len - len); + s->len -= len; +} + +static void +handle_disconnect (struct service *s) +{ + struct timeval time; + + event_del (&s->ev); + s->dm->running = 0; + v4v_close (s->fd); + s->fd = -1; + + warning ("Remote service \"%s\" disconnected.", s->dm->name); + + if (s->dm->dmbus_state) + s->dm->dmbus_state (s->dm, DMBUS_DISCONNECT); + + info ("Try to reconnect in %u ms", DMBUS_TIMEOUT); + time.tv_sec = DMBUS_TIMEOUT / 1000; + time.tv_usec = (DMBUS_TIMEOUT % 1000) * 1000; + event_add (&s->ev_reconnect, &time); +} + +static union dmbus_msg *sync_recv (struct service *s) +{ + int rc; + union dmbus_msg *m = (union dmbus_msg *)s->buff; + + while ((s->len < sizeof (struct dmbus_msg_hdr)) + || (s->len < m->hdr.msg_len)) + { + rc = v4v_recv (s->fd, s->buff + s->len, sizeof (s->buff) - s->len, 0); + switch (rc) { + case 0: + handle_disconnect (s); + return NULL; + case -1: + if (errno == EINTR) + continue; + error ("recv error: %s", strerror (errno)); + return NULL; + default: + s->len += rc; + } + + } + + return m; +} + +static void dmbus_fd_handler (int fd, short ev, void *opaque) +{ + int rc; + struct service *s = opaque; + union dmbus_msg *m = (union dmbus_msg *)s->buff; + + (void) fd; + (void) ev; + + do { + rc = v4v_recv (s->fd, s->buff + s->len, sizeof (s->buff) - s->len, + MSG_DONTWAIT); + switch (rc) { + case 0: + handle_disconnect (s); + return; + case -1: + if (errno == EINTR) + continue; + error ("recv error: %s", strerror (errno)); + return; + default: + s->len += rc; + } + } while (rc <= 0); + m = sync_recv (s); + if (!m) + return; + + while ((s->len >= sizeof (struct dmbus_msg_hdr)) + && (s->len >= m->hdr.msg_len)) + { + handle_message (s, m); + pop_message (s); + } +} + +static bool dmbus_connect (struct service *s) +{ + int rc = 0; + + s->fd = v4v_socket (SOCK_STREAM); + if (s->fd == -1) + { + error ("Failed to create v4v socket: %s ", strerror (errno)); + return false; + } + rc = v4v_connect (s->fd, &s->peer); + if (rc == -1) + { + error ("Failed to connect v4v socket: %s", strerror (errno)); + v4v_close (s->fd); + return false; + } + rc = v4v_send (s->fd, &s->prologue, sizeof (s->prologue), 0); + if (rc != sizeof (s->prologue)) + { + error ("Failed to initialize dmbus connection: %s", strerror (errno)); + v4v_close (s->fd); + return false; + } + + return true; +} + +static void dmbus_reconnect (int fd, short ev, void *opaque) +{ + struct timeval time; + struct service *s = opaque; + + (void) fd; + (void) ev; + + info ("Try to reconnect to service \"%s\"", s->dm->name); + + if (!dmbus_connect (s)) + goto rearm; + + event_set (&s->ev, s->fd, EV_PERSIST | EV_READ, dmbus_fd_handler, s); + event_add (&s->ev, NULL); + + return; +rearm: + s->fd = -1; + warning ("Failed. Try to reconnect in %u ms", DMBUS_TIMEOUT); + time.tv_sec = DMBUS_TIMEOUT / 1000; + time.tv_usec = (DMBUS_TIMEOUT % 1000) * 1000; + event_add (&s->ev_reconnect, &time); +} + +int dmbus_sync_recv (dmbus_service_t service, uint32_t type, + void *data, size_t size) +{ + struct service *s = service; + union dmbus_msg *m; + + m = sync_recv(s); + if (!m) + return -1; + + while (m->hdr.msg_type != type) { + handle_message (s, m); + pop_message (s); + m = sync_recv (s); + if (!m) + return -1; + } + + if (size > m->hdr.msg_len) + size = m->hdr.msg_len; + + memcpy (data, m, size); + pop_message (s); + + return size; +} + +static void fill_hash (uint8_t *h) +{ + const char *hash_str = DMBUS_SHA1_STRING; + size_t i; + + for (i = 0; i < 20; i++) { + unsigned int c; + + sscanf (hash_str + 2 * i, "%02x", &c); + h[i] = c; + } +} + +dmbus_service_t dmbus_service_connect (int service, DeviceType devtype, + domid_t domid, s_spawn_dm *dm) +{ + struct service *s; + struct timeval time; + + s = calloc (1, sizeof (*s)); + if (!s) + return NULL; + + s->dm = dm; + s->peer.port = DMBUS_BASE_PORT + service; + s->peer.domain = 0; /* Dom 0 */ + s->prologue.domain = domid; + s->prologue.type = devtype; + fill_hash (s->prologue.hash); + + s->first_connection = 1; + + event_set (&s->ev_reconnect, -1, EV_TIMEOUT, dmbus_reconnect, s); + + if (!dmbus_connect (s)) + { + info ("Try to reconnect in %u ms", DMBUS_TIMEOUT); + time.tv_sec = DMBUS_TIMEOUT / 1000; + time.tv_usec = (DMBUS_TIMEOUT % 1000) * 1000; + event_add (&s->ev_reconnect, &time); + } + else + { + event_set (&s->ev, s->fd, EV_READ | EV_PERSIST, dmbus_fd_handler, s); + event_add (&s->ev, NULL); + } + + return s; +} + +void dmbus_service_disconnect (dmbus_service_t service) +{ + struct service *s = service; + + event_del (&s->ev_reconnect); + + if (s->fd != -1) + { + event_del (&s->ev); + v4v_close (s->fd); + } + free (s); +} + +int dmbus_send (dmbus_service_t service, + uint32_t msgtype, + void *data, + size_t len) +{ + struct service *s = service; + struct dmbus_msg_hdr *hdr = data; + int rc; + size_t b = 0; + + hdr->msg_type = msgtype; + hdr->msg_len = len; + + while (b < len) + { + rc = v4v_send (s->fd, data + b, len - b, 0); + if (rc == -1) + { + if (errno == ECONNRESET) + handle_disconnect(s); + else + error ("failed: %s", strerror (errno)); + return -1; + } + + b += rc; + } + + return b; +} diff --git a/src/qemu-wrapper.c b/src/qemu-wrapper.c new file mode 100644 index 0000000..2a081d7 --- /dev/null +++ b/src/qemu-wrapper.c @@ -0,0 +1,732 @@ +/* + * Copyright (c) 2013 Citrix Systems, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#ifdef SYSLOG +# include +#endif +#include +#include +#include +#include "arguments.h" +#include "qemu-wrapper.h" +#include "util.h" + +# define QEMU_OLD_PATH "/usr/bin/qemu-dm-wrapper-old" +# define QEMU_OLD_STUBDOMAIN_PATH "/usr/lib/xen/bin/qemu-dm" +# define QEMU_NEW_PATH "/usr/bin/qemu-system-i386" + +/** + * FIXME: + * - destroy dmbus connection on fork + * - check the command line. For the moment we assume that it's valid + */ + +static struct +{ + /* Xen store handle */ + struct xs_handle *xsh; + /* Xen store event */ + struct event xs_ev; + char *dompath; + unsigned int domid; + unsigned int num_watch; + /* Number of QEMU spawned */ + unsigned int num_dm; + int shutting_down; + /* Don't spawn multiple qemu if it's an HVM */ + int is_hvm; + int in_stubdomain; + /* first pci slot available */ + uint8_t pcidev_slot; +} g_info; + +static uint8_t pcidev_get_slot (void) +{ + return g_info.pcidev_slot++; +} + +static int dm_has_cap (const s_spawn_dm *dm, cap_t cap) +{ + return (dm->cap & cap); +} + +static char * const *build_args_qemu_old (const s_spawn_dm *dm, + int argc, char * const *argv) +{ + s_arguments args; + int i = 0; + int has_xengfx = 0; + + (void) dm; + + arguments_init (&args); + + /* Don't use a wrapper if we are in stubdomain */ + arguments_add (&args, g_info.in_stubdomain ? QEMU_OLD_STUBDOMAIN_PATH : QEMU_OLD_PATH); + + if (find_arg (argv, "-xengfx")) + has_xengfx = 1; + + for (i = 0; i < argc; i++) + { + if (i == 0 && g_info.in_stubdomain) /* Skip domid */ + continue; + else if (!strcmp ("-surfman", argv[i]) && has_xengfx) + { + arguments_add (&args, "-vga"); + arguments_add (&args, "none"); + } + else if (!strcmp ("-net", argv[i]) + && dm_has_cap (dm, DM_CAP_NETWORK) != DM_CAP_NETWORK) + i++; + else if (!strcmp ("-videoram", argv[i]) && has_xengfx) + i++; + else + arguments_add (&args, argv[i]); + } + + if (dm_has_cap (dm, DM_CAP_NETWORK) != DM_CAP_NETWORK) + { + arguments_add (&args, "-net"); + arguments_add (&args, "none"); + } + + return arguments_get (&args); +} + +static int qemu_new_net (const s_spawn_dm *dm, char * const *argv, + s_arguments *args) +{ + char *tmp; + char *type; + char *value; + char *opt; + char *id = NULL; + char *mac = NULL; + char *model = NULL; + char *ifname = NULL; + int res = 1; + char str[200]; + + if (strcmp (argv[0], "-net")) + return 0; + if (!argv[1]) + return 0; + + tmp = xstrdup (argv[1]); + + type = strtok (tmp, ","); + + if (!type) + return 0; + + while ((opt = strtok (NULL, ","))) + { + value = strchr (opt,'='); + + if (!value) + continue; + + *value = '\0'; + value ++; + + if (!strcmp ("vlan", opt)) + id = value; + else if (!strcmp ("macaddr", opt)) + mac = value; + else if (!strcmp ("model", opt)) + model = value; + else if (!strcmp ("ifname", opt)) + ifname = value; + } + + info ("%s id = %s cap = 0x%x", dm->name, id, dm->cap); + + if (!id || (atoi(id) && !dm_has_cap (dm, DM_CAP_WIFI)) + || (!atoi(id) && !dm_has_cap (dm, DM_CAP_WIRED))) + { + info ("Out"); + res = 0; + } + else if (!strcmp ("nic", type) && model && mac) + { + arguments_add (args, "-device"); + snprintf (str, sizeof (str), "%s,id=%s%s,netdev=net%s,mac=%s,addr=%u", + model, atoi(id) ? "vwif" : "vif", id, id, mac, + pcidev_get_slot ()); + arguments_add (args, str); + } + else if (!strcmp ("tap", type) && ifname) + { + arguments_add (args, "-netdev"); + snprintf (str, sizeof (str), "type=tap,id=net%s,ifname=%s,script=no,downscript=no", + id, ifname); + arguments_add (args, str); + } + else + res = 0; + + free (tmp); + + return res; +} + +static char * const *build_args_qemu_new (const s_spawn_dm *dm, + int argc, char * const * argv) +{ + s_arguments args; + char str[100]; + char * const *arg = NULL; + int i = 0; + + (void) argc; + + arguments_init (&args); + + arguments_add (&args, QEMU_NEW_PATH); + arguments_add (&args, "-xen-domid"); + + snprintf (str, sizeof (str), "%u", g_info.domid); + arguments_add (&args, str); + + arguments_add (&args, "-nodefaults"); + arguments_add (&args, "-name"); + + snprintf (str, sizeof (str), "%u-%s", g_info.domid, dm->name); + arguments_add (&args, str); + + arguments_add (&args, "-machine"); + + snprintf (str, sizeof (str), "xenfv,xen_dmid=%u,xen_default_dev=off", dm->dmid); + arguments_add (&args, str); + + arguments_add (&args, "-nographic"); + + arg = find_arg (argv, "-m"); + + if (arg && arg[1]) + { + arguments_add (&args, "-m"); + arguments_add (&args, arg[1]); + } + + for (i = 0; i < argc; i++) + { + if (dm_has_cap (dm, DM_CAP_NETWORK)) + i += qemu_new_net (dm, &argv[i], &args); + } + + return arguments_get (&args); +} + +static int is_needed_qemu_old (const s_spawn_dm *dm, + int argc, char * const *argv) +{ + (void) dm; + (void) argc; + (void) argv; + + return g_info.is_hvm; +} + +static int is_needed_qemu (const s_spawn_dm *dm, + int argc, char * const *argv) +{ + (void) dm; + (void) argc; + (void) argv; + + return 0; + return g_info.is_hvm && !g_info.in_stubdomain; +} + +static int is_needed_surfman (const s_spawn_dm *dm, + int argc, char * const *argv) +{ + int i = 0; + (void) dm; + + /* For pv we use xenfb graphical card */ + if (!g_info.is_hvm) + return (dm->devtype == DEVICE_TYPE_XENFB); + + for (i = 0; i < argc; i++) + { + if (!strcmp ("-xengfx", argv[i]) && dm->devtype == DEVICE_TYPE_XENGFX) + return 1; + } + + return 0; +} + +static int is_needed_input (const s_spawn_dm *dm, + int argc, char * const * argv) +{ + (void) dm; + (void) argc; + (void) argv; + + return !g_info.is_hvm; +} + +static void dmbus_state_surfman (const s_spawn_dm *dm, e_dmbus_state state) +{ + (void)dm; + + if (state == DMBUS_DISCONNECT && g_info.is_hvm) + fatal ("Surfman is not able to handle hvm reconnection"); +} + +static void dmbus_state_input (const s_spawn_dm *dm, e_dmbus_state state) +{ + struct msg_switcher_abs msg; + + switch (state) + { + case DMBUS_CONNECT: + case DMBUS_RECONNECT: + msg.enabled = 0; + dmbus_send (dm->serv, DMBUS_MSG_SWITCHER_ABS, &msg, sizeof (msg)); + break; + default: + break; + } +} + +/** + * Some information about the structure: + * - We can only support one old QEMU (ie ioemu) + * - Device model name must be unique. It's used with xen_watch + */ + +static s_spawn_dm dms[] = { + /* Always spawn qemu-old, it's capabilities is build dynamically */ + SPAWN_QEMU_OLD ("qemu-old", is_needed_qemu_old, build_args_qemu_old), + SPAWN_QEMU ("qemu-net", is_needed_qemu, build_args_qemu_new, DM_CAP_WIRED), + SPAWN_QEMU ("qemu-net2", is_needed_qemu, build_args_qemu_new, DM_CAP_WIFI), + SPAWN_DMBUS ("surfman-xengfx", is_needed_surfman, dmbus_state_surfman, + DMBUS_SERVICE_SURFMAN, DEVICE_TYPE_XENGFX), + SPAWN_DMBUS ("surfman-xenfb", is_needed_surfman, dmbus_state_surfman, + DMBUS_SERVICE_SURFMAN, DEVICE_TYPE_XENFB), + SPAWN_DMBUS ("input-pv", is_needed_input, dmbus_state_input, + DMBUS_SERVICE_INPUT, DEVICE_TYPE_INPUT), + SPAWN_END_OF_LIST (), +}; + +static void kill_dms () +{ + const s_spawn_dm *dm; + + FOREACH_DMS (dm) + { + switch (dm->type) + { + case SPAWN_QEMU_OLD: + case SPAWN_QEMU: + if (dm->pid > 0 && kill (dm->pid, SIGTERM) == -1) + warning ("Unable to kill pid %d", dm->pid); + else if (dm->pid > 0) + info ("kill \"%s\"", dm->name); + break; + case SPAWN_DMBUS: + if (dm->serv) + { + dmbus_service_disconnect (dm->serv); + info ("kill \"%s\"", dm->name); + } + break; + default: + error ("Unknow how to kill type %u", dm->type); + } + } +} + +static void handle_signal (int signal) +{ + pid_t child; + int status; + s_spawn_dm *dm; + + if (g_info.shutting_down) + return; + + g_info.shutting_down = 1; + + switch (signal) + { + case SIGABRT: + warning ("abort"); + break; + case SIGCHLD: + child = wait (&status); + if (child != -1) + { + FOREACH_DMS (dm) + { + if (dm->pid != child) + continue; + + if (!dm->running) + warning ("child \"%s\" died during startup signal", dm->name); + else + warning ("child \"%s\" died", dm->name); + if (WIFSIGNALED (status)) + warning ("signal = %d", WTERMSIG(status)); + + dm->pid = -1; + + break; + } + } + else + warning ("unknow child died"); + break; + case SIGTERM: + info ("terminated"); + break; + } + + /** + * Kill all children. We can't let a child alive cause something + * goes wrong. + **/ + kill_dms (); + + if (g_info.xsh) + xs_close (g_info.xsh); + + if (g_info.dompath) + free (g_info.dompath); + + if (g_info.in_stubdomain) + sleep (2); /* Sleep few seconds to be sure that consoled retrieve all logs */ + exit (0); +} + +static const char *dm_xs_path (s_spawn_dm *dm, char *path, unsigned int len) +{ + if (dm->type == SPAWN_QEMU_OLD) + snprintf (path, len, "%s/dms/qemu-old", g_info.dompath); + else + snprintf (path, len, "%s/dms/%u/state", g_info.dompath, dm->dmid); + + return path; +} + +static void dump_cmdline (s_spawn_dm *dm, char * const *argv) +{ + char *buf = NULL; + char *tmp = NULL; + + while (*argv) + { + if (buf) + { + if (asprintf (&tmp, "%s %s", buf, *argv) == -1) + { + free (buf); + return; + } + free (buf); + buf = tmp; + } + else + { + buf = strdup (*argv); + if (!buf) + return; + } + if (buf && asprintf (&tmp, "%s %s", buf, *argv) == -1) + { + free (buf); + return; + } + argv++; + } + + if (buf) + info ("%s cmdline: %s", dm->name, buf); + + free (buf); +} + +static void spawn_process (s_spawn_dm *dm, int argc, char * const *argv) +{ + char path[100]; + int i = 0; + + info ("fork \"%s\"", dm->name); + + if (dm->type != SPAWN_QEMU_OLD) + dm->dmid = g_info.num_dm++; + + /* Build command line before fork. Otherwise the bdf will be wrong */ + argv = dm->build_args (dm, argc, argv); + + dm->pid = fork (); + if (dm->pid == 0) + { + if (g_info.xsh) + xs_close (g_info.xsh); + if (g_info.dompath) + free (g_info.dompath); + info ("exec \"%s\"", dm->name); + dump_cmdline (dm, argv); + execv (argv[0], argv); + fatal ("failed to exec \"%s\" path \"%s\"", dm->name, argv[0]); + } + else if (dm->pid == -1) + fatal ("failed to fork \"%s\" errno = %d\n", dm->name, errno); + + if (!xs_watch (g_info.xsh, dm_xs_path (dm, path, sizeof (path)), dm->name)) + fatal ("failed to watch \"%s\" in xenstore", dm->name); + + for (i = 0; argv[i]; i++) + free (argv[i]); + + free ((void *)argv); +} + +static void spawn_dmbus (s_spawn_dm *dm) +{ + info ("connect to dmbus for \"%s\"", dm->name); + dm->serv = dmbus_service_connect (dm->service, dm->devtype, + g_info.domid, dm); + if (!dm->serv) + fatal ("failed to connect to service \"%s\"", dm->name); +} + +static s_spawn_dm *find_dm (const char *name) +{ + s_spawn_dm *dm; + + FOREACH_DMS (dm) + { + if (!strcmp (dm->name, name)) + return dm; + } + + return NULL; +} + +static void dm_ready (s_spawn_dm *dm, int reconnect) +{ + char path[100]; + + if (!dm->running) + { + info ("Dm \"%s\" is ready", dm->name); + if (!reconnect) + g_info.num_watch--; + } + + dm->running = 1; + + if (!g_info.num_watch && !reconnect) + { + info ("All device models are now ready"); + snprintf (path, sizeof (path), "%s/device-misc/dm-ready", + g_info.dompath); + if (!xs_write (g_info.xsh, XBT_NULL, path, "1", 1)) + fatal ("Failed to write in xenstore"); + } +} + +static void dm_ready_xs (int fd, short ev, void *opaque) +{ + unsigned int count; + char **vec = xs_read_watch (g_info.xsh, &count); + s_spawn_dm *dm = NULL; + char *res; + char path[100]; + unsigned int len; + + (void) fd; + (void) ev; + (void) opaque; + + if (!vec) + return; + + if (!(dm = find_dm (vec[XS_WATCH_TOKEN]))) + { + warning ("invalid token %s", vec[XS_WATCH_TOKEN]); + goto end_dm_ready; + } + + res = xs_read (g_info.xsh, XBT_NULL, dm_xs_path (dm, path, sizeof (path)), + &len); + + if (!res) + goto end_dm_ready; + + free (res); + + /* Fix: perhaps we should check the value */ + + if (!dm->running) + xs_unwatch (g_info.xsh, dm_xs_path (dm, path, sizeof (path)), + dm->name); + + dm_ready (dm, 0); + +end_dm_ready: + free (vec); +} + +void dm_ready_dmbus (s_spawn_dm *dm, int reconnect) +{ + dm_ready (dm, reconnect); +} + +static void qemu_old_build_cap (int argc, char * const *argv) +{ + s_spawn_dm *qemu_old = NULL; + s_spawn_dm *dm = dms; + cap_t cap = 0; + + FOREACH_DMS (dm) + { + if (dm->is_needed && !dm->is_needed (dm, argc, argv)) + continue; + + switch (dm->type) + { + case SPAWN_QEMU_OLD: + qemu_old = dm; + break; + default: + cap |= dm->cap; + } + } + + /* The old QEMU emulate all devices that the others dms don't handle */ + if (qemu_old) + qemu_old->cap = ~cap; +} + +static void initialize_wrapper (int argc, char * const *argv) +{ + int i = 0; + + /* First pci slot available is 00:0b.0 */ + g_info.pcidev_slot = 0xb; + + g_info.is_hvm = 1; + + for (i = 0; i < argc; i++) + { + if (!strcmp ("-M", argv[i])) + { + if ((i + 1) < argc && !strcmp ("xenpv", argv[i + 1])) + g_info.is_hvm = 0; + i++; + } + else if (!strcmp ("-stubdom", argv[i])) + g_info.in_stubdomain = 1; + } + + g_info.xsh = xs_open (0); + if (!g_info.xsh) + fatal ("Unable to open Xenstore"); + + if (!(g_info.dompath = xs_get_domain_path (g_info.xsh, g_info.domid))) + fatal ("Unable to retrieve domain path"); + + qemu_old_build_cap (argc, argv); +} + +void write_pid (void) +{ + char path[100]; + char pid[20]; + + snprintf (path, sizeof (path), "%s/qemu-pid", g_info.dompath); + snprintf (pid, sizeof (pid), "%u", getpid ()); + + if (!xs_write (g_info.xsh, XBT_NULL, path, pid, strlen (pid))) + fatal ("Unable to write PID in Xenstore"); +} + +int main (int argc, char **argv) +{ + s_spawn_dm *dm; + + g_info.domid = atoi (argv[1]); + + argv++; + argc--; + + signal (SIGABRT, handle_signal); + signal (SIGCHLD, handle_signal); + signal (SIGTERM, handle_signal); + + +#ifdef SYSLOG + { + char path[100]; + + snprintf (path, sizeof (path), "dm-wrapper-%u", g_info.domid); + openlog (path, LOG_CONS, LOG_USER); + } +#endif + + initialize_wrapper (argc, argv); + + if (!g_info.in_stubdomain) + write_pid (); + + event_init (); + + /* spawn device model */ + FOREACH_DMS (dm) + { + if (dm->is_needed && !dm->is_needed (dm, argc, argv)) + continue; + info ("spawn dm \"%s\"", dm->name); + + switch (dm->type) + { + case SPAWN_QEMU_OLD: + case SPAWN_QEMU: + spawn_process (dm, argc, argv); + break; + case SPAWN_DMBUS: + spawn_dmbus (dm); + break; + default: + warning ("Invalid type %u for dm \"%s\"", dm->type, dm->name); + } + g_info.num_watch++; + } + + event_set (&g_info.xs_ev, xs_fileno (g_info.xsh), EV_READ | EV_PERSIST, + dm_ready_xs, dms); + event_add (&g_info.xs_ev, NULL); + + event_dispatch (); + + fatal ("must never leave"); + + return 0; +} diff --git a/src/qemu-wrapper.h b/src/qemu-wrapper.h new file mode 100644 index 0000000..825804a --- /dev/null +++ b/src/qemu-wrapper.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2012 Citrix Systems, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef QEMU_WRAPPER_H_ +# define QEMU_WRAPPER_H_ + +# include +# include + +typedef enum +{ + SPAWN_QEMU_OLD, + SPAWN_QEMU, + SPAWN_DMBUS, + SPAWN_INVALID, +} e_spawn_type; + +typedef struct spawn_dm s_spawn_dm; +typedef struct service *dmbus_service_t; + +typedef char * const *(*build_args_func)(const s_spawn_dm *dm, + int argc, char * const *argv); +typedef int (*is_needed_func)(const s_spawn_dm *dm, + int argc, char * const *argv); + +typedef enum dmbus_state +{ + DMBUS_CONNECT, + DMBUS_RECONNECT, + DMBUS_DISCONNECT, +} e_dmbus_state; + +typedef void (*dmbus_state_func) (const s_spawn_dm *dm, e_dmbus_state state); + +void dm_ready_dmbus (s_spawn_dm *dm, int reconnect); + +typedef uint32_t cap_t; + +struct spawn_dm +{ + const char* name; + e_spawn_type type; + is_needed_func is_needed; + build_args_func build_args; + dmbus_state_func dmbus_state; + pid_t pid; + int running; + unsigned int dmid; + int service; + DeviceType devtype; + dmbus_service_t serv; + cap_t cap; /* Device model capabilities */ +}; + +#define DM_CAP_NONE (0) +/* Emulate wired network */ +#define DM_CAP_WIRED (1 << 0) +/* Emulate wifi network */ +#define DM_CAP_WIFI (1 << 1) +/** + * Be careful, we can't emulate only one network card outside QEMU old + */ +#define DM_CAP_NETWORK (DM_CAP_WIRED | DM_CAP_WIFI) + +#define SPAWN_END_OF_LIST() \ +{ NULL, SPAWN_INVALID, NULL, NULL, NULL, -1, 0, 0, 0, 0, NULL, 0 } + +#define SPAWN_QEMU_OLD(name, is_needed, build_args) \ +{ name, SPAWN_QEMU_OLD, is_needed, build_args, NULL, -1, 0, 0, 0, 0, NULL, 0 } + +#define SPAWN_QEMU(name, is_needed, build_args, cap) \ +{ name, SPAWN_QEMU, is_needed, build_args, NULL, -1, 0, 0, 0, 0, NULL, cap } + +#define SPAWN_DMBUS(name, is_needed, dmbus_state, service, devtype) \ +{ name, SPAWN_DMBUS, is_needed, NULL, dmbus_state, -1, 0, 0, service, \ + devtype, NULL, 0 } + +# define FOREACH_DMS(it) \ + for ((it) = dms; (it) && (it)->type != SPAWN_INVALID; (it)++) + + +dmbus_service_t dmbus_service_connect (int service, DeviceType devtype, + domid_t domid, s_spawn_dm *dm); +void dmbus_service_disconnect (dmbus_service_t service); +int dmbus_sync_recv (dmbus_service_t service, uint32_t type, void *data, + size_t size); +int dmbus_send (dmbus_service_t service, uint32_t msgtype, void *data, + size_t len); + +#endif /* !QEMU_WRAPPER_H_ */ diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..ff034a7 --- /dev/null +++ b/src/util.c @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2012 Citrix Systems, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#define _BSD_SOURCE +#include +#include +#include +#include +#include "util.h" + +void +message (int flags, const char *file, const char *function, int line, + const char *fmt, ...) +{ + va_list ap; + +#ifndef SYSLOG + char *level = NULL; + + if (flags & MESSAGE_INFO) + { + level = "Info"; + } + else if (flags & MESSAGE_WARNING) + { + level = "Warning"; + } + else if (flags & MESSAGE_ERROR) + { + level = "Error"; + } + else if (flags & MESSAGE_FATAL) + { + level = "Fatal"; + } + + fprintf (stderr, "%s:%s:%s:%d:", level, file, function, line); + + va_start (ap, fmt); + vfprintf (stderr, fmt, ap); + va_end (ap); + + fprintf (stderr, "\n"); + fflush (stderr); +#else + (void) file; + (void) function; + (void) line; + + + if (flags & (MESSAGE_INFO | MESSAGE_WARNING | MESSAGE_ERROR | MESSAGE_FATAL)) + { + va_start (ap, fmt); + vsyslog (LOG_ERR, fmt, ap); + va_end (ap); + } +#endif /* !SYSLOG */ + + if (flags & MESSAGE_FATAL) + { + abort (); + } +} + +void * +xcalloc (size_t n, size_t s) +{ + void *ret = calloc (n, s); + if (!ret) + fatal ("calloc failed"); + return ret; +} + +void * +xmalloc (size_t s) +{ + void *ret = malloc (s); + if (!ret) + fatal ("malloc failed"); + return ret; +} + +void * +xrealloc (void *p, size_t s) +{ + p = realloc (p, s); + if (!p) + fatal ("realloc failed"); + return p; +} + +char * +xstrdup (const char *s) +{ + char *ret = strdup (s); + if (!ret) + fatal ("strdup failed"); + return ret; +} + diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..8b2434b --- /dev/null +++ b/src/util.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2012 Citrix Systems, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef UTIL_H_ +# define UTIL_H_ + +# include + +/* + ** Utils and debug tools. + */ +# define MESSAGE_INFO (1UL << 0) +# define MESSAGE_WARNING (1UL << 1) +# define MESSAGE_ERROR (1UL << 2) +# define MESSAGE_FATAL (1UL << 3) + +# define info(a...) message(MESSAGE_INFO,__FILE__,__PRETTY_FUNCTION__,__LINE__,a) +# define warning(a...) message(MESSAGE_WARNING,__FILE__,__PRETTY_FUNCTION__,__LINE__,a) +# define error(a...) message(MESSAGE_ERROR,__FILE__,__PRETTY_FUNCTION__,__LINE__,a) +# define fatal(a...) message(MESSAGE_FATAL,__FILE__,__PRETTY_FUNCTION__,__LINE__,a) + +void message (int flags, const char *file, const char *function, int line, + const char *fmt, ...) __attribute__ ((format (printf, 5, 6))); + +void *xcalloc (size_t n, size_t s); +void *xmalloc (size_t s); +void *xrealloc (void *p, size_t s); +char *xstrdup (const char *s); + +#endif /* !UTIL_H_ */