1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2011, Joyent, Inc. All rights reserved.
  25  */
  26 
  27 #include <sys/types.h>
  28 #include <sys/stat.h>
  29 #include <sys/wait.h>
  30 #include <fcntl.h>
  31 #include <errno.h>
  32 #include <signal.h>
  33 #include <stdio.h>
  34 #include <stdlib.h>
  35 #include <strings.h>
  36 #include <unistd.h>
  37 #include <libscf.h>
  38 #include <libscf_priv.h>
  39 #include <libintl.h>
  40 #include <locale.h>
  41 #include <zone.h>
  42 #include <libzonecfg.h>
  43 
  44 #include "utils.h"
  45 #include "rcapd.h"
  46 #include "rcapd_conf.h"
  47 #include "rcapd_stat.h"
  48 
  49 static void
  50 usage()
  51 {
  52         (void) fprintf(stderr,
  53             gettext("usage: rcapadm\n"
  54             "               [-E|-D]                                "
  55             "# enable/disable rcapd\n"
  56             "               [-n]                                   "
  57             "# don't start/stop rcapd\n"
  58             "               [-i <scan|sample|report|config>=value] "
  59             "# set intervals\n"
  60             "               [-c <percent>]                         "
  61             "# set memory cap\n"
  62             "                                                      "
  63             "# enforcement threshold\n"
  64             "               [-z <zonename> -m <max-rss>]               "
  65             "# update zone memory cap\n"));
  66         exit(E_USAGE);
  67 }
  68 
  69 static rcfg_t conf;
  70 static int enable = -1;
  71 static int disable = -1;
  72 static int pressure = -1;
  73 static int no_starting_stopping = -1;
  74 static int scan_interval = -1;
  75 static int report_interval = -1;
  76 static int config_interval = -1;
  77 static int sample_interval = -1;
  78 
  79 static char *subopt_v[] = {
  80         "scan",
  81         "sample",
  82         "report",
  83         "config",
  84         NULL
  85 };
  86 
  87 typedef enum {
  88         OPT_SCAN = 0,
  89         OPT_SAMPLE,
  90         OPT_REPORT,
  91         OPT_CONFIG
  92 } subopt_idx_t;
  93 
  94 static void
  95 print_state(void)
  96 {
  97         scf_simple_prop_t *persistent_prop = NULL;
  98         scf_simple_prop_t *temporary_prop = NULL;
  99         uint8_t *persistent = NULL;
 100         uint8_t *temporary = NULL;
 101         scf_handle_t *h;
 102         /* LINTED: conditionally assigned and used in function */
 103         ssize_t numvals;
 104 
 105         if ((h = scf_handle_create(SCF_VERSION)) == NULL ||
 106             scf_handle_bind(h) != 0)
 107                 goto out;
 108 
 109         if ((persistent_prop = scf_simple_prop_get(h, RCAP_FMRI,
 110             SCF_PG_GENERAL, SCF_PROPERTY_ENABLED)) != NULL && (numvals =
 111             scf_simple_prop_numvalues(persistent_prop)) > 0)
 112                 persistent = scf_simple_prop_next_boolean(persistent_prop);
 113 
 114         if ((temporary_prop = scf_simple_prop_get(h, RCAP_FMRI,
 115             SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED)) != NULL && (numvals =
 116             scf_simple_prop_numvalues(temporary_prop)) > 0)
 117                 temporary = scf_simple_prop_next_boolean(temporary_prop);
 118 
 119 out:
 120         if (!persistent)
 121                 (void) printf(gettext("                                      "
 122                     "state: unknown"));
 123         else if (temporary && *temporary != *persistent)
 124                 (void) printf(gettext("                                      "
 125                     "state: %s (%s at next boot)\n"), *temporary ?
 126                     gettext("enabled") : gettext("disabled"), *persistent ?
 127                     gettext("enabled") : gettext("disabled"));
 128         else
 129                 (void) printf(gettext("                                      "
 130                     "state: %s\n"), *persistent ? gettext("enabled") :
 131                     gettext("disabled"));
 132 
 133         (void) printf(gettext("           memory cap enforcement"
 134             " threshold: %d%%\n"), conf.rcfg_memory_cap_enforcement_pressure);
 135         (void) printf(gettext("                    process scan rate"
 136             " (sec): %d\n"), conf.rcfg_proc_walk_interval);
 137         (void) printf(gettext("                 reconfiguration rate"
 138             " (sec): %d\n"), conf.rcfg_reconfiguration_interval);
 139         (void) printf(gettext("                          report rate"
 140             " (sec): %d\n"), conf.rcfg_report_interval);
 141         (void) printf(gettext("                    RSS sampling rate"
 142             " (sec): %d\n"), conf.rcfg_rss_sample_interval);
 143 
 144         scf_simple_prop_free(temporary_prop);
 145         scf_simple_prop_free(persistent_prop);
 146         scf_handle_destroy(h);
 147 }
 148 
 149 static int
 150 set_zone_cap(char *zonename, uint64_t mcap)
 151 {
 152         char cmd[128 + ZONENAME_MAX];
 153 
 154         (void) snprintf(cmd, sizeof (cmd), "/usr/bin/prctl -r "
 155             "-n zone.max-physical-memory -v %llu -i zone %s", mcap, zonename);
 156         return (system(cmd));
 157 }
 158 
 159 /*
 160  * Update the in-kernel memory cap for the specified zone.
 161  */
 162 static int
 163 update_zone_mcap(char *zonename, char *maxrss)
 164 {
 165         uint64_t num;
 166 
 167         if (getzoneid() != GLOBAL_ZONEID || zonecfg_in_alt_root())
 168                 return (E_SUCCESS);
 169 
 170         /* get the running zone from the kernel */
 171         if (getzoneidbyname(zonename) == -1) {
 172                 (void) fprintf(stderr, gettext("zone '%s' must be running\n"),
 173                     zonename);
 174                 return (E_ERROR);
 175         }
 176 
 177         if (zonecfg_str_to_bytes(maxrss, &num) == -1) {
 178                 (void) fprintf(stderr, gettext("invalid max-rss value\n"));
 179                 return (E_ERROR);
 180         }
 181 
 182         if (set_zone_cap(zonename, num) == -1) {
 183                 (void) fprintf(stderr, gettext("could not set memory "
 184                     "cap for zone '%s'\n"), zonename);
 185                 return (E_ERROR);
 186         }
 187 
 188         return (E_SUCCESS);
 189 }
 190 
 191 int
 192 main(int argc, char *argv[])
 193 {
 194         char *subopts, *optval;
 195         int modified = 0;
 196         boolean_t refresh = B_FALSE;
 197         int opt;
 198         char *zonename;
 199         char *maxrss = NULL;
 200 
 201         (void) setpname("rcapadm");
 202         (void) setlocale(LC_ALL, "");
 203         (void) textdomain(TEXT_DOMAIN);
 204 
 205         while ((opt = getopt(argc, argv, "DEc:i:m:nz:")) != EOF) {
 206                 switch (opt) {
 207                 case 'n':
 208                         no_starting_stopping = 1;
 209                         break;
 210                 case 'c':
 211                         if ((pressure = xatoi(optarg)) < 0 ||
 212                             pressure > 100 ||
 213                             errno == EINVAL)
 214                                 usage();
 215                         modified++;
 216                         break;
 217                 case 'E':
 218                         enable = 1;
 219                         disable = 0;
 220                         break;
 221                 case 'D':
 222                         disable = 1;
 223                         enable = 0;
 224                         break;
 225                 case 'i':
 226                         subopts = optarg;
 227                         while (*subopts != '\0') {
 228                                 switch (getsubopt(&subopts, subopt_v,
 229                                     &optval)) {
 230                                         case OPT_SCAN:
 231                                                 if (optval == NULL ||
 232                                                     (scan_interval =
 233                                                     xatoi(optval)) <= 0)
 234                                                         usage();
 235                                                 break;
 236                                         case OPT_SAMPLE:
 237                                                 if (optval == NULL ||
 238                                                     (sample_interval =
 239                                                     xatoi(optval)) <= 0)
 240                                                         usage();
 241                                                 break;
 242                                         case OPT_REPORT:
 243                                                 if (optval == NULL ||
 244                                                     (report_interval =
 245                                                     xatoi(optval)) < 0)
 246                                                         usage();
 247                                                 break;
 248                                         case OPT_CONFIG:
 249                                                 if (optval == NULL ||
 250                                                     (config_interval =
 251                                                     xatoi(optval)) < 0)
 252                                                         usage();
 253                                                 break;
 254                                         default:
 255                                                 usage();
 256                                 }
 257                         }
 258                         modified++;
 259                         break;
 260                 case 'm':
 261                         maxrss = optarg;
 262                         break;
 263                 case 'z':
 264                         refresh = B_TRUE;
 265                         zonename = optarg;
 266                         break;
 267                 default:
 268                         usage();
 269                 }
 270         }
 271 
 272         /* the -z & -m options must be used together */
 273         if (argc > optind || (refresh && maxrss == NULL) ||
 274             (!refresh && maxrss != NULL))
 275                 usage();
 276 
 277         if (refresh && (no_starting_stopping > 0 || modified))
 278                 usage();
 279 
 280         /*
 281          * disable/enable before reading configuration from the repository
 282          * which may fail and prevents the disabling/enabling to complete.
 283          */
 284         if (disable > 0) {
 285                 if (smf_disable_instance(RCAP_FMRI, no_starting_stopping > 0
 286                     ? SMF_AT_NEXT_BOOT : 0) != 0)
 287                         die(gettext("cannot disable service: %s\n"),
 288                             scf_strerror(scf_error()));
 289         }
 290 
 291         if (enable > 0) {
 292                 if (smf_enable_instance(RCAP_FMRI, no_starting_stopping > 0
 293                     ? SMF_AT_NEXT_BOOT : 0) != 0)
 294                         die(gettext("cannot enable service: %s\n"),
 295                             scf_strerror(scf_error()));
 296         }
 297 
 298         if (rcfg_read(&conf, NULL) != E_SUCCESS) {
 299                 /*
 300                  * If instance is enabled, put it in maintenance since we
 301                  * failed to read configuration from the repository or
 302                  * create statistics file.
 303                  */
 304                 if (strcmp(smf_get_state(RCAP_FMRI),
 305                     SCF_STATE_STRING_DISABLED) != 0)
 306                         (void) smf_maintain_instance(RCAP_FMRI, 0);
 307 
 308                 die(gettext("resource caps not configured\n"));
 309         } else {
 310                 /* Done reading configuration */
 311                 if (strcmp(conf.rcfg_mode_name, "project") != 0) {
 312                         warn(gettext("%s mode specification ignored -- using"
 313                             " project mode\n"), conf.rcfg_mode_name);
 314                         conf.rcfg_mode_name = "project";
 315                         conf.rcfg_mode = rctype_project;
 316                 }
 317         }
 318 
 319         if (refresh)
 320                 return (update_zone_mcap(zonename, maxrss));
 321 
 322         if (modified) {
 323                 if (pressure >= 0)
 324                         conf.rcfg_memory_cap_enforcement_pressure = pressure;
 325                 if (config_interval >= 0)
 326                         conf.rcfg_reconfiguration_interval = config_interval;
 327                 if (scan_interval >= 0)
 328                         conf.rcfg_proc_walk_interval = scan_interval;
 329                 if (report_interval >= 0)
 330                         conf.rcfg_report_interval = report_interval;
 331                 if (sample_interval >= 0)
 332                         conf.rcfg_rss_sample_interval = sample_interval;
 333 
 334                 /*
 335                  * Modify configuration with the new parameter(s). The
 336                  * function will exit if it fails.
 337                  */
 338                 if ((modify_config(&conf)) != 0)
 339                         die(gettext("Error updating repository \n"));
 340 
 341                 if (smf_refresh_instance(RCAP_FMRI) != 0)
 342                         die(gettext("cannot refresh service: %s\n"),
 343                             scf_strerror(scf_error()));
 344         }
 345 
 346         /*
 347          * Display current configuration
 348          */
 349         print_state();
 350         return (E_SUCCESS);
 351 }