github.com/ubuntu-core/snappy@v0.0.0-20210827154228-9e584df982bb/cmd/libsnap-confine-private/utils.h (about)

     1  /*
     2   * Copyright (C) 2015 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  #ifndef CORE_LAUNCHER_UTILS_H
    18  #define CORE_LAUNCHER_UTILS_H
    19  
    20  #include <stdlib.h>
    21  #include <stdbool.h>
    22  
    23  __attribute__((noreturn))
    24      __attribute__((format(printf, 1, 2)))
    25  void die(const char *fmt, ...);
    26  
    27  __attribute__((format(printf, 1, 2)))
    28  void debug(const char *fmt, ...);
    29  
    30  /**
    31   * Get an environment variable and convert it to a boolean.
    32   *
    33   * Supported values are those of parse_bool(), namely "yes", "no" as well as "1"
    34   * and "0". All other values are treated as false and a diagnostic message is
    35   * printed to stderr. If the environment variable is unset, set value to the
    36   * default_value as if the environment variable was set to default_value.
    37   **/
    38  bool getenv_bool(const char *name, bool default_value);
    39  
    40  /**
    41   * Return true if debugging is enabled.
    42   *
    43   * This can used to avoid costly computation that is only useful for debugging.
    44   **/
    45  bool sc_is_debug_enabled(void);
    46  
    47  /**
    48   * Return true if re-execution is enabled.
    49   **/
    50  bool sc_is_reexec_enabled(void);
    51  
    52  /**
    53   * sc_identity describes the user performing certain operation.
    54   *
    55   * UID and GID represent user and group accounts numbers and are controlled by
    56   * change_uid and change_gid flags.
    57  **/
    58  typedef struct sc_identity {
    59  	uid_t uid;
    60  	gid_t gid;
    61  	unsigned change_uid:1;
    62  	unsigned change_gid:1;
    63  } sc_identity;
    64  
    65  /**
    66   * Identity of the root group.
    67   *
    68   * The return value is suitable for passing to sc_set_effective_identity. It
    69   * causes the effective group to change to the root group. No change is made to
    70   * effective user identity.
    71   **/
    72  static inline sc_identity sc_root_group_identity(void)
    73  {
    74  	sc_identity id = {
    75  		/* Explicitly set our intent of changing just the GID.
    76  		 * Refactoring of this code must retain this property. */
    77  		.change_uid = 0,
    78  		.change_gid = 1,
    79  		.gid = 0,
    80  	};
    81  	return id;
    82  }
    83  
    84  /**
    85   * Set the effective user and group IDs to given values.
    86   *
    87   * Effective user and group identifiers are applied to the system. The
    88   * current values are returned as another identity that can be restored via
    89   * another call to sc_set_effective_identity.
    90   *
    91   * The fields change_uid and change_gid control if user and group ID is changed.
    92   * The returned old identity has identical values of both use flags.
    93  **/
    94  sc_identity sc_set_effective_identity(sc_identity identity);
    95  
    96  void write_string_to_file(const char *filepath, const char *buf);
    97  
    98  /**
    99   * Safely create a given directory.
   100   *
   101   * NOTE: non-fatal functions don't die on errors. It is the responsibility of
   102   * the caller to call die() or handle the error appropriately.
   103   *
   104   * This function behaves like "mkdir -p" (recursive mkdir) with the exception
   105   * that each directory is carefully created in a way that avoids symlink
   106   * attacks. The preceding directory is kept openat(2) (along with O_DIRECTORY)
   107   * and the next directory is created using mkdirat(2), this sequence continues
   108   * while there are more directories to process.
   109   *
   110   * The function returns -1 in case of any error.
   111   **/
   112  __attribute__((warn_unused_result))
   113  int sc_nonfatal_mkpath(const char *const path, mode_t mode);
   114  #endif