github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/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   * Return true if debugging is enabled.
    32   *
    33   * This can used to avoid costly computation that is only useful for debugging.
    34   **/
    35  bool sc_is_debug_enabled(void);
    36  
    37  /**
    38   * Return true if re-execution is enabled.
    39   **/
    40  bool sc_is_reexec_enabled(void);
    41  
    42  /**
    43   * sc_identity describes the user performing certain operation.
    44   *
    45   * UID and GID represent user and group accounts numbers and are controlled by
    46   * change_uid and change_gid flags.
    47  **/
    48  typedef struct sc_identity {
    49  	uid_t uid;
    50  	gid_t gid;
    51  	unsigned change_uid:1;
    52  	unsigned change_gid:1;
    53  } sc_identity;
    54  
    55  /**
    56   * Identity of the root group.
    57   *
    58   * The return value is suitable for passing to sc_set_effective_identity. It
    59   * causes the effective group to change to the root group. No change is made to
    60   * effective user identity.
    61   **/
    62  static inline sc_identity sc_root_group_identity(void)
    63  {
    64  	sc_identity id = {
    65  		/* Explicitly set our intent of changing just the GID.
    66  		 * Refactoring of this code must retain this property. */
    67  		.change_uid = 0,
    68  		.change_gid = 1,
    69  		.gid = 0,
    70  	};
    71  	return id;
    72  }
    73  
    74  /**
    75   * Set the effective user and group IDs to given values.
    76   *
    77   * Effective user and group identifiers are applied to the system. The
    78   * current values are returned as another identity that can be restored via
    79   * another call to sc_set_effective_identity.
    80   *
    81   * The fields change_uid and change_gid control if user and group ID is changed.
    82   * The returned old identity has identical values of both use flags.
    83  **/
    84  sc_identity sc_set_effective_identity(sc_identity identity);
    85  
    86  void write_string_to_file(const char *filepath, const char *buf);
    87  
    88  /**
    89   * Safely create a given directory.
    90   *
    91   * NOTE: non-fatal functions don't die on errors. It is the responsibility of
    92   * the caller to call die() or handle the error appropriately.
    93   *
    94   * This function behaves like "mkdir -p" (recursive mkdir) with the exception
    95   * that each directory is carefully created in a way that avoids symlink
    96   * attacks. The preceding directory is kept openat(2) (along with O_DIRECTORY)
    97   * and the next directory is created using mkdirat(2), this sequence continues
    98   * while there are more directories to process.
    99   *
   100   * The function returns -1 in case of any error.
   101   **/
   102  __attribute__((warn_unused_result))
   103  int sc_nonfatal_mkpath(const char *const path, mode_t mode);
   104  #endif