1*d6b5c4c1SYi Liu /*
2*d6b5c4c1SYi Liu * Copyright (c) 2019, Mellanox Technologies. All rights reserved.
3*d6b5c4c1SYi Liu * Copyright (C) 2023 Intel Corporation.
4*d6b5c4c1SYi Liu *
5*d6b5c4c1SYi Liu * This software is available to you under a choice of one of two
6*d6b5c4c1SYi Liu * licenses. You may choose to be licensed under the terms of the GNU
7*d6b5c4c1SYi Liu * General Public License (GPL) Version 2, available from the file
8*d6b5c4c1SYi Liu * COPYING in the main directory of this source tree, or the
9*d6b5c4c1SYi Liu * OpenIB.org BSD license below:
10*d6b5c4c1SYi Liu *
11*d6b5c4c1SYi Liu * Redistribution and use in source and binary forms, with or
12*d6b5c4c1SYi Liu * without modification, are permitted provided that the following
13*d6b5c4c1SYi Liu * conditions are met:
14*d6b5c4c1SYi Liu *
15*d6b5c4c1SYi Liu * - Redistributions of source code must retain the above
16*d6b5c4c1SYi Liu * copyright notice, this list of conditions and the following
17*d6b5c4c1SYi Liu * disclaimer.
18*d6b5c4c1SYi Liu *
19*d6b5c4c1SYi Liu * - Redistributions in binary form must reproduce the above
20*d6b5c4c1SYi Liu * copyright notice, this list of conditions and the following
21*d6b5c4c1SYi Liu * disclaimer in the documentation and/or other materials
22*d6b5c4c1SYi Liu * provided with the distribution.
23*d6b5c4c1SYi Liu *
24*d6b5c4c1SYi Liu * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25*d6b5c4c1SYi Liu * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26*d6b5c4c1SYi Liu * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27*d6b5c4c1SYi Liu * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28*d6b5c4c1SYi Liu * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29*d6b5c4c1SYi Liu * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30*d6b5c4c1SYi Liu * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31*d6b5c4c1SYi Liu * SOFTWARE.
32*d6b5c4c1SYi Liu *
33*d6b5c4c1SYi Liu * Authors: Yi Liu <yi.l.liu@intel.com>
34*d6b5c4c1SYi Liu *
35*d6b5c4c1SYi Liu * Copied from
36*d6b5c4c1SYi Liu * https://github.com/linux-rdma/rdma-core/blob/master/util/open_cdev.c
37*d6b5c4c1SYi Liu *
38*d6b5c4c1SYi Liu */
39*d6b5c4c1SYi Liu
40*d6b5c4c1SYi Liu #include "qemu/osdep.h"
41*d6b5c4c1SYi Liu #include "qemu/chardev_open.h"
42*d6b5c4c1SYi Liu
open_cdev_internal(const char * path,dev_t cdev)43*d6b5c4c1SYi Liu static int open_cdev_internal(const char *path, dev_t cdev)
44*d6b5c4c1SYi Liu {
45*d6b5c4c1SYi Liu struct stat st;
46*d6b5c4c1SYi Liu int fd;
47*d6b5c4c1SYi Liu
48*d6b5c4c1SYi Liu fd = qemu_open_old(path, O_RDWR);
49*d6b5c4c1SYi Liu if (fd == -1) {
50*d6b5c4c1SYi Liu return -1;
51*d6b5c4c1SYi Liu }
52*d6b5c4c1SYi Liu if (fstat(fd, &st) || !S_ISCHR(st.st_mode) ||
53*d6b5c4c1SYi Liu (cdev != 0 && st.st_rdev != cdev)) {
54*d6b5c4c1SYi Liu close(fd);
55*d6b5c4c1SYi Liu return -1;
56*d6b5c4c1SYi Liu }
57*d6b5c4c1SYi Liu return fd;
58*d6b5c4c1SYi Liu }
59*d6b5c4c1SYi Liu
open_cdev_robust(dev_t cdev)60*d6b5c4c1SYi Liu static int open_cdev_robust(dev_t cdev)
61*d6b5c4c1SYi Liu {
62*d6b5c4c1SYi Liu g_autofree char *devpath = NULL;
63*d6b5c4c1SYi Liu
64*d6b5c4c1SYi Liu /*
65*d6b5c4c1SYi Liu * This assumes that udev is being used and is creating the /dev/char/
66*d6b5c4c1SYi Liu * symlinks.
67*d6b5c4c1SYi Liu */
68*d6b5c4c1SYi Liu devpath = g_strdup_printf("/dev/char/%u:%u", major(cdev), minor(cdev));
69*d6b5c4c1SYi Liu return open_cdev_internal(devpath, cdev);
70*d6b5c4c1SYi Liu }
71*d6b5c4c1SYi Liu
open_cdev(const char * devpath,dev_t cdev)72*d6b5c4c1SYi Liu int open_cdev(const char *devpath, dev_t cdev)
73*d6b5c4c1SYi Liu {
74*d6b5c4c1SYi Liu int fd;
75*d6b5c4c1SYi Liu
76*d6b5c4c1SYi Liu fd = open_cdev_internal(devpath, cdev);
77*d6b5c4c1SYi Liu if (fd == -1 && cdev != 0) {
78*d6b5c4c1SYi Liu return open_cdev_robust(cdev);
79*d6b5c4c1SYi Liu }
80*d6b5c4c1SYi Liu return fd;
81*d6b5c4c1SYi Liu }
82