1# Copyright 2017 Sasha Goldshtein 2# Copyright 2018 Red Hat, Inc. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16"""syscall.py contains functions useful for mapping between syscall names and numbers""" 17 18import subprocess 19import platform 20 21# 22# Syscall table for Linux x86_64, not very recent. 23# Automatically generated from strace/linux/x86_64/syscallent.h using the 24# following command: 25# 26# cat syscallent.h | awk -F, '{ gsub(/[ \t"}]/, "", $4); 27# gsub(/[\[\] \t{]/, "", $1); split($1, a, "="); 28# print " "a[1]": b\""$4"\","; } 29# BEGIN { print "syscalls = {" } 30# END { print "}" '} 31syscalls = { 32 0: b"read", 33 1: b"write", 34 2: b"open", 35 3: b"close", 36 4: b"stat", 37 5: b"fstat", 38 6: b"lstat", 39 7: b"poll", 40 8: b"lseek", 41 9: b"mmap", 42 10: b"mprotect", 43 11: b"munmap", 44 12: b"brk", 45 13: b"rt_sigaction", 46 14: b"rt_sigprocmask", 47 15: b"rt_sigreturn", 48 16: b"ioctl", 49 17: b"pread64", 50 18: b"pwrite64", 51 19: b"readv", 52 20: b"writev", 53 21: b"access", 54 22: b"pipe", 55 23: b"select", 56 24: b"sched_yield", 57 25: b"mremap", 58 26: b"msync", 59 27: b"mincore", 60 28: b"madvise", 61 29: b"shmget", 62 30: b"shmat", 63 31: b"shmctl", 64 32: b"dup", 65 33: b"dup2", 66 34: b"pause", 67 35: b"nanosleep", 68 36: b"getitimer", 69 37: b"alarm", 70 38: b"setitimer", 71 39: b"getpid", 72 40: b"sendfile", 73 41: b"socket", 74 42: b"connect", 75 43: b"accept", 76 44: b"sendto", 77 45: b"recvfrom", 78 46: b"sendmsg", 79 47: b"recvmsg", 80 48: b"shutdown", 81 49: b"bind", 82 50: b"listen", 83 51: b"getsockname", 84 52: b"getpeername", 85 53: b"socketpair", 86 54: b"setsockopt", 87 55: b"getsockopt", 88 56: b"clone", 89 57: b"fork", 90 58: b"vfork", 91 59: b"execve", 92 60: b"exit", 93 61: b"wait4", 94 62: b"kill", 95 63: b"uname", 96 64: b"semget", 97 65: b"semop", 98 66: b"semctl", 99 67: b"shmdt", 100 68: b"msgget", 101 69: b"msgsnd", 102 70: b"msgrcv", 103 71: b"msgctl", 104 72: b"fcntl", 105 73: b"flock", 106 74: b"fsync", 107 75: b"fdatasync", 108 76: b"truncate", 109 77: b"ftruncate", 110 78: b"getdents", 111 79: b"getcwd", 112 80: b"chdir", 113 81: b"fchdir", 114 82: b"rename", 115 83: b"mkdir", 116 84: b"rmdir", 117 85: b"creat", 118 86: b"link", 119 87: b"unlink", 120 88: b"symlink", 121 89: b"readlink", 122 90: b"chmod", 123 91: b"fchmod", 124 92: b"chown", 125 93: b"fchown", 126 94: b"lchown", 127 95: b"umask", 128 96: b"gettimeofday", 129 97: b"getrlimit", 130 98: b"getrusage", 131 99: b"sysinfo", 132 100: b"times", 133 101: b"ptrace", 134 102: b"getuid", 135 103: b"syslog", 136 104: b"getgid", 137 105: b"setuid", 138 106: b"setgid", 139 107: b"geteuid", 140 108: b"getegid", 141 109: b"setpgid", 142 110: b"getppid", 143 111: b"getpgrp", 144 112: b"setsid", 145 113: b"setreuid", 146 114: b"setregid", 147 115: b"getgroups", 148 116: b"setgroups", 149 117: b"setresuid", 150 118: b"getresuid", 151 119: b"setresgid", 152 120: b"getresgid", 153 121: b"getpgid", 154 122: b"setfsuid", 155 123: b"setfsgid", 156 124: b"getsid", 157 125: b"capget", 158 126: b"capset", 159 127: b"rt_sigpending", 160 128: b"rt_sigtimedwait", 161 129: b"rt_sigqueueinfo", 162 130: b"rt_sigsuspend", 163 131: b"sigaltstack", 164 132: b"utime", 165 133: b"mknod", 166 134: b"uselib", 167 135: b"personality", 168 136: b"ustat", 169 137: b"statfs", 170 138: b"fstatfs", 171 139: b"sysfs", 172 140: b"getpriority", 173 141: b"setpriority", 174 142: b"sched_setparam", 175 143: b"sched_getparam", 176 144: b"sched_setscheduler", 177 145: b"sched_getscheduler", 178 146: b"sched_get_priority_max", 179 147: b"sched_get_priority_min", 180 148: b"sched_rr_get_interval", 181 149: b"mlock", 182 150: b"munlock", 183 151: b"mlockall", 184 152: b"munlockall", 185 153: b"vhangup", 186 154: b"modify_ldt", 187 155: b"pivot_root", 188 156: b"_sysctl", 189 157: b"prctl", 190 158: b"arch_prctl", 191 159: b"adjtimex", 192 160: b"setrlimit", 193 161: b"chroot", 194 162: b"sync", 195 163: b"acct", 196 164: b"settimeofday", 197 165: b"mount", 198 166: b"umount2", 199 167: b"swapon", 200 168: b"swapoff", 201 169: b"reboot", 202 170: b"sethostname", 203 171: b"setdomainname", 204 172: b"iopl", 205 173: b"ioperm", 206 174: b"create_module", 207 175: b"init_module", 208 176: b"delete_module", 209 177: b"get_kernel_syms", 210 178: b"query_module", 211 179: b"quotactl", 212 180: b"nfsservctl", 213 181: b"getpmsg", 214 182: b"putpmsg", 215 183: b"afs_syscall", 216 184: b"tuxcall", 217 185: b"security", 218 186: b"gettid", 219 187: b"readahead", 220 188: b"setxattr", 221 189: b"lsetxattr", 222 190: b"fsetxattr", 223 191: b"getxattr", 224 192: b"lgetxattr", 225 193: b"fgetxattr", 226 194: b"listxattr", 227 195: b"llistxattr", 228 196: b"flistxattr", 229 197: b"removexattr", 230 198: b"lremovexattr", 231 199: b"fremovexattr", 232 200: b"tkill", 233 201: b"time", 234 202: b"futex", 235 203: b"sched_setaffinity", 236 204: b"sched_getaffinity", 237 205: b"set_thread_area", 238 206: b"io_setup", 239 207: b"io_destroy", 240 208: b"io_getevents", 241 209: b"io_submit", 242 210: b"io_cancel", 243 211: b"get_thread_area", 244 212: b"lookup_dcookie", 245 213: b"epoll_create", 246 214: b"epoll_ctl_old", 247 215: b"epoll_wait_old", 248 216: b"remap_file_pages", 249 217: b"getdents64", 250 218: b"set_tid_address", 251 219: b"restart_syscall", 252 220: b"semtimedop", 253 221: b"fadvise64", 254 222: b"timer_create", 255 223: b"timer_settime", 256 224: b"timer_gettime", 257 225: b"timer_getoverrun", 258 226: b"timer_delete", 259 227: b"clock_settime", 260 228: b"clock_gettime", 261 229: b"clock_getres", 262 230: b"clock_nanosleep", 263 231: b"exit_group", 264 232: b"epoll_wait", 265 233: b"epoll_ctl", 266 234: b"tgkill", 267 235: b"utimes", 268 236: b"vserver", 269 237: b"mbind", 270 238: b"set_mempolicy", 271 239: b"get_mempolicy", 272 240: b"mq_open", 273 241: b"mq_unlink", 274 242: b"mq_timedsend", 275 243: b"mq_timedreceive", 276 244: b"mq_notify", 277 245: b"mq_getsetattr", 278 246: b"kexec_load", 279 247: b"waitid", 280 248: b"add_key", 281 249: b"request_key", 282 250: b"keyctl", 283 251: b"ioprio_set", 284 252: b"ioprio_get", 285 253: b"inotify_init", 286 254: b"inotify_add_watch", 287 255: b"inotify_rm_watch", 288 256: b"migrate_pages", 289 257: b"openat", 290 258: b"mkdirat", 291 259: b"mknodat", 292 260: b"fchownat", 293 261: b"futimesat", 294 262: b"newfstatat", 295 263: b"unlinkat", 296 264: b"renameat", 297 265: b"linkat", 298 266: b"symlinkat", 299 267: b"readlinkat", 300 268: b"fchmodat", 301 269: b"faccessat", 302 270: b"pselect6", 303 271: b"ppoll", 304 272: b"unshare", 305 273: b"set_robust_list", 306 274: b"get_robust_list", 307 275: b"splice", 308 276: b"tee", 309 277: b"sync_file_range", 310 278: b"vmsplice", 311 279: b"move_pages", 312 280: b"utimensat", 313 281: b"epoll_pwait", 314 282: b"signalfd", 315 283: b"timerfd_create", 316 284: b"eventfd", 317 285: b"fallocate", 318 286: b"timerfd_settime", 319 287: b"timerfd_gettime", 320 288: b"accept4", 321 289: b"signalfd4", 322 290: b"eventfd2", 323 291: b"epoll_create1", 324 292: b"dup3", 325 293: b"pipe2", 326 294: b"inotify_init1", 327 295: b"preadv", 328 296: b"pwritev", 329 297: b"rt_tgsigqueueinfo", 330 298: b"perf_event_open", 331 299: b"recvmmsg", 332 300: b"fanotify_init", 333 301: b"fanotify_mark", 334 302: b"prlimit64", 335 303: b"name_to_handle_at", 336 304: b"open_by_handle_at", 337 305: b"clock_adjtime", 338 306: b"syncfs", 339 307: b"sendmmsg", 340 308: b"setns", 341 309: b"getcpu", 342 310: b"process_vm_readv", 343 311: b"process_vm_writev", 344 312: b"kcmp", 345 313: b"finit_module", 346 314: b"sched_setattr", 347 315: b"sched_getattr", 348 316: b"renameat2", 349 317: b"seccomp", 350 318: b"getrandom", 351 319: b"memfd_create", 352 320: b"kexec_file_load", 353 321: b"bpf", 354 322: b"execveat", 355 323: b"userfaultfd", 356 324: b"membarrier", 357 325: b"mlock2", 358 326: b"copy_file_range", 359 327: b"preadv2", 360 328: b"pwritev2", 361 329: b"pkey_mprotect", 362 330: b"pkey_alloc", 363 331: b"pkey_free", 364 332: b"statx", 365 333: b"io_pgetevents", 366 334: b"rseq", 367} 368 369# Try to use ausyscall if it is available, because it can give us an up-to-date 370# list of syscalls for various architectures, rather than the x86-64 hardcoded 371# list above. 372def _parse_syscall(line): 373 parts = line.split() 374 return (int(parts[0]), parts[1].strip()) 375 376try: 377 # Skip the first line, which is a header. The rest of the lines are simply 378 # SYSCALL_NUM\tSYSCALL_NAME pairs. 379 out = subprocess.check_output(['ausyscall', '--dump'], stderr=subprocess.STDOUT) 380 # remove the first line of expected output 381 out = out.split(b'\n',1)[1] 382 syscalls = dict(map(_parse_syscall, out.strip().split(b'\n'))) 383except Exception as e: 384 if platform.machine() == "x86_64": 385 pass 386 else: 387 raise Exception("ausyscall: command not found") 388 389def syscall_name(syscall_num): 390 """Return the syscall name for the particular syscall number.""" 391 return syscalls.get(syscall_num, b"[unknown: %d]" % syscall_num) 392