summaryrefslogtreecommitdiff
path: root/ipc/kdbus/util.h
blob: 529716669fe717f11173a0900fb5a05f151df631 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Copyright (C) 2013-2015 Kay Sievers
 * Copyright (C) 2013-2015 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
 * Copyright (C) 2013-2015 Daniel Mack <daniel@zonque.org>
 * Copyright (C) 2013-2015 David Herrmann <dh.herrmann@gmail.com>
 * Copyright (C) 2013-2015 Linux Foundation
 * Copyright (C) 2014-2015 Djalal Harouni <tixxdz@opendz.org>
 *
 * kdbus is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 */

#ifndef __KDBUS_UTIL_H
#define __KDBUS_UTIL_H

#include <linux/dcache.h>
#include <linux/ioctl.h>

#include <uapi/linux/kdbus.h>

/* all exported addresses are 64 bit */
#define KDBUS_PTR(addr) ((void __user *)(uintptr_t)(addr))

/* all exported sizes are 64 bit and data aligned to 64 bit */
#define KDBUS_ALIGN8(s) ALIGN((s), 8)
#define KDBUS_IS_ALIGNED8(s) (IS_ALIGNED(s, 8))

/**
 * kdbus_member_set_user - write a structure member to user memory
 * @_s:		Variable to copy from
 * @_b:		Buffer to write to
 * @_t:		Structure type
 * @_m:		Member name in the passed structure
 *
 * Return: the result of copy_to_user()
 */
#define kdbus_member_set_user(_s, _b, _t, _m)				\
({									\
	u64 __user *_sz =						\
		(void __user *)((u8 __user *)(_b) + offsetof(_t, _m));	\
	copy_to_user(_sz, _s, FIELD_SIZEOF(_t, _m));			\
})

/**
 * kdbus_strhash - calculate a hash
 * @str:	String
 *
 * Return: hash value
 */
static inline unsigned int kdbus_strhash(const char *str)
{
	unsigned long hash = init_name_hash();

	while (*str)
		hash = partial_name_hash(*str++, hash);

	return end_name_hash(hash);
}

int kdbus_verify_uid_prefix(const char *name, struct user_namespace *user_ns,
			    kuid_t kuid);
int kdbus_sanitize_attach_flags(u64 flags, u64 *attach_flags);

int kdbus_copy_from_user(void *dest, void __user *user_ptr, size_t size);

struct kvec;

void kdbus_kvec_set(struct kvec *kvec, void *src, size_t len, u64 *total_len);
size_t kdbus_kvec_pad(struct kvec *kvec, u64 *len);

#endif