18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: LGPL-2.1
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * trace/beauty/fcntl.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2017, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "trace/beauty/beauty.h"
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <uapi/linux/fcntl.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic size_t fcntl__scnprintf_getfd(unsigned long val, char *bf, size_t size, bool show_prefix)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	return val ? scnprintf(bf, size, "%s", "0") :
158c2ecf20Sopenharmony_ci		     scnprintf(bf, size, "%s%s", show_prefix ? "FD_" : "", "CLOEXEC");
168c2ecf20Sopenharmony_ci}
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistatic size_t syscall_arg__scnprintf_fcntl_getfd(char *bf, size_t size, struct syscall_arg *arg)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	return fcntl__scnprintf_getfd(arg->val, bf, size, arg->show_string_prefix);
218c2ecf20Sopenharmony_ci}
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic size_t fcntl__scnprintf_getlease(unsigned long val, char *bf, size_t size, bool show_prefix)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	static const char *fcntl_setlease[] = { "RDLCK", "WRLCK", "UNLCK", };
268c2ecf20Sopenharmony_ci	static DEFINE_STRARRAY(fcntl_setlease, "F_");
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	return strarray__scnprintf(&strarray__fcntl_setlease, bf, size, "%x", show_prefix, val);
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic size_t syscall_arg__scnprintf_fcntl_getlease(char *bf, size_t size, struct syscall_arg *arg)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	return fcntl__scnprintf_getlease(arg->val, bf, size, arg->show_string_prefix);
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cisize_t syscall_arg__scnprintf_fcntl_cmd(char *bf, size_t size, struct syscall_arg *arg)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	if (arg->val == F_GETFL) {
398c2ecf20Sopenharmony_ci		syscall_arg__set_ret_scnprintf(arg, syscall_arg__scnprintf_open_flags);
408c2ecf20Sopenharmony_ci		goto mask_arg;
418c2ecf20Sopenharmony_ci	}
428c2ecf20Sopenharmony_ci	if (arg->val == F_GETFD) {
438c2ecf20Sopenharmony_ci		syscall_arg__set_ret_scnprintf(arg, syscall_arg__scnprintf_fcntl_getfd);
448c2ecf20Sopenharmony_ci		goto mask_arg;
458c2ecf20Sopenharmony_ci	}
468c2ecf20Sopenharmony_ci	if (arg->val == F_DUPFD_CLOEXEC || arg->val == F_DUPFD) {
478c2ecf20Sopenharmony_ci		syscall_arg__set_ret_scnprintf(arg, syscall_arg__scnprintf_fd);
488c2ecf20Sopenharmony_ci		goto out;
498c2ecf20Sopenharmony_ci	}
508c2ecf20Sopenharmony_ci	if (arg->val == F_GETOWN) {
518c2ecf20Sopenharmony_ci		syscall_arg__set_ret_scnprintf(arg, syscall_arg__scnprintf_pid);
528c2ecf20Sopenharmony_ci		goto mask_arg;
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci	if (arg->val == F_GETLEASE) {
558c2ecf20Sopenharmony_ci		syscall_arg__set_ret_scnprintf(arg, syscall_arg__scnprintf_fcntl_getlease);
568c2ecf20Sopenharmony_ci		goto mask_arg;
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci	/*
598c2ecf20Sopenharmony_ci	 * Some commands ignore the third fcntl argument, "arg", so mask it
608c2ecf20Sopenharmony_ci	 */
618c2ecf20Sopenharmony_ci	if (arg->val == F_GET_SEALS ||
628c2ecf20Sopenharmony_ci	    arg->val == F_GETSIG) {
638c2ecf20Sopenharmony_cimask_arg:
648c2ecf20Sopenharmony_ci		arg->mask |= (1 << 2);
658c2ecf20Sopenharmony_ci	}
668c2ecf20Sopenharmony_ciout:
678c2ecf20Sopenharmony_ci	return syscall_arg__scnprintf_strarrays(bf, size, arg);
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cisize_t syscall_arg__scnprintf_fcntl_arg(char *bf, size_t size, struct syscall_arg *arg)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	bool show_prefix = arg->show_string_prefix;
738c2ecf20Sopenharmony_ci	int cmd = syscall_arg__val(arg, 1);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	if (cmd == F_DUPFD)
768c2ecf20Sopenharmony_ci		return syscall_arg__scnprintf_fd(bf, size, arg);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (cmd == F_SETFD)
798c2ecf20Sopenharmony_ci		return fcntl__scnprintf_getfd(arg->val, bf, size, show_prefix);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	if (cmd == F_SETFL)
828c2ecf20Sopenharmony_ci		return open__scnprintf_flags(arg->val, bf, size, show_prefix);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	if (cmd == F_SETOWN)
858c2ecf20Sopenharmony_ci		return syscall_arg__scnprintf_pid(bf, size, arg);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	if (cmd == F_SETLEASE)
888c2ecf20Sopenharmony_ci		return fcntl__scnprintf_getlease(arg->val, bf, size, show_prefix);
898c2ecf20Sopenharmony_ci	/*
908c2ecf20Sopenharmony_ci	 * We still don't grab the contents of pointers on entry or exit,
918c2ecf20Sopenharmony_ci	 * so just print them as hex numbers
928c2ecf20Sopenharmony_ci	 */
938c2ecf20Sopenharmony_ci	if (cmd == F_SETLK || cmd == F_SETLKW || cmd == F_GETLK ||
948c2ecf20Sopenharmony_ci	    cmd == F_OFD_SETLK || cmd == F_OFD_SETLKW || cmd == F_OFD_GETLK ||
958c2ecf20Sopenharmony_ci	    cmd == F_GETOWN_EX || cmd == F_SETOWN_EX ||
968c2ecf20Sopenharmony_ci	    cmd == F_GET_RW_HINT || cmd == F_SET_RW_HINT ||
978c2ecf20Sopenharmony_ci	    cmd == F_GET_FILE_RW_HINT || cmd == F_SET_FILE_RW_HINT)
988c2ecf20Sopenharmony_ci		return syscall_arg__scnprintf_hex(bf, size, arg);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	return syscall_arg__scnprintf_long(bf, size, arg);
1018c2ecf20Sopenharmony_ci}
102