summaryrefslogtreecommitdiff
path: root/tools/sys_cap.h
blob: 2f1071dd84c4a491705fd93cb89f2e404e475646 (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
/*
 * sys_cap.h:
 *
 * See the main source file 'xineliboutput.c' for copyright information and
 * how to reach the author.
 *
 * $Id: sys_cap.h,v 1.3 2011-04-09 12:50:16 phintuka Exp $
 *
 */

#ifndef _XINELIBOUTPUT_SYS_CAP_H_
#define _XINELIBOUTPUT_SYS_CAP_H_

#include "../features.h"

#include <unistd.h>
#include <sys/types.h>
#ifdef HAVE_LIBCAP
# include <sys/capability.h>
#endif

#ifdef HAVE_LIBCAP
static inline cap_flag_value_t get_cap_flag(cap_value_t cap)
{
  cap_flag_value_t val  = CAP_CLEAR;
  cap_t            caps = cap_get_proc();

  if (!caps) {
    LOGDBG("cap_get_proc() failed");
    return val;
  }

  if (cap_get_flag(caps, cap, CAP_EFFECTIVE, &val)) {
    LOGDBG("cap_get_flag(%d) failed", cap);
  }

  cap_free(caps);

  return val;
}
#endif

static inline int have_cap_sys_nice(void)
{
#ifdef HAVE_LIBCAP
  if (get_cap_flag(CAP_SYS_NICE) == CAP_SET) {
    LOGDBG("Have CAP_SYS_NICE capability");
    return 1;
  }
  LOGDBG("No CAP_SYS_NICE capability");
#endif

  return geteuid() == 0;
}

#endif // _XINELIBOUTPUT_SYS_CAP_H_