]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/udev/v4l_id/v4l_id.c
seccomp-util: allowlist open_tree() as part of @file-system
[thirdparty/systemd.git] / src / udev / v4l_id / v4l_id.c
... / ...
CommitLineData
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (c) 2009 Filippo Argiolas <filippo.argiolas@gmail.com>
4 */
5
6#include <fcntl.h>
7#include <getopt.h>
8#include <linux/videodev2.h>
9#include <stdio.h>
10#include <sys/ioctl.h>
11
12#include "build.h"
13#include "errno-util.h"
14#include "fd-util.h"
15#include "log.h"
16#include "main-func.h"
17
18static const char *arg_device = NULL;
19
20static int parse_argv(int argc, char *argv[]) {
21 static const struct option options[] = {
22 { "help", no_argument, NULL, 'h' },
23 { "version", no_argument, NULL, 'v' },
24 {}
25 };
26 int c;
27
28 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
29 switch (c) {
30 case 'h':
31 printf("%s [OPTIONS...] DEVICE\n\n"
32 "Video4Linux device identification.\n\n"
33 " -h --help Show this help text\n"
34 " --version Show package version\n",
35 program_invocation_short_name);
36 return 0;
37 case 'v':
38 return version();
39 case '?':
40 return -EINVAL;
41 default:
42 assert_not_reached();
43 }
44
45 if (!argv[optind])
46 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
47 "DEVICE argument missing.");
48
49 arg_device = argv[optind];
50 return 1;
51}
52
53static int run(int argc, char *argv[]) {
54 _cleanup_close_ int fd = -EBADF;
55 struct v4l2_capability v2cap;
56 int r;
57
58 r = parse_argv(argc, argv);
59 if (r <= 0)
60 return r;
61
62 fd = open(arg_device, O_RDONLY|O_CLOEXEC|O_NOCTTY);
63 if (fd < 0) {
64 bool ignore = ERRNO_IS_DEVICE_ABSENT(errno);
65 log_full_errno(ignore ? LOG_DEBUG : LOG_WARNING, errno,
66 "Failed to open device node '%s'%s: %m",
67 arg_device, ignore ? ", ignoring" : "");
68 return ignore ? 0 : -errno;
69 }
70
71 if (ioctl(fd, VIDIOC_QUERYCAP, &v2cap) == 0) {
72 int capabilities;
73
74 printf("ID_V4L_VERSION=2\n");
75 printf("ID_V4L_PRODUCT=%s\n", v2cap.card);
76 printf("ID_V4L_CAPABILITIES=:");
77
78 if (v2cap.capabilities & V4L2_CAP_DEVICE_CAPS)
79 capabilities = v2cap.device_caps;
80 else
81 capabilities = v2cap.capabilities;
82
83 if ((capabilities & V4L2_CAP_VIDEO_CAPTURE) > 0 ||
84 (capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) > 0)
85 printf("capture:");
86 if ((capabilities & V4L2_CAP_VIDEO_OUTPUT) > 0 ||
87 (capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE) > 0)
88 printf("video_output:");
89 if ((capabilities & V4L2_CAP_VIDEO_OVERLAY) > 0)
90 printf("video_overlay:");
91 if ((capabilities & V4L2_CAP_AUDIO) > 0)
92 printf("audio:");
93 if ((capabilities & V4L2_CAP_TUNER) > 0)
94 printf("tuner:");
95 if ((capabilities & V4L2_CAP_RADIO) > 0)
96 printf("radio:");
97 printf("\n");
98 }
99
100 return 0;
101}
102
103DEFINE_MAIN_FUNCTION(run);