1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/tests/cmknod.c
38 * Simple test to check that device nodes are correctly created and visible
44 #include <sys/types.h>
50 #define TEST_MINOR 120
53 void usage(char *prog)
55 fprintf(stderr, "usage: %s <filename>\n", prog);
59 /* UMKA: This stuff inlined here instead of using appropriate header
60 to avoid linking to symbols which is not present in newer libc.
62 Currently this is the case, as UML image contains RedHat 9 and
63 developers use something newer (Fedora, etc.). */
65 __gnu_dev_major (unsigned long long int __dev)
67 return ((__dev >> 8) & 0xfff) | ((unsigned int) (__dev >> 32) & ~0xfff);
71 __gnu_dev_minor (unsigned long long int __dev)
73 return (__dev & 0xff) | ((unsigned int) (__dev >> 12) & ~0xff);
76 inline unsigned long long int
77 __gnu_dev_makedev (unsigned int __major, unsigned int __minor)
79 return ((__minor & 0xff) | ((__major & 0xfff) << 8)
80 | (((unsigned long long int) (__minor & ~0xff)) << 12)
81 | (((unsigned long long int) (__major & ~0xfff)) << 32));
84 #define __minor(dev) __gnu_dev_minor(dev)
85 #define __major(dev) __gnu_dev_major(dev)
86 #define __makedev(maj, min) __gnu_dev_makedev(maj, min)
88 int main( int argc, char **argv)
91 char *filename = argv[1];
94 dev_t device = __makedev(TEST_MAJOR, TEST_MINOR);
101 /* First try block devices */
102 rc = mknod(filename, 0700 | S_IFBLK, device);
104 fprintf(stderr, "%s: mknod(%s) failed: rc %d: %s\n",
105 prog, filename, errno, strerror(errno));
109 rc = stat(filename, &st);
111 fprintf(stderr, "%s: stat(%s) failed: rc %d: %s\n",
112 prog, filename, errno, strerror(errno));
116 if ( st.st_rdev != device) {
117 fprintf(stderr, "%s: created device other than requested: (%u,%u) instead of (%u,%u)\n",
118 prog, __major(st.st_rdev),__minor(st.st_rdev),__major(device),__minor(device));
121 if (!S_ISBLK(st.st_mode)) {
122 fprintf(stderr, "%s: created device of different type. Requested block device, got mode %o\n", prog, st.st_mode);
126 rc = unlink(filename);
128 fprintf(stderr, "%s: Cannot unlink created device %s, rc %d: %s\n",
129 prog, filename, errno, strerror(errno));
133 /* Second try char devices */
134 rc = mknod(filename, 0700 | S_IFCHR, device);
136 fprintf(stderr, "%s: mknod(%s) failed: rc %d: %s\n",
137 prog, filename, errno, strerror(errno));
141 rc = stat(filename, &st);
143 fprintf(stderr, "%s: stat(%s) failed: rc %d: %s\n",
144 prog, filename, errno, strerror(errno));
147 if ( st.st_rdev != device) {
148 fprintf(stderr, "%s: created device other than requested: (%u,%u) instead of (%u,%u)\n",
149 prog, __major(st.st_rdev),__minor(st.st_rdev),__major(device),__minor(device));
152 if (!S_ISCHR(st.st_mode)) {
153 fprintf(stderr, "%s: created device of different type. Requested char device, got mode %o\n", prog, st.st_mode);
157 rc = unlink(filename);
159 fprintf(stderr, "%s: Cannot unlink created device %s, rc %d: %s\n",
160 prog, filename, errno, strerror(errno));
164 printf("%s: device nodes created correctly\n", prog);