1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2015 Joyent, Inc.
  14  */
  15 
  16 /*
  17  * Lookup functions for various file paths used by ipmgmtd.  This mechanism
  18  * primarily exists to account for a native root prefix when run within a
  19  * branded zone (e.g. "/native").
  20  */
  21 
  22 #include <stdio.h>
  23 #include <zone.h>
  24 #include "ipmgmt_impl.h"
  25 
  26 #define IPADM_PERM_DIR          "/etc/ipadm"
  27 #define IPADM_TMPFS_DIR         "/etc/svc/volatile/ipadm"
  28 
  29 typedef struct ipadm_path_ent {
  30         ipadm_path_t ipe_id;
  31         const char *ipe_path;
  32 } ipadm_path_ent_t;
  33 
  34 static ipadm_path_ent_t ipadm_paths[] = {
  35         /*
  36          * A temporary directory created in the SMF volatile filesystem.
  37          */
  38         { IPADM_PATH_TMPFS_DIR,         IPADM_TMPFS_DIR },
  39 
  40         /*
  41          * This file captures the in-memory copy of list `aobjmap' on disk.
  42          * This allows the system to recover in the event that the daemon
  43          * crashes or is restarted.
  44          */
  45         { IPADM_PATH_ADDROBJ_MAP_DB,    IPADM_TMPFS_DIR "/aobjmap.conf" },
  46 
  47         /*
  48          * The permanent data store for ipadm.
  49          */
  50         { IPADM_PATH_DB,                IPADM_PERM_DIR "/ipadm.conf" },
  51 
  52         /*
  53          * A temporary copy of the ipadm configuration created, if needed, to
  54          * service write requests early in boot.  This file is merged with the
  55          * permanent data store once it is available for writes.
  56          */
  57         { IPADM_PATH_VOL_DB,            IPADM_TMPFS_DIR "/ipadm.conf" },
  58 
  59         { 0,                            NULL }
  60 };
  61 
  62 /*
  63  * Load one of the paths used by ipadm into the provided string buffer.
  64  * Prepends the native system prefix (e.g. "/native") if one is in effect,
  65  * such as when running within a branded zone.
  66  */
  67 void
  68 ipmgmt_path(ipadm_path_t ip, char *buf, size_t bufsz)
  69 {
  70         int i;
  71 
  72         for (i = 0; ipadm_paths[i].ipe_path != NULL; i++) {
  73                 if (ipadm_paths[i].ipe_id == ip) {
  74                         const char *zroot = zone_get_nroot();
  75 
  76                         (void) snprintf(buf, bufsz, "%s%s", zroot != NULL ?
  77                             zroot : "", ipadm_paths[i].ipe_path);
  78 
  79                         return;
  80                 }
  81         }
  82 
  83         abort();
  84 }