1 /* 2 * Copyright (C) 2012 Linux Test Project, Inc. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of version 2 of the GNU General Public 6 * License as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it would be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11 * 12 * Further, this software is distributed without any warranty that it 13 * is free of the rightful claim of any third person regarding 14 * infringement or the like. Any license provided herein, whether 15 * implied or otherwise, applies only to this software file. Patent 16 * licenses, if any, provided herein do not apply to combinations of 17 * this program with other software, or any other product whatsoever. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 22 * 02110-1301, USA. 23 */ 24 25 #ifndef _PROCESS_VM_H_ 26 #define _PROCESS_VM_H_ 27 28 #include <sys/types.h> 29 #include <sys/ipc.h> 30 #include <sys/sem.h> 31 #include <sys/syscall.h> 32 #include <sys/uio.h> 33 #include <unistd.h> 34 #include "lapi/semun.h" 35 36 static inline ssize_t test_process_vm_readv(pid_t pid, 37 const struct iovec *lvec, unsigned long liovcnt, 38 const struct iovec *rvec, unsigned long riovcnt, 39 unsigned long flags) 40 { 41 #if defined(__NR_process_vm_readv) 42 return syscall(__NR_process_vm_readv, pid, lvec, liovcnt, 43 rvec, riovcnt, flags); 44 #else 45 return syscall(-1); 46 #endif 47 } 48 49 static inline ssize_t test_process_vm_writev(pid_t pid, 50 const struct iovec *lvec, unsigned long liovcnt, 51 const struct iovec *rvec, unsigned long riovcnt, 52 unsigned long flags) 53 { 54 #if defined(__NR_process_vm_writev) 55 return syscall(__NR_process_vm_writev, pid, lvec, liovcnt, 56 rvec, riovcnt, flags); 57 #else 58 return syscall(-1); 59 #endif 60 } 61 62 void safe_semop(int id, unsigned short num, short op) 63 { 64 int ret; 65 struct sembuf sem_op; 66 sem_op.sem_num = num, 67 sem_op.sem_op = op, 68 sem_op.sem_flg = 0; 69 70 do { 71 ret = semop(id, &sem_op, 1); 72 } while (ret == -1 && errno == EINTR); 73 if (ret == -1) 74 tst_brkm(TBROK|TERRNO, NULL, "semop(%d, (%d, %d)) failed", 75 id, num, op); 76 } 77 78 int init_sem(int num) 79 { 80 int id, i; 81 union semun u; 82 if ((id = semget(IPC_PRIVATE, num, IPC_CREAT|S_IRWXU)) == -1) 83 tst_brkm(TBROK|TERRNO, NULL, "Couldn't allocate semaphore"); 84 85 for (i = 0; i < num; i++) { 86 u.val = 0; 87 if (semctl(id, 0, SETVAL, u) == -1) 88 tst_brkm(TBROK|TERRNO, NULL, 89 "Couldn't initialize sem %d value", i); 90 } 91 return id; 92 } 93 94 void clean_sem(int id) 95 { 96 if (semctl(id, 0, IPC_RMID) == -1) 97 tst_brkm(TBROK|TERRNO, NULL, "Couldn't remove sem"); 98 } 99 100 #endif /* _PROCESS_VM_H_ */ 101