Home | History | Annotate | Download | only in lib
      1 /*
      2  * Copyright (c) 2017 Cyril Hrubis <chrubis (at) suse.cz>
      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 as published by
      6  * the Free Software Foundation, either version 2 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 #define _GNU_SOURCE
     19 #include <unistd.h>
     20 #include <errno.h>
     21 #include "config.h"
     22 #ifdef HAVE_SYS_FANOTIFY_H
     23 # include <sys/fanotify.h>
     24 #endif
     25 #define TST_NO_DEFAULT_MAIN
     26 #include "tst_test.h"
     27 #include "tst_safe_macros.h"
     28 #include "lapi/personality.h"
     29 
     30 int safe_setpgid(const char *file, const int lineno, pid_t pid, pid_t pgid)
     31 {
     32 	int rval;
     33 
     34 	rval = setpgid(pid, pgid);
     35 	if (rval) {
     36 		tst_brk(TBROK | TERRNO,
     37 		        "%s:%d: setpgid(%i, %i) failed",
     38 			file, lineno, pid, pgid);
     39 	}
     40 
     41 	return rval;
     42 }
     43 
     44 pid_t safe_getpgid(const char *file, const int lineno, pid_t pid)
     45 {
     46 	pid_t pgid;
     47 
     48 	pgid = getpgid(pid);
     49 	if (pgid == -1) {
     50 		tst_brk(TBROK | TERRNO,
     51 			"%s:%d: getpgid(%i) failed", file, lineno, pid);
     52 	}
     53 
     54 	return pgid;
     55 }
     56 
     57 int safe_fanotify_init(const char *file, const int lineno,
     58 	unsigned int flags, unsigned int event_f_flags)
     59 {
     60 	int rval;
     61 
     62 #ifdef HAVE_SYS_FANOTIFY_H
     63 	rval = fanotify_init(flags, event_f_flags);
     64 
     65 	if (rval == -1) {
     66 		if (errno == ENOSYS) {
     67 			tst_brk(TCONF,
     68 				"fanotify is not configured in this kernel.");
     69 		}
     70 		tst_brk(TBROK | TERRNO,
     71 			"%s:%d: fanotify_init() failed", file, lineno);
     72 	}
     73 #else
     74 	tst_brk(TCONF, "Header <sys/fanotify.h> is not present");
     75 #endif /* HAVE_SYS_FANOTIFY_H */
     76 
     77 	return rval;
     78 }
     79 
     80 int safe_personality(const char *filename, unsigned int lineno,
     81 		    unsigned long persona)
     82 {
     83 	int prev_persona = personality(persona);
     84 
     85 	if (prev_persona < 0) {
     86 		tst_brk_(filename, lineno, TBROK | TERRNO,
     87 			 "persona(%ld) failed", persona);
     88 	}
     89 
     90 	return prev_persona;
     91 }
     92 
     93 int safe_setregid(const char *file, const int lineno,
     94 		  gid_t rgid, gid_t egid)
     95 {
     96 	int rval;
     97 
     98 	rval = setregid(rgid, egid);
     99 	if (rval == -1) {
    100 		tst_brk_(file, lineno, TBROK | TERRNO,
    101 			 "setregid(%li, %li) failed",
    102 			 (long)rgid, (long)egid);
    103 	}
    104 
    105 	return rval;
    106 }
    107 
    108 
    109 int safe_setreuid(const char *file, const int lineno,
    110 		  uid_t ruid, uid_t euid)
    111 {
    112 	int rval;
    113 
    114 	rval = setreuid(ruid, euid);
    115 	if (rval == -1) {
    116 		tst_brk_(file, lineno, TBROK | TERRNO,
    117 			 "setreuid(%li, %li) failed",
    118 			 (long)ruid, (long)euid);
    119 	}
    120 
    121 	return rval;
    122 }
    123 
    124 
    125 int safe_sigaction(const char *file, const int lineno,
    126                    int signum, const struct sigaction *act,
    127                    struct sigaction *oldact)
    128 {
    129 	int rval;
    130 
    131 	rval = sigaction(signum, act, oldact);
    132 
    133 	if (rval == -1) {
    134 		tst_brk_(file, lineno, TBROK | TERRNO,
    135 			"sigaction(%s (%d), %p, %p) failed",
    136 			tst_strsig(signum), signum, act, oldact);
    137 	}
    138 
    139 	return rval;
    140 }
    141 
    142 struct group *safe_getgrnam(const char *file, const int lineno,
    143 			    const char *name)
    144 {
    145 	struct group *rval;
    146 
    147 	errno = 0;
    148 	rval = getgrnam(name);
    149 	if (rval == NULL) {
    150 		tst_brk_(file, lineno, TBROK | TERRNO,
    151 			"getgrnam(%s) failed", name);
    152 	}
    153 
    154 	return rval;
    155 }
    156 
    157 struct group *safe_getgrnam_fallback(const char *file, const int lineno,
    158 				     const char *name, const char *fallback)
    159 {
    160 	struct group *rval;
    161 
    162 	errno = 0;
    163 	rval = getgrnam(name);
    164 	if (rval == NULL) {
    165 		tst_res_(file, lineno, TINFO,
    166 			 "getgrnam(%s) failed - try fallback %s",
    167 			 name, fallback);
    168 		rval = safe_getgrnam(file, lineno, fallback);
    169 	}
    170 
    171 	return rval;
    172 }
    173 
    174 struct group *safe_getgrgid(const char *file, const int lineno, gid_t gid)
    175 {
    176 	struct group *rval;
    177 
    178 	errno = 0;
    179 	rval = getgrgid(gid);
    180 	if (rval == NULL) {
    181 		tst_brk_(file, lineno, TBROK | TERRNO,
    182 			"getgrgid(%li) failed", (long)gid);
    183 	}
    184 
    185 	return rval;
    186 }
    187 
    188 int safe_chroot(const char *file, const int lineno, const char *path)
    189 {
    190 	int rval;
    191 
    192 	rval = chroot(path);
    193 	if (rval == -1) {
    194 		tst_brk_(file, lineno, TBROK | TERRNO,
    195 			 "chroot(%s) failed", path);
    196 	}
    197 
    198 	return rval;
    199 }
    200