github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap-confine/snap-confine-args.h (about)

     1  /*
     2   * Copyright (C) 2016 Canonical Ltd
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License version 3 as
     6   * published by the Free Software Foundation.
     7   *
     8   * This program is distributed in the hope that it will be useful,
     9   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11   * GNU General Public License for more details.
    12   *
    13   * You should have received a copy of the GNU General Public License
    14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15   *
    16   */
    17  
    18  #ifndef SC_SNAP_CONFINE_ARGS_H
    19  #define SC_SNAP_CONFINE_ARGS_H
    20  
    21  #include <stdbool.h>
    22  
    23  #include "../libsnap-confine-private/error.h"
    24  
    25  /**
    26   * Error domain for errors related to argument parsing.
    27   **/
    28  #define SC_ARGS_DOMAIN "args"
    29  
    30  enum {
    31  	/**
    32  	 * Error indicating that the command line arguments could not be parsed
    33  	 * correctly and usage message should be displayed to the user.
    34  	 **/
    35  	SC_ARGS_ERR_USAGE = 1,
    36  };
    37  
    38  /**
    39   * Opaque structure describing command-line arguments to snap-confine.
    40   **/
    41  struct sc_args;
    42  
    43  /**
    44   * Parse command line arguments for snap-confine.
    45   *
    46   * Snap confine understands very specific arguments.
    47   *
    48   * The argument vector can begin with "ubuntu-core-launcher" (with an optional
    49   * path) which implies that the first arctual argument (argv[1]) is a copy of
    50   * argv[2] and can be discarded.
    51   *
    52   * The argument vector is scanned, left to right, to look for switches that
    53   * start with the minus sign ('-'). Recognized options are stored and
    54   * memorized. Unrecognized options return an appropriate error object.
    55   *
    56   * Currently only one option is understood, that is "--version". It is simply
    57   * scanned, memorized and discarded. The presence of this switch can be
    58   * retrieved with sc_args_is_version_query().
    59   *
    60   * After all the option switches are scanned it is expected to scan two more
    61   * arguments: the security tag and the name of the executable to run.  An error
    62   * object is returned when those is missing.
    63   *
    64   * Both argc and argv are modified so the caller can look at the first unparsed
    65   * argument at argc[0]. This is only done if argument parsing is successful.
    66   **/
    67  __attribute__((warn_unused_result))
    68  struct sc_args *sc_nonfatal_parse_args(int *argcp, char ***argvp,
    69  				       sc_error ** errorp);
    70  
    71  /**
    72   * Free the object describing command-line arguments to snap-confine.
    73   **/
    74  void sc_args_free(struct sc_args *args);
    75  
    76  /**
    77   * Cleanup an error with sc_args_free()
    78   *
    79   * This function is designed to be used with
    80   * SC_CLEANUP(sc_cleanup_args).
    81   **/
    82  void sc_cleanup_args(struct sc_args **ptr);
    83  
    84  /**
    85   * Check if snap-confine was invoked with the --version switch.
    86   **/
    87  bool sc_args_is_version_query(const struct sc_args *args);
    88  
    89  /**
    90   * Check if snap-confine was invoked with the --classic switch.
    91   **/
    92  bool sc_args_is_classic_confinement(const struct sc_args *args);
    93  
    94  /**
    95   * Get the security tag passed to snap-confine.
    96   *
    97   * The return value may be NULL if snap-confine was invoked with --version. It
    98   * is never NULL otherwise.
    99   *
   100   * The return value must not be freed(). It is bound to the lifetime of
   101   * the argument parser.
   102   **/
   103  const char *sc_args_security_tag(const struct sc_args *args);
   104  
   105  /**
   106   * Get the executable name passed to snap-confine.
   107   *
   108   * The return value may be NULL if snap-confine was invoked with --version. It
   109   * is never NULL otherwise.
   110   *
   111   * The return value must not be freed(). It is bound to the lifetime of
   112   * the argument parser.
   113   **/
   114  const char *sc_args_executable(const struct sc_args *args);
   115  
   116  /**
   117   * Get the name of the base snap to use.
   118   *
   119   * The return value must not be freed(). It is bound to the lifetime of
   120   * the argument parser.
   121   **/
   122  const char *sc_args_base_snap(const struct sc_args *args);
   123  
   124  #endif