LCOV - code coverage report
Current view: top level - tapback - tapback.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 353 0
Test Date: 2026-06-26 00:00:00 Functions: 0.0 % 13 0
Branches: 0.0 % 185 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2016, Citrix Systems, Inc.
       3                 :             :  *
       4                 :             :  * All rights reserved.
       5                 :             :  *
       6                 :             :  * Redistribution and use in source and binary forms, with or without
       7                 :             :  * modification, are permitted provided that the following conditions are met:
       8                 :             :  * 
       9                 :             :  *  1. Redistributions of source code must retain the above copyright
      10                 :             :  *     notice, this list of conditions and the following disclaimer.
      11                 :             :  *  2. Redistributions in binary form must reproduce the above copyright
      12                 :             :  *     notice, this list of conditions and the following disclaimer in the
      13                 :             :  *     documentation and/or other materials provided with the distribution.
      14                 :             :  *  3. Neither the name of the copyright holder nor the names of its 
      15                 :             :  *     contributors may be used to endorse or promote products derived from 
      16                 :             :  *     this software without specific prior written permission.
      17                 :             :  *
      18                 :             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      19                 :             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      20                 :             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      21                 :             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
      22                 :             :  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
      23                 :             :  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
      24                 :             :  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
      25                 :             :  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
      26                 :             :  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
      27                 :             :  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
      28                 :             :  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      29                 :             :  *
      30                 :             :  * This file contains the core of the tapback daemon, the user space daemon
      31                 :             :  * that acts as a device's back-end.
      32                 :             :  */
      33                 :             : 
      34                 :             : /*
      35                 :             :  * TODO Some of these includes may be useless.
      36                 :             :  * TODO Replace hard-coding strings with defines/const string.
      37                 :             :  */
      38                 :             : #include <stdlib.h>
      39                 :             : #include <stdarg.h>
      40                 :             : #include <assert.h>
      41                 :             : #include <syslog.h>
      42                 :             : #include <fcntl.h>
      43                 :             : #include <unistd.h>
      44                 :             : #include <libgen.h>
      45                 :             : #include <getopt.h>
      46                 :             : #include <sys/ioctl.h>
      47                 :             : #include <sys/mount.h>
      48                 :             : #include <syslog.h>
      49                 :             : #include <time.h>
      50                 :             : 
      51                 :             : #include "config.h"
      52                 :             : #include "blktap3.h"
      53                 :             : #include "stdio.h" /* TODO tap-ctl.h needs to include stdio.h */
      54                 :             : #include "tap-ctl.h"
      55                 :             : #include "tapback.h"
      56                 :             : #include <signal.h>
      57                 :             : 
      58                 :             : const char tapback_name[] = "tapback";
      59                 :             : unsigned log_level;
      60                 :             : int tapdev_major;
      61                 :             : 
      62                 :             : struct list_head backends = LIST_HEAD_INIT(backends);
      63                 :             : 
      64                 :           0 : char *xenbus_strstate(const XenbusState xbs)
      65                 :             : {
      66                 :           0 :     static char * const str[] = {
      67                 :             :         [XenbusStateUnknown] = "0 (unknown)",
      68                 :             :         [XenbusStateInitialising] = "1 (initialising)",
      69                 :             :         [XenbusStateInitWait] = "2 (init wait)",
      70                 :             :         [XenbusStateInitialised] = "3 (initialised)",
      71                 :             :         [XenbusStateConnected] = "4 (connected)",
      72                 :             :         [XenbusStateClosing] = "5 (closing)",
      73                 :             :         [XenbusStateClosed] = "6 (closed)",
      74                 :             :         [XenbusStateReconfiguring] = "7 (reconfiguring)",
      75                 :             :         [XenbusStateReconfigured] = "8 (reconfigured)"
      76                 :             :     };
      77                 :           0 :     return str[xbs];
      78                 :             : }
      79                 :             : 
      80                 :             : /**
      81                 :             :  * Read changes that occurred on the "backend/<backend name>" XenStore path
      82                 :             :  * or one of the front-end paths and act accordingly.
      83                 :             :  */
      84                 :             : static inline void
      85                 :           0 : tapback_read_watch(backend_t *backend)
      86                 :             : {
      87                 :           0 :     char **watch = NULL, *path = NULL, *token = NULL;
      88                 :           0 :     unsigned int n = 0;
      89                 :           0 :     int err = 0;
      90                 :             : 
      91         [ #  # ]:           0 :         ASSERT(backend);
      92                 :             : 
      93                 :             :     /* read the change */
      94                 :           0 :     watch = xs_read_watch(backend->xs, &n);
      95                 :           0 :     path = watch[XS_WATCH_PATH];
      96                 :           0 :     token = watch[XS_WATCH_TOKEN];
      97                 :             : 
      98                 :             :     /*
      99                 :             :      * print the path the watch triggered on for debug purposes
     100                 :             :      *
     101                 :             :      * TODO include token
     102                 :             :      */
     103         [ #  # ]:           0 :     if (verbose()) {
     104                 :           0 :         char *val = tapback_xs_read(backend->xs, XBT_NULL, "%s", path);
     105         [ #  # ]:           0 :         if (val) {
     106         [ #  # ]:           0 :             if (0 == strlen(val))
     107                 :             :                 /*
     108                 :             :                  * XXX "(created)" might be printed when the a XenStore
     109                 :             :                  * directory gets removed, the XenStore watch fires, and a
     110                 :             :                  * XenStore node is created under the directory that just got
     111                 :             :                  * removed. This usually happens when the toolstack removes the
     112                 :             :                  * VBD from XenStore and then immediately writes the
     113                 :             :                  * tools/xenops/cancel key in it.
     114                 :             :                  */
     115                 :           0 :                 DBG(NULL, "%s -> (created)\n", path);
     116                 :             :             else
     117                 :           0 :                 DBG(NULL, "%s -> \'%s\'\n", path, val);
     118                 :           0 :             free(val);
     119                 :             :         } else {
     120                 :           0 :             err = errno;
     121         [ #  # ]:           0 :             if (err == ENOENT)
     122                 :           0 :                 DBG(NULL, "%s -> (removed)\n", path);
     123                 :             :             else
     124                 :           0 :                 WARN(NULL, "failed to read %s: %s\n", path, strerror(err));
     125                 :             :         }
     126                 :             :     }
     127                 :             : 
     128                 :             :     /*
     129                 :             :      * The token indicates which XenStore watch triggered, the front-end one or
     130                 :             :      * the back-end one.
     131                 :             :      */
     132         [ #  # ]:           0 :     if (!strcmp(token, backend->frontend_token)) {
     133                 :           0 :         ASSERT(!tapback_is_master(backend));
     134                 :           0 :         err = -tapback_backend_handle_otherend_watch(backend, path);
     135         [ #  # ]:           0 :     } else if (!strcmp(token, backend->backend_token)) {
     136                 :           0 :         err = -tapback_backend_handle_backend_watch(backend, path);
     137                 :             :     } else {
     138                 :           0 :         WARN(NULL, "invalid token \'%s\'\n", token);
     139                 :             :         err = EINVAL;
     140                 :             :     }
     141                 :             : 
     142         [ #  # ]:           0 :         if (err)
     143                 :           0 :                 WARN(NULL, "failed to process XenStore watch on %s: %s\n",
     144                 :             :                                 path, strerror(abs(err)));
     145                 :             : 
     146                 :           0 :     free(watch);
     147                 :           0 :     return;
     148                 :             : }
     149                 :             : 
     150                 :             : /**
     151                 :             :  * NB must be async-signal-safe.
     152                 :             :  */
     153                 :             : void
     154                 :           0 : tapback_backend_destroy(backend_t *backend)
     155                 :             : {
     156         [ #  # ]:           0 :         if (!backend)
     157                 :             :                 return;
     158                 :             : 
     159         [ #  # ]:           0 :     if (backend->pidfile) {
     160                 :           0 :         unlink(backend->pidfile);
     161                 :           0 :         free(backend->pidfile);
     162                 :             :     }
     163                 :             : 
     164                 :           0 :     free(backend->name);
     165                 :           0 :     free(backend->path);
     166                 :           0 :     free(backend->frontend_token);
     167                 :           0 :     free(backend->backend_token);
     168                 :             : 
     169         [ #  # ]:           0 :     if (backend->xs) {
     170                 :           0 :         xs_daemon_close(backend->xs);
     171                 :           0 :         backend->xs = NULL;
     172                 :             :     }
     173                 :             : 
     174                 :           0 :     unlink(backend->local.sun_path);
     175                 :             : 
     176                 :           0 :         list_del(&backend->entry);
     177                 :             : 
     178                 :           0 :         free(backend);
     179                 :             : }
     180                 :             : 
     181                 :             : static void
     182                 :           0 : tapback_signal_handler(int signum, siginfo_t *siginfo __attribute__((unused)),
     183                 :             :         void *context __attribute__((unused)))
     184                 :             : {
     185         [ #  # ]:           0 :     if (likely(signum == SIGINT || signum == SIGTERM)) {
     186                 :           0 :                 backend_t *backend, *tmp;
     187                 :             : 
     188         [ #  # ]:           0 :                 list_for_each_entry(backend, &backends, entry) {
     189                 :           0 :             if (tapback_is_master(backend)) {
     190         [ #  # ]:           0 :                 if (backend->master.slaves)
     191                 :             :                     return;
     192                 :             :             } else {
     193                 :           0 :                 if (!list_empty(&backend->slave.slave.devices))
     194                 :             :                     return;
     195                 :             :             }
     196                 :             :                 }
     197                 :             : 
     198         [ #  # ]:           0 :                 list_for_each_entry_safe(backend, tmp, &backends, entry)
     199                 :           0 :                         tapback_backend_destroy(backend);
     200                 :             : 
     201                 :           0 :         _exit(EXIT_SUCCESS);
     202                 :             :     }
     203                 :             : }
     204                 :             : 
     205                 :             : static inline int
     206                 :           0 : tapback_write_pid(const char *pidfile)
     207                 :             : {
     208                 :           0 :     FILE *fp;
     209                 :           0 :     int err = 0;
     210                 :             : 
     211         [ #  # ]:           0 :     ASSERT(pidfile);
     212                 :             : 
     213                 :           0 :     fp = fopen(pidfile, "w");
     214         [ #  # ]:           0 :     if (!fp)
     215                 :           0 :         return errno;
     216                 :           0 :     err = fprintf(fp, "%d\n", getpid());
     217         [ #  # ]:           0 :     if (err < 0)
     218                 :           0 :         err = errno;
     219                 :             :     else
     220                 :             :         err = 0;
     221                 :           0 :     fclose(fp);
     222                 :           0 :     return err;
     223                 :             : }
     224                 :             : 
     225                 :             : /**
     226                 :             :  * Initializes the back-end descriptor. There is one back-end per tapback
     227                 :             :  * process. Also, it initiates a watch to XenStore on backend/<backend name>.
     228                 :             :  *
     229                 :             :  * @returns a new back-end, NULL on failure, sets errno
     230                 :             :  */
     231                 :             : static inline backend_t *
     232                 :           0 : tapback_backend_create(const char *name, const char *pidfile,
     233                 :             :         const domid_t domid, const bool barrier)
     234                 :             : {
     235                 :           0 :     int err;
     236                 :           0 :     int len;
     237                 :           0 :         backend_t *backend = NULL;
     238                 :             : 
     239         [ #  # ]:           0 :     ASSERT(name);
     240                 :             : 
     241                 :           0 :         backend = calloc(1, sizeof(*backend));
     242         [ #  # ]:           0 :         if (!backend) {
     243                 :           0 :                 err = errno;
     244                 :           0 :                 goto out;
     245                 :             :         }
     246                 :             : 
     247         [ #  # ]:           0 :     if (pidfile) {
     248                 :           0 :         backend->pidfile = strdup(pidfile);
     249         [ #  # ]:           0 :         if (unlikely(!backend->pidfile)) {
     250                 :           0 :             err = errno;
     251                 :           0 :             WARN(NULL, "failed to strdup: %s\n", strerror(err));
     252                 :           0 :             goto out;
     253                 :             :         }
     254                 :           0 :         err = tapback_write_pid(backend->pidfile);
     255         [ #  # ]:           0 :         if (unlikely(err)) {
     256                 :           0 :             WARN(NULL, "failed to write PID to %s: %s\n",
     257                 :             :                     pidfile, strerror(err));
     258                 :           0 :             goto out;
     259                 :             :         }
     260                 :             :     }
     261                 :             : 
     262                 :           0 :     backend->name = strdup(name);
     263         [ #  # ]:           0 :     if (!backend->name) {
     264                 :           0 :         err = errno;
     265                 :           0 :         goto out;
     266                 :             :     }
     267                 :             : 
     268                 :           0 :         backend->barrier = barrier;
     269                 :             : 
     270                 :           0 :     backend->path = NULL;
     271                 :             : 
     272         [ #  # ]:           0 :     INIT_LIST_HEAD(&backend->entry);
     273                 :             : 
     274         [ #  # ]:           0 :     if (domid) {
     275                 :           0 :         backend->slave_domid = domid;
     276                 :           0 :         INIT_LIST_HEAD(&backend->slave.slave.devices);
     277                 :           0 :         err = asprintf(&backend->path, "%s/%s/%d", XENSTORE_BACKEND,
     278                 :             :                 backend->name, backend->slave_domid);
     279         [ #  # ]:           0 :         if (err == -1) {
     280                 :           0 :             backend->path = NULL;
     281                 :           0 :             err = errno;
     282                 :           0 :             goto out;
     283                 :             :         }
     284                 :             :     } else {
     285                 :           0 :         backend->master.slaves = NULL;
     286                 :           0 :         err = asprintf(&backend->path, "%s/%s", XENSTORE_BACKEND,
     287                 :             :                 backend->name);
     288         [ #  # ]:           0 :         if (err == -1) {
     289                 :           0 :             backend->path = NULL;
     290                 :           0 :             err = errno;
     291                 :           0 :             goto out;
     292                 :             :         }
     293                 :             :     }
     294                 :             : 
     295         [ #  # ]:           0 :     if (domid) {
     296                 :           0 :         err = asprintf(&backend->frontend_token, "%s-%d-front", tapback_name,
     297                 :             :                 domid);
     298         [ #  # ]:           0 :         if (err == -1) {
     299                 :           0 :             backend->frontend_token = NULL;
     300                 :           0 :             err = errno;
     301                 :           0 :             goto out;
     302                 :             :         }
     303                 :             : 
     304                 :           0 :         err = asprintf(&backend->backend_token, "%s-%d-back", tapback_name,
     305                 :             :                 domid);
     306         [ #  # ]:           0 :         if (err == -1) {
     307                 :           0 :             backend->backend_token = NULL;
     308                 :           0 :             err = errno;
     309                 :           0 :             goto out;
     310                 :             :         }
     311                 :             :     } else {
     312                 :           0 :         err = asprintf(&backend->frontend_token, "%s-master-front",
     313                 :             :                 tapback_name);
     314         [ #  # ]:           0 :         if (err == -1) {
     315                 :           0 :             backend->frontend_token = NULL;
     316                 :           0 :             err = errno;
     317                 :           0 :             goto out;
     318                 :             :         }
     319                 :             : 
     320                 :           0 :         err = asprintf(&backend->backend_token, "%s-master-back",
     321                 :             :                 tapback_name);
     322         [ #  # ]:           0 :         if (err == -1) {
     323                 :           0 :             backend->backend_token = NULL;
     324                 :           0 :             err = errno;
     325                 :           0 :             goto out;
     326                 :             :         }
     327                 :             :     }
     328                 :             : 
     329                 :           0 :     err = 0;
     330                 :             : 
     331                 :           0 :     backend->ctrl_sock = -1;
     332                 :             : 
     333         [ #  # ]:           0 :     if (!(backend->xs = xs_daemon_open())) {
     334                 :           0 :         err = EINVAL;
     335                 :           0 :         goto out;
     336                 :             :     }
     337                 :             : 
     338                 :           0 :         err = get_my_domid(backend->xs, XBT_NULL);
     339         [ #  # ]:           0 :         if (err < 0) {
     340                 :             :                 /*
     341                 :             :                  * If the domid XenStore key is not yet written, it means we're running
     342                 :             :                  * in dom0, otherwise if we were running in a driver domain the key
     343                 :             :                  * would have been written before the domain had even started.
     344                 :             :                  *
     345                 :             :                  * XXX We can always set a XenStore watch as a fullproof solution.
     346                 :             :                  */
     347         [ #  # ]:           0 :                 if (err == -ENOENT) {
     348                 :           0 :                         INFO(NULL, "domid XenStore key not yet present, assuming we are "
     349                 :             :                                         "domain 0\n");
     350                 :           0 :                         err = 0;
     351                 :             :                 } else {
     352                 :           0 :                         err = -err;
     353                 :           0 :                         WARN(NULL, "failed to get current domain ID: %s\n", strerror(err));
     354                 :           0 :                         goto out;
     355                 :             :                 }
     356                 :             :         }
     357                 :           0 :         backend->domid = err;
     358                 :           0 :         err = 0;
     359                 :             : 
     360                 :             :     /*
     361                 :             :      * Watch the back-end.
     362                 :             :      */
     363         [ #  # ]:           0 :     if (!xs_watch(backend->xs, backend->path, backend->backend_token)) {
     364                 :           0 :         err = errno;
     365                 :           0 :         goto out;
     366                 :             :     }
     367                 :             : 
     368                 :             :     /*
     369                 :             :      * Create the control socket.
     370                 :             :      * XXX We don't listen for connections as we don't yet support any control
     371                 :             :      * commands.
     372                 :             :      */
     373                 :           0 :     backend->ctrl_sock = socket(AF_UNIX, SOCK_STREAM, 0);
     374         [ #  # ]:           0 :     if (backend->ctrl_sock == -1) {
     375                 :           0 :         err = errno;
     376                 :           0 :         WARN(NULL, "failed to create control socket: %s\n", strerror(errno));
     377                 :           0 :         goto out;
     378                 :             :     }
     379                 :           0 :     backend->local.sun_family = AF_UNIX;
     380         [ #  # ]:           0 :     if (domid)
     381                 :           0 :         err = snprintf(backend->local.sun_path,
     382                 :             :                 ARRAY_SIZE(backend->local.sun_path),
     383                 :             :                 "/var/run/%s.%d", tapback_name, domid);
     384                 :             :     else
     385                 :           0 :         err = snprintf(backend->local.sun_path,
     386                 :             :                 ARRAY_SIZE(backend->local.sun_path),
     387                 :             :                 "/var/run/%s.master", tapback_name);
     388         [ #  # ]:           0 :     if (err >= (int)ARRAY_SIZE(backend->local.sun_path)) {
     389                 :           0 :         err = ENAMETOOLONG;
     390                 :           0 :         WARN(NULL, "UNIX domain socket name too long\n");
     391                 :           0 :         goto out;
     392         [ #  # ]:           0 :     } else if (err < 0) {
     393                 :           0 :         err = errno;
     394                 :           0 :         WARN(NULL, "failed to snprintf: %s\n", strerror(err));
     395                 :           0 :         goto out;
     396                 :             :     }
     397                 :           0 :     err = 0;
     398                 :             : 
     399                 :           0 :     err = unlink(backend->local.sun_path);
     400   [ #  #  #  # ]:           0 :     if (err && errno != ENOENT) {
     401                 :           0 :         err = errno;
     402                 :           0 :         WARN(NULL, "failed to remove %s: %s\n", backend->local.sun_path,
     403                 :             :                 strerror(err));
     404                 :           0 :         goto out;
     405                 :             :     }
     406                 :           0 :     len = strlen(backend->local.sun_path) + sizeof(backend->local.sun_family);
     407                 :           0 :     err = bind(backend->ctrl_sock, (struct sockaddr *)&backend->local, len);
     408         [ #  # ]:           0 :     if (err == -1) {
     409                 :           0 :         err = errno;
     410                 :           0 :         WARN(NULL, "failed to bind to %s: %s\n", backend->local.sun_path,
     411                 :             :                 strerror(err));
     412                 :           0 :         goto out;
     413                 :             :     }
     414                 :             : 
     415                 :           0 :         list_add(&backend->entry, &backends);
     416                 :             : 
     417                 :           0 : out:
     418         [ #  # ]:           0 :         if (err) {
     419                 :           0 :                 tapback_backend_destroy(backend);
     420                 :           0 :                 backend = NULL;
     421                 :           0 :                 errno = err;
     422                 :             :         }
     423                 :             : 
     424                 :           0 :     return backend;
     425                 :             : }
     426                 :             : 
     427                 :             : /**
     428                 :             :  * Runs the daemon.
     429                 :             :  *
     430                 :             :  * Watches backend/<backend name> and the front-end devices.
     431                 :             :  */
     432                 :             : static inline int
     433                 :           0 : tapback_backend_run(backend_t *backend)
     434                 :             : {
     435                 :           0 :     int fd;
     436                 :           0 :         int err;
     437                 :             : 
     438         [ #  # ]:           0 :         ASSERT(backend);
     439                 :             : 
     440                 :           0 :         fd = xs_fileno(backend->xs);
     441                 :             : 
     442                 :           0 :     if (tapback_is_master(backend))
     443                 :           0 :         INFO(NULL, "master tapback daemon started\n");
     444                 :             :     else
     445                 :           0 :         INFO(NULL, "slave tapback daemon started, only serving domain %d\n",
     446                 :             :                 backend->slave_domid);
     447                 :             : 
     448                 :           0 :     do {
     449                 :           0 :         fd_set rfds;
     450                 :           0 :         int nfds = 0;
     451                 :             : 
     452         [ #  # ]:           0 :         FD_ZERO(&rfds);
     453   [ #  #  #  # ]:           0 :         FD_SET(fd, &rfds);
     454                 :             : 
     455                 :             :         /*
     456                 :             :          * poll the fd for changes in the XenStore path we're interested in
     457                 :             :          */
     458                 :           0 :         nfds = select(fd + 1, &rfds, NULL, NULL, NULL);
     459         [ #  # ]:           0 :         if (nfds == -1) {
     460         [ #  # ]:           0 :             if (likely(errno == EINTR))
     461                 :           0 :                 continue;
     462                 :           0 :             err = -errno;
     463                 :           0 :             WARN(NULL, "error monitoring XenStore: %s\n", strerror(-err));
     464                 :           0 :             break;
     465                 :             :         }
     466                 :             : 
     467   [ #  #  #  #  :           0 :         if (FD_ISSET(fd, &rfds))
                   #  # ]
     468                 :           0 :             tapback_read_watch(backend);
     469                 :           0 :         DBG(NULL, "--\n");
     470                 :             :     } while (1);
     471                 :             : 
     472                 :           0 :     return err;
     473                 :             : }
     474                 :             : 
     475                 :             : /**
     476                 :             :  * Print tapback's usage instructions.
     477                 :             :  */
     478                 :             : static void
     479                 :           0 : usage(FILE * const stream, const char * const prog)
     480                 :             : {
     481         [ #  # ]:           0 :     ASSERT(stream);
     482         [ #  # ]:           0 :     ASSERT(prog);
     483                 :             : 
     484                 :           0 :     fprintf(stream,
     485                 :             :             "usage: %s\n"
     486                 :             :             "\t[-d|--debug]\n"
     487                 :             :                         "\t[-h|--help]\n"
     488                 :             :             "\t[-v|--verbose]\n"
     489                 :             :                         "\t[-b]--nobarrier]\n"
     490                 :             :             "\t[-n|--name]\n", prog);
     491                 :           0 : }
     492                 :             : 
     493                 :             : extern char *optarg;
     494                 :             : 
     495                 :             : /**
     496                 :             :  * Returns 0 in success, -errno on failure.
     497                 :             :  */
     498                 :             : static inline int
     499                 :           0 : tapback_install_sighdl(void)
     500                 :             : {
     501                 :           0 :     int err;
     502                 :           0 :     struct sigaction sigact;
     503                 :           0 :         sigset_t set;
     504                 :           0 :     static const int signals[] = {SIGTERM, SIGINT, SIGCHLD};
     505                 :           0 :     unsigned i;
     506                 :             : 
     507                 :           0 :     err = sigfillset(&set);
     508         [ #  # ]:           0 :     if (unlikely(err == -1)) {
     509                 :           0 :         err = errno;
     510                 :           0 :         WARN(NULL, "failed to fill signal set: %s\n", strerror(err));
     511                 :           0 :         goto out;
     512                 :             :     }
     513                 :             : 
     514         [ #  # ]:           0 :     for (i = 0; i < ARRAY_SIZE(signals); i++) {
     515                 :           0 :         err = sigdelset(&set, signals[i]);
     516         [ #  # ]:           0 :         if (unlikely(err == -1)) {
     517                 :           0 :             err = errno;
     518                 :           0 :             WARN(NULL, "failed to add signal %d to signal set: %s\n",
     519                 :             :                     signals[i], strerror(err));
     520                 :           0 :             goto out;
     521                 :             :         }
     522                 :             :     }
     523                 :             : 
     524                 :           0 :     err = sigprocmask(SIG_BLOCK, &set, NULL);
     525         [ #  # ]:           0 :         if (unlikely(err == -1)) {
     526                 :           0 :         err = errno;
     527                 :           0 :         WARN(NULL, "failed to set signal mask: %s\n", strerror(err));
     528                 :           0 :         goto out;
     529                 :             :         }
     530                 :             : 
     531                 :           0 :     sigact.sa_sigaction = &tapback_signal_handler;
     532                 :           0 :     sigact.sa_flags = SA_SIGINFO | SA_NOCLDSTOP | SA_NOCLDWAIT;
     533                 :             : 
     534                 :           0 :     err = sigemptyset(&sigact.sa_mask);
     535         [ #  # ]:           0 :     if (unlikely(err == -1)) {
     536                 :           0 :         err = errno;
     537                 :           0 :         WARN(NULL, "failed to fill empty signal set in sa_mask: %s\n",
     538                 :             :                     strerror(err));
     539                 :             :     }
     540                 :             : 
     541         [ #  # ]:           0 :     for (i = 0; i < ARRAY_SIZE(signals); i++) {
     542                 :           0 :         err = sigaction(signals[i], &sigact, NULL);
     543         [ #  # ]:           0 :         if (unlikely(err == -1)) {
     544                 :           0 :             err = errno;
     545                 :           0 :             WARN(NULL, "failed to register signal %d: %s\n",
     546                 :             :                     signals[i], strerror(err));
     547                 :           0 :             goto out;
     548                 :             :         }
     549                 :             :     }
     550                 :           0 : out:
     551                 :           0 :     return -err;
     552                 :             : }
     553                 :             : 
     554                 :           0 : int main(int argc, char **argv)
     555                 :             : {
     556                 :           0 :     const char *prog = NULL;
     557                 :           0 :     char *opt_name = "vbd3", *opt_pidfile = NULL, *end = NULL;
     558                 :           0 :     bool opt_debug = false, opt_verbose = false;
     559                 :           0 :     int err = 0;
     560                 :           0 :         backend_t *backend = NULL;
     561                 :           0 :     domid_t opt_domid = 0;
     562                 :           0 :         bool opt_barrier = true;
     563                 :             : 
     564         [ #  # ]:           0 :         if (access("/dev/xen/gntdev", F_OK ) == -1) {
     565                 :           0 :                 WARN(NULL, "grant device does not exist\n");
     566                 :           0 :                 err = EINVAL;
     567                 :           0 :                 goto fail;
     568                 :             :         }
     569                 :             : 
     570                 :           0 :     prog = basename(argv[0]);
     571                 :             : 
     572                 :           0 :     opt_debug = 0;
     573                 :             : 
     574                 :           0 :     do {
     575                 :           0 :         const struct option longopts[] = {
     576                 :             :             {"help", 0, NULL, 'h'},
     577                 :             :             {"debug", 0, NULL, 'd'},
     578                 :             :             {"verbose", 0, NULL, 'v'},
     579                 :             :             {"name", 0, NULL, 'n'},
     580                 :             :             {"pidfile", 0, NULL, 'p'},
     581                 :             :             {"domain", 0, NULL, 'x'},
     582                 :             :                         {"nobarrier", 0, NULL, 'b'},
     583                 :             : 
     584                 :             :         };
     585                 :           0 :         int c;
     586                 :             : 
     587                 :           0 :         c = getopt_long(argc, argv, "hdvn:p:x:b", longopts, NULL);
     588         [ #  # ]:           0 :         if (c < 0)
     589                 :             :             break;
     590                 :             : 
     591   [ #  #  #  #  :           0 :         switch (c) {
             #  #  #  #  
                      # ]
     592                 :           0 :         case 'h':
     593                 :           0 :             usage(stdout, prog);
     594                 :           0 :             return 0;
     595                 :           0 :         case 'd':
     596                 :           0 :             opt_debug = true;
     597                 :           0 :             break;
     598                 :           0 :         case 'v':
     599                 :           0 :             opt_verbose = true;
     600                 :           0 :             break;
     601                 :           0 :         case 'n':
     602                 :           0 :             opt_name = strdup(optarg);
     603         [ #  # ]:           0 :             if (!opt_name) {
     604                 :           0 :                 err = errno;
     605                 :           0 :                 goto fail;
     606                 :             :             }
     607                 :             :             break;
     608                 :           0 :         case 'p':
     609                 :           0 :             opt_pidfile = strdup(optarg);
     610         [ #  # ]:           0 :             if (!opt_pidfile) {
     611                 :           0 :                 err = errno;
     612                 :           0 :                 goto fail;
     613                 :             :             }
     614                 :             :             break;
     615                 :           0 :         case 'x':
     616                 :           0 :             opt_domid = strtoul(optarg, &end, 0);
     617   [ #  #  #  # ]:           0 :             if (*end != 0 || end == optarg) {
     618                 :           0 :                 WARN(NULL, "invalid domain ID %s\n", optarg);
     619                 :           0 :                 err = EINVAL;
     620                 :           0 :                 goto fail;
     621                 :             :             }
     622                 :           0 :             INFO(NULL, "only serving domain %d\n", opt_domid);
     623                 :             :             break;
     624                 :           0 :                 case 'b':
     625                 :           0 :                         opt_barrier = false;
     626                 :           0 :                         break;
     627                 :           0 :         case '?':
     628                 :           0 :             goto usage;
     629                 :             :         }
     630                 :             :     } while (1);
     631                 :             : 
     632         [ #  # ]:           0 :     if (!opt_debug) {
     633         [ #  # ]:           0 :         if ((err = daemon(0, 0))) {
     634                 :           0 :             err = -errno;
     635                 :           0 :             goto fail;
     636                 :             :         }
     637                 :             :     }
     638                 :             : 
     639                 :           0 :     openlog(tapback_name, LOG_PID, LOG_DAEMON);
     640         [ #  # ]:           0 :     if (opt_verbose)
     641                 :           0 :         log_level = LOG_DEBUG;
     642                 :             :     else
     643                 :           0 :         log_level = LOG_INFO;
     644                 :           0 :     setlogmask(LOG_UPTO(log_level));
     645                 :             : 
     646         [ #  # ]:           0 :     if (!opt_debug) {
     647         [ #  # ]:           0 :         if ((err = daemon(0, 0))) {
     648                 :           0 :             err = -errno;
     649                 :           0 :             goto fail;
     650                 :             :         }
     651                 :             :     }
     652                 :             : 
     653                 :           0 :     err = tapback_install_sighdl();
     654         [ #  # ]:           0 :     if (unlikely(err))
     655                 :             :     {
     656                 :           0 :         WARN(NULL, "failed to set up signal handling: %s\n", strerror(-err));
     657                 :           0 :         goto fail;
     658                 :             :     }
     659                 :             : 
     660                 :           0 :         backend = tapback_backend_create(opt_name, opt_pidfile, opt_domid,
     661                 :             :                         opt_barrier);
     662         [ #  # ]:           0 :         if (!backend) {
     663                 :           0 :                 err = errno;
     664                 :           0 :         WARN(NULL, "error creating back-end: %s\n", strerror(err));
     665                 :           0 :         goto fail;
     666                 :             :     }
     667                 :             : 
     668                 :           0 :     err = tapback_backend_run(backend);
     669                 :             : 
     670                 :           0 :     tapback_backend_destroy(backend);
     671                 :             : 
     672                 :           0 : fail:
     673                 :           0 :     return err ? -err : 0;
     674                 :             : 
     675                 :           0 : usage:
     676                 :           0 :     usage(stderr, prog);
     677                 :             :     return 1;
     678                 :             : }
     679                 :             : 
     680                 :             : /**
     681                 :             :  * Returns the current domain ID or -errno.
     682                 :             :  */
     683                 :             : int
     684                 :           0 : get_my_domid(struct xs_handle * const xs, xs_transaction_t xst)
     685                 :             : {
     686                 :           0 :         char *buf = NULL, *end = NULL;
     687                 :           0 :         int domid;
     688                 :             : 
     689                 :           0 :         buf = tapback_xs_read(xs, xst, "domid");
     690         [ #  # ]:           0 :         if (!buf) {
     691                 :           0 :                 domid = -errno;
     692                 :           0 :                 goto out;
     693                 :             :         }
     694                 :             : 
     695                 :           0 :         domid = strtoul(buf, &end, 0);
     696   [ #  #  #  # ]:           0 :         if (*end != 0 || end == buf) {
     697                 :           0 :                 domid = -EINVAL;
     698                 :             :         }
     699                 :           0 : out:
     700                 :           0 :         free(buf);
     701         [ #  # ]:           0 :         if (domid >= 0)
     702         [ #  # ]:           0 :                 ASSERT(domid <= UINT16_MAX);
     703                 :           0 :         return domid;
     704                 :             : }
     705                 :             : 
     706                 :             : bool
     707                 :           0 : tapback_is_master(const backend_t *backend)
     708                 :             : {
     709   [ #  #  #  #  :           0 :     if (backend->slave_domid == 0)
             #  #  #  # ]
     710                 :             :         return true;
     711                 :             :     else
     712         [ #  # ]:           0 :         return false;
     713                 :             : }
     714                 :             : 
     715                 :             : int
     716                 :           0 : compare(const void *pa, const void *pb)
     717                 :             : {
     718                 :           0 :     const struct backend_slave *_pa, *_pb;
     719                 :             : 
     720         [ #  # ]:           0 :     ASSERT(pa);
     721         [ #  # ]:           0 :     ASSERT(pb);
     722                 :             : 
     723                 :           0 :     _pa = pa;
     724                 :           0 :     _pb = pb;
     725                 :             : 
     726                 :           0 :     return _pa->master.domid - _pb->master.domid;
     727                 :             : }
        

Generated by: LCOV version 2.0-1