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