|
Packit Service |
e080da |
/* Copyright (c) 2014 Red Hat, Inc. All rights reserved.
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
* This copyrighted material is made available to anyone wishing
|
|
Packit Service |
e080da |
* to use, modify, copy, or redistribute it subject to the terms
|
|
Packit Service |
e080da |
* and conditions of the GNU General Public License version 2.
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
* This program is distributed in the hope that it will be
|
|
Packit Service |
e080da |
* useful, but WITHOUT ANY WARRANTY; without even the implied
|
|
Packit Service |
e080da |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
Packit Service |
e080da |
* PURPOSE. See the GNU General Public License for more details.
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
* You should have received a copy of the GNU General Public
|
|
Packit Service |
e080da |
* License along with this program; if not, write to the Free
|
|
Packit Service |
e080da |
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
Packit Service |
e080da |
* Boston, MA 02110-1301, USA.
|
|
Packit Service |
e080da |
*/
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
/* Filesystem basic sanity check, tests all (almost) fops. */
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
#include <stdio.h>
|
|
Packit Service |
e080da |
#include <fcntl.h>
|
|
Packit Service |
e080da |
#include <unistd.h>
|
|
Packit Service |
e080da |
#include <sys/types.h>
|
|
Packit Service |
e080da |
#include <sys/stat.h>
|
|
Packit Service |
e080da |
#include <sys/xattr.h>
|
|
Packit Service |
e080da |
#include <errno.h>
|
|
Packit Service |
e080da |
#include <string.h>
|
|
Packit Service |
e080da |
#include <dirent.h>
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
#ifndef linux
|
|
Packit Service |
e080da |
#include <sys/socket.h>
|
|
Packit Service |
e080da |
#include <sys/types.h>
|
|
Packit Service |
e080da |
#include <sys/un.h>
|
|
Packit Service |
e080da |
#endif
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
/* for fd based fops after unlink */
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
fd_based_fops_1(char *filename);
|
|
Packit Service |
e080da |
/* for fd based fops before unlink */
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
fd_based_fops_2(char *filename);
|
|
Packit Service |
e080da |
/* fops based on fd after dup */
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
dup_fd_based_fops(char *filename);
|
|
Packit Service |
e080da |
/* for fops based on path */
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
path_based_fops(char *filename);
|
|
Packit Service |
e080da |
/* for fops which operate on directory */
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
dir_based_fops(char *filename);
|
|
Packit Service |
e080da |
/* for fops which operate in link files (symlinks) */
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
link_based_fops(char *filename);
|
|
Packit Service |
e080da |
/* to test open syscall with open modes available. */
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
test_open_modes(char *filename);
|
|
Packit Service |
e080da |
/* generic function which does open write and read. */
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
generic_open_read_write(char *filename, int flag, mode_t mode);
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
#define OPEN_MODE 0666
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
main(int argc, char *argv[])
|
|
Packit Service |
e080da |
{
|
|
Packit Service |
e080da |
int ret = -1;
|
|
Packit Service |
e080da |
int result = 0;
|
|
Packit Service |
e080da |
char filename[255] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
if (argc > 1)
|
|
Packit Service |
e080da |
strcpy(filename, argv[1]);
|
|
Packit Service |
e080da |
else
|
|
Packit Service |
e080da |
strcpy(filename, "temp-xattr-test-file");
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fd_based_fops_1(strcat(filename, "_1"));
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fd based file operation 1 failed\n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
} else {
|
|
Packit Service |
e080da |
fprintf(stdout, "fd based file operation 1 passed\n");
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fd_based_fops_2(strcat(filename, "_2"));
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
fprintf(stderr, "fd based file operation 2 failed\n");
|
|
Packit Service |
e080da |
} else {
|
|
Packit Service |
e080da |
fprintf(stdout, "fd based file operation 2 passed\n");
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = dup_fd_based_fops(strcat(filename, "_3"));
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
fprintf(stderr, "dup fd based file operation failed\n");
|
|
Packit Service |
e080da |
} else {
|
|
Packit Service |
e080da |
fprintf(stdout, "dup fd based file operation passed\n");
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = path_based_fops(strcat(filename, "_4"));
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
fprintf(stderr, "path based file operation failed\n");
|
|
Packit Service |
e080da |
} else {
|
|
Packit Service |
e080da |
fprintf(stdout, "path based file operation passed\n");
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = dir_based_fops(strcat(filename, "_5"));
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
fprintf(stderr, "directory based file operation failed\n");
|
|
Packit Service |
e080da |
} else {
|
|
Packit Service |
e080da |
fprintf(stdout, "directory based file operation passed\n");
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = link_based_fops(strcat(filename, "_5"));
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
fprintf(stderr, "link based file operation failed\n");
|
|
Packit Service |
e080da |
} else {
|
|
Packit Service |
e080da |
fprintf(stdout, "link based file operation passed\n");
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = test_open_modes(strcat(filename, "_5"));
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
fprintf(stderr, "testing modes of `open' call failed\n");
|
|
Packit Service |
e080da |
} else {
|
|
Packit Service |
e080da |
fprintf(stdout, "testing modes of `open' call passed\n");
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
return result;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
/* Execute all possible fops on a fd which is unlinked */
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
fd_based_fops_1(char *filename)
|
|
Packit Service |
e080da |
{
|
|
Packit Service |
e080da |
int fd = 0;
|
|
Packit Service |
e080da |
int ret = -1;
|
|
Packit Service |
e080da |
int result = 0;
|
|
Packit Service |
e080da |
struct stat stbuf = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
char wstr[50] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
char rstr[50] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
fd = open(filename, O_RDWR | O_CREAT, OPEN_MODE);
|
|
Packit Service |
e080da |
if (fd < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "open failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
return ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = unlink(filename);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "unlink failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
strcpy(wstr, "This is my string\n");
|
|
Packit Service |
e080da |
ret = write(fd, wstr, strlen(wstr));
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "write failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = lseek(fd, 0, SEEK_SET);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "lseek failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = read(fd, rstr, strlen(wstr));
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "read failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = memcmp(rstr, wstr, strlen(wstr));
|
|
Packit Service |
e080da |
if (ret != 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "read returning junk\n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = ftruncate(fd, 0);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "ftruncate failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fstat(fd, &stbuf);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fstat failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fsync(fd);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fsync failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fdatasync(fd);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fdatasync failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
/*
|
|
Packit Service |
e080da |
* These metadata operations fail at the moment because kernel doesn't
|
|
Packit Service |
e080da |
* pass the client fd in the operation.
|
|
Packit Service |
e080da |
* The following bug tracks this change.
|
|
Packit Service |
e080da |
* https://bugzilla.redhat.com/show_bug.cgi?id=1084422
|
|
Packit Service |
e080da |
* ret = fchmod (fd, 0640);
|
|
Packit Service |
e080da |
* if (ret < 0) {
|
|
Packit Service |
e080da |
* fprintf (stderr, "fchmod failed : %s\n", strerror (errno));
|
|
Packit Service |
e080da |
* result |= ret;
|
|
Packit Service |
e080da |
* }
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
* ret = fchown (fd, 10001, 10001);
|
|
Packit Service |
e080da |
* if (ret < 0) {
|
|
Packit Service |
e080da |
* fprintf (stderr, "fchown failed : %s\n", strerror (errno));
|
|
Packit Service |
e080da |
* result |= ret;
|
|
Packit Service |
e080da |
* }
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
* ret = fsetxattr (fd, "trusted.xattr-test", "working", 8, 0);
|
|
Packit Service |
e080da |
* if (ret < 0) {
|
|
Packit Service |
e080da |
* fprintf (stderr, "fsetxattr failed : %s\n", strerror
|
|
Packit Service |
e080da |
(errno));
|
|
Packit Service |
e080da |
* result |= ret;
|
|
Packit Service |
e080da |
* }
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
* ret = flistxattr (fd, NULL, 0);
|
|
Packit Service |
e080da |
* if (ret <= 0) {
|
|
Packit Service |
e080da |
* fprintf (stderr, "flistxattr failed : %s\n", strerror
|
|
Packit Service |
e080da |
(errno));
|
|
Packit Service |
e080da |
* result |= ret;
|
|
Packit Service |
e080da |
* }
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
* ret = fgetxattr (fd, "trusted.xattr-test", NULL, 0);
|
|
Packit Service |
e080da |
* if (ret <= 0) {
|
|
Packit Service |
e080da |
* fprintf (stderr, "fgetxattr failed : %s\n", strerror
|
|
Packit Service |
e080da |
(errno));
|
|
Packit Service |
e080da |
* result |= ret;
|
|
Packit Service |
e080da |
* }
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
* ret = fremovexattr (fd, "trusted.xattr-test");
|
|
Packit Service |
e080da |
* if (ret < 0) {
|
|
Packit Service |
e080da |
* fprintf (stderr, "fremovexattr failed : %s\n", strerror
|
|
Packit Service |
e080da |
(errno));
|
|
Packit Service |
e080da |
* result |= ret;
|
|
Packit Service |
e080da |
* }
|
|
Packit Service |
e080da |
*/
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
if (fd)
|
|
Packit Service |
e080da |
close(fd);
|
|
Packit Service |
e080da |
return result;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
fd_based_fops_2(char *filename)
|
|
Packit Service |
e080da |
{
|
|
Packit Service |
e080da |
int fd = 0;
|
|
Packit Service |
e080da |
int ret = -1;
|
|
Packit Service |
e080da |
int result = 0;
|
|
Packit Service |
e080da |
struct stat stbuf = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
char wstr[50] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
char rstr[50] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
fd = open(filename, O_RDWR | O_CREAT, OPEN_MODE);
|
|
Packit Service |
e080da |
if (fd < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "open failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
return ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = ftruncate(fd, 0);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "ftruncate failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
strcpy(wstr, "This is my second string\n");
|
|
Packit Service |
e080da |
ret = write(fd, wstr, strlen(wstr));
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "write failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
lseek(fd, 0, SEEK_SET);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "lseek failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = read(fd, rstr, strlen(wstr));
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "read failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = memcmp(rstr, wstr, strlen(wstr));
|
|
Packit Service |
e080da |
if (ret != 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "read returning junk\n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fstat(fd, &stbuf);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fstat failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fchmod(fd, 0640);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fchmod failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fchown(fd, 10001, 10001);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fchown failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fsync(fd);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fsync failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fsetxattr(fd, "trusted.xattr-test", "working", 8, 0);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fsetxattr failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fdatasync(fd);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fdatasync failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = flistxattr(fd, NULL, 0);
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "flistxattr failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fgetxattr(fd, "trusted.xattr-test", NULL, 0);
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fgetxattr failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fremovexattr(fd, "trusted.xattr-test");
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fremovexattr failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
if (fd)
|
|
Packit Service |
e080da |
close(fd);
|
|
Packit Service |
e080da |
unlink(filename);
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
return result;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
path_based_fops(char *filename)
|
|
Packit Service |
e080da |
{
|
|
Packit Service |
e080da |
int ret = -1;
|
|
Packit Service |
e080da |
int fd = 0;
|
|
Packit Service |
e080da |
int result = 0;
|
|
Packit Service |
e080da |
struct stat stbuf = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
char newfilename[255] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
char *hardlink = "linkfile-hard.txt";
|
|
Packit Service |
e080da |
char *symlnk = "linkfile-soft.txt";
|
|
Packit Service |
e080da |
char buf[1024] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
fd = creat(filename, 0644);
|
|
Packit Service |
e080da |
if (fd < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "creat failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
return ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = truncate(filename, 0);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "truncate failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = stat(filename, &stbuf);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "stat failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = chmod(filename, 0640);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "chmod failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = chown(filename, 10001, 10001);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "chown failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = setxattr(filename, "trusted.xattr-test", "working", 8, 0);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "setxattr failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = listxattr(filename, NULL, 0);
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
ret = -1;
|
|
Packit Service |
e080da |
fprintf(stderr, "listxattr failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = getxattr(filename, "trusted.xattr-test", NULL, 0);
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "getxattr failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = removexattr(filename, "trusted.xattr-test");
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "removexattr failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = access(filename, R_OK | W_OK);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "access failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = link(filename, hardlink);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "link failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
unlink(hardlink);
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = symlink(filename, symlnk);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "symlink failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = readlink(symlnk, buf, sizeof(buf));
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "readlink failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
unlink(symlnk);
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
/* Create a character special file */
|
|
Packit Service |
e080da |
ret = mknod("cspecial", S_IFCHR | S_IRWXU | S_IRWXG, makedev(2, 3));
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "cpsecial mknod failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
unlink("cspecial");
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = mknod("bspecial", S_IFBLK | S_IRWXU | S_IRWXG, makedev(4, 5));
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "bspecial mknod failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
unlink("bspecial");
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
#ifdef linux
|
|
Packit Service |
e080da |
ret = mknod("fifo", S_IFIFO | S_IRWXU | S_IRWXG, 0);
|
|
Packit Service |
e080da |
#else
|
|
Packit Service |
e080da |
ret = mkfifo("fifo", 0);
|
|
Packit Service |
e080da |
#endif
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fifo mknod failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
unlink("fifo");
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
#ifdef linux
|
|
Packit Service |
e080da |
ret = mknod("sock", S_IFSOCK | S_IRWXU | S_IRWXG, 0);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "sock mknod failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
#else
|
|
Packit Service |
e080da |
{
|
|
Packit Service |
e080da |
int s;
|
|
Packit Service |
e080da |
const char *pathname = "sock";
|
|
Packit Service |
e080da |
struct sockaddr_un addr;
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
s = socket(PF_LOCAL, SOCK_STREAM, 0);
|
|
Packit Service |
e080da |
memset(&addr, 0, sizeof(addr));
|
|
Packit Service |
e080da |
strncpy(addr.sun_path, pathname, sizeof(addr.sun_path));
|
|
Packit Service |
e080da |
ret = bind(s, (const struct sockaddr *)&addr, SUN_LEN(&addr));
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fifo mknod failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
close(s);
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
#endif
|
|
Packit Service |
e080da |
unlink("sock");
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
strcpy(newfilename, filename);
|
|
Packit Service |
e080da |
strcat(newfilename, "_new");
|
|
Packit Service |
e080da |
ret = rename(filename, newfilename);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "rename failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
unlink(newfilename);
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
if (fd)
|
|
Packit Service |
e080da |
close(fd);
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
unlink(filename);
|
|
Packit Service |
e080da |
return result;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
dup_fd_based_fops(char *filename)
|
|
Packit Service |
e080da |
{
|
|
Packit Service |
e080da |
int fd = 0;
|
|
Packit Service |
e080da |
int result = 0;
|
|
Packit Service |
e080da |
int newfd = 0;
|
|
Packit Service |
e080da |
int ret = -1;
|
|
Packit Service |
e080da |
struct stat stbuf = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
char wstr[50] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
char rstr[50] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
fd = open(filename, O_RDWR | O_CREAT, OPEN_MODE);
|
|
Packit Service |
e080da |
if (fd < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "open failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
return ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
newfd = dup(fd);
|
|
Packit Service |
e080da |
if (newfd < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "dup failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
close(fd);
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
strcpy(wstr, "This is my string\n");
|
|
Packit Service |
e080da |
ret = write(newfd, wstr, strlen(wstr));
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "write failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = lseek(newfd, 0, SEEK_SET);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "lseek failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = read(newfd, rstr, strlen(wstr));
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "read failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = memcmp(rstr, wstr, strlen(wstr));
|
|
Packit Service |
e080da |
if (ret != 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "read returning junk\n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = ftruncate(newfd, 0);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "ftruncate failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fstat(newfd, &stbuf);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fstat failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fchmod(newfd, 0640);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fchmod failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fchown(newfd, 10001, 10001);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fchown failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fsync(newfd);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fsync failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fsetxattr(newfd, "trusted.xattr-test", "working", 8, 0);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fsetxattr failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fdatasync(newfd);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fdatasync failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = flistxattr(newfd, NULL, 0);
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "flistxattr failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fgetxattr(newfd, "trusted.xattr-test", NULL, 0);
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fgetxattr failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = fremovexattr(newfd, "trusted.xattr-test");
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "fremovexattr failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
if (newfd)
|
|
Packit Service |
e080da |
close(newfd);
|
|
Packit Service |
e080da |
ret = unlink(filename);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "unlink failed : %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
return result;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
dir_based_fops(char *dirname)
|
|
Packit Service |
e080da |
{
|
|
Packit Service |
e080da |
int ret = -1;
|
|
Packit Service |
e080da |
int result = 0;
|
|
Packit Service |
e080da |
DIR *dp = NULL;
|
|
Packit Service |
e080da |
char buff[255] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
struct dirent *dbuff = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
struct stat stbuff = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
char newdname[255] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
char *cwd = NULL;
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = mkdir(dirname, 0755);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "mkdir failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
return ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
dp = opendir(dirname);
|
|
Packit Service |
e080da |
if (dp == NULL) {
|
|
Packit Service |
e080da |
fprintf(stderr, "opendir failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
dbuff = readdir(dp);
|
|
Packit Service |
e080da |
if (NULL == dbuff) {
|
|
Packit Service |
e080da |
fprintf(stderr, "readdir failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = closedir(dp);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "closedir failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = stat(dirname, &stbuff);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "stat failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = chmod(dirname, 0744);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "chmod failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = chown(dirname, 10001, 10001);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "chmod failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = setxattr(dirname, "trusted.xattr-test", "working", 8, 0);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "setxattr failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = listxattr(dirname, NULL, 0);
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
ret = -1;
|
|
Packit Service |
e080da |
fprintf(stderr, "listxattr failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = getxattr(dirname, "trusted.xattr-test", NULL, 0);
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
ret = -1;
|
|
Packit Service |
e080da |
fprintf(stderr, "getxattr failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = removexattr(dirname, "trusted.xattr-test");
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "removexattr failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
strcpy(newdname, dirname);
|
|
Packit Service |
e080da |
strcat(newdname, "/../");
|
|
Packit Service |
e080da |
ret = chdir(newdname);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "chdir failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
cwd = getcwd(buff, 255);
|
|
Packit Service |
e080da |
if (NULL == cwd) {
|
|
Packit Service |
e080da |
fprintf(stderr, "getcwd failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
strcpy(newdname, dirname);
|
|
Packit Service |
e080da |
strcat(newdname, "new");
|
|
Packit Service |
e080da |
ret = rename(dirname, newdname);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "rename failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = rmdir(newdname);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "rmdir failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
rmdir(dirname);
|
|
Packit Service |
e080da |
return result;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
link_based_fops(char *filename)
|
|
Packit Service |
e080da |
{
|
|
Packit Service |
e080da |
int ret = -1;
|
|
Packit Service |
e080da |
int result = 0;
|
|
Packit Service |
e080da |
int fd = 0;
|
|
Packit Service |
e080da |
char newname[255] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
char linkname[255] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
struct stat lstbuf = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
fd = creat(filename, 0644);
|
|
Packit Service |
e080da |
if (fd < 0) {
|
|
Packit Service |
e080da |
fd = 0;
|
|
Packit Service |
e080da |
fprintf(stderr, "creat failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
return ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
strcpy(newname, filename);
|
|
Packit Service |
e080da |
strcat(newname, "_hlink");
|
|
Packit Service |
e080da |
ret = link(filename, newname);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "link failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = unlink(filename);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "unlink failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
strcpy(linkname, filename);
|
|
Packit Service |
e080da |
strcat(linkname, "_slink");
|
|
Packit Service |
e080da |
ret = symlink(newname, linkname);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "symlink failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = lstat(linkname, &lstbuf);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "lstbuf failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = lchown(linkname, 10001, 10001);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "lchown failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = lsetxattr(linkname, "trusted.lxattr-test", "working", 8, 0);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "lsetxattr failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = llistxattr(linkname, NULL, 0);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
ret = -1;
|
|
Packit Service |
e080da |
fprintf(stderr, "llistxattr failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = lgetxattr(linkname, "trusted.lxattr-test", NULL, 0);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
ret = -1;
|
|
Packit Service |
e080da |
fprintf(stderr, "lgetxattr failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = lremovexattr(linkname, "trusted.lxattr-test");
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "lremovexattr failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
if (fd)
|
|
Packit Service |
e080da |
close(fd);
|
|
Packit Service |
e080da |
unlink(linkname);
|
|
Packit Service |
e080da |
unlink(newname);
|
|
Packit Service |
e080da |
return result;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
test_open_modes(char *filename)
|
|
Packit Service |
e080da |
{
|
|
Packit Service |
e080da |
int ret = -1;
|
|
Packit Service |
e080da |
int result = 0;
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = generic_open_read_write(filename, O_CREAT | O_WRONLY, OPEN_MODE);
|
|
Packit Service |
e080da |
if (ret != 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "flag O_CREAT|O_WRONLY failed: \n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = generic_open_read_write(filename, O_CREAT | O_RDWR, OPEN_MODE);
|
|
Packit Service |
e080da |
if (ret != 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "flag O_CREAT|O_RDWR failed\n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = generic_open_read_write(filename, O_CREAT | O_RDONLY, OPEN_MODE);
|
|
Packit Service |
e080da |
if (ret != 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "flag O_CREAT|O_RDONLY failed\n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = creat(filename, 0644);
|
|
Packit Service |
e080da |
close(ret);
|
|
Packit Service |
e080da |
ret = generic_open_read_write(filename, O_WRONLY, 0);
|
|
Packit Service |
e080da |
if (ret != 0) {
|
|
Packit Service |
e080da |
fprintf(stderr, "flag O_WRONLY failed\n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = creat(filename, 0644);
|
|
Packit Service |
e080da |
close(ret);
|
|
Packit Service |
e080da |
ret = generic_open_read_write(filename, O_RDWR, 0);
|
|
Packit Service |
e080da |
if (0 != ret) {
|
|
Packit Service |
e080da |
fprintf(stderr, "flag O_RDWR failed\n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = creat(filename, 0644);
|
|
Packit Service |
e080da |
close(ret);
|
|
Packit Service |
e080da |
ret = generic_open_read_write(filename, O_RDONLY, 0);
|
|
Packit Service |
e080da |
if (0 != ret) {
|
|
Packit Service |
e080da |
fprintf(stderr, "flag O_RDONLY failed\n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = creat(filename, 0644);
|
|
Packit Service |
e080da |
close(ret);
|
|
Packit Service |
e080da |
ret = generic_open_read_write(filename, O_TRUNC | O_WRONLY, 0);
|
|
Packit Service |
e080da |
if (0 != ret) {
|
|
Packit Service |
e080da |
fprintf(stderr, "flag O_TRUNC|O_WRONLY failed\n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
#if 0 /* undefined behaviour, unable to reliably test */
|
|
Packit Service |
e080da |
ret = creat (filename, 0644);
|
|
Packit Service |
e080da |
close (ret);
|
|
Packit Service |
e080da |
ret = generic_open_read_write (filename, O_TRUNC|O_RDONLY, 0);
|
|
Packit Service |
e080da |
if (0 != ret) {
|
|
Packit Service |
e080da |
fprintf (stderr, "flag O_TRUNC|O_RDONLY failed\n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
#endif
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = generic_open_read_write(filename, O_CREAT | O_RDWR | O_SYNC,
|
|
Packit Service |
e080da |
OPEN_MODE);
|
|
Packit Service |
e080da |
if (0 != ret) {
|
|
Packit Service |
e080da |
fprintf(stderr, "flag O_CREAT|O_RDWR|O_SYNC failed\n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = creat(filename, 0644);
|
|
Packit Service |
e080da |
close(ret);
|
|
Packit Service |
e080da |
ret = generic_open_read_write(filename, O_CREAT | O_EXCL, OPEN_MODE);
|
|
Packit Service |
e080da |
if (0 != ret) {
|
|
Packit Service |
e080da |
fprintf(stderr, "flag O_CREAT|O_EXCL failed\n");
|
|
Packit Service |
e080da |
result |= ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
return result;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
int
|
|
Packit Service |
e080da |
generic_open_read_write(char *filename, int flag, mode_t mode)
|
|
Packit Service |
e080da |
{
|
|
Packit Service |
e080da |
int fd = 0;
|
|
Packit Service |
e080da |
int ret = -1;
|
|
Packit Service |
e080da |
char wstring[50] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
char rstring[50] = {
|
|
Packit Service |
e080da |
0,
|
|
Packit Service |
e080da |
};
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
fd = open(filename, flag, mode);
|
|
Packit Service |
e080da |
if (fd < 0) {
|
|
Packit Service |
e080da |
if (flag == (O_CREAT | O_EXCL) && errno == EEXIST) {
|
|
Packit Service |
e080da |
unlink(filename);
|
|
Packit Service |
e080da |
return 0;
|
|
Packit Service |
e080da |
} else {
|
|
Packit Service |
e080da |
fprintf(stderr, "open failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
return -1;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
strcpy(wstring, "My string to write\n");
|
|
Packit Service |
e080da |
ret = write(fd, wstring, strlen(wstring));
|
|
Packit Service |
e080da |
if (ret <= 0) {
|
|
Packit Service |
e080da |
if (errno != EBADF) {
|
|
Packit Service |
e080da |
fprintf(stderr, "write failed: %s\n", strerror(errno));
|
|
Packit Service |
e080da |
close(fd);
|
|
Packit Service |
e080da |
unlink(filename);
|
|
Packit Service |
e080da |
return ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = lseek(fd, 0, SEEK_SET);
|
|
Packit Service |
e080da |
if (ret < 0) {
|
|
Packit Service |
e080da |
close(fd);
|
|
Packit Service |
e080da |
unlink(filename);
|
|
Packit Service |
e080da |
return ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = read(fd, rstring, strlen(wstring));
|
|
Packit Service |
e080da |
if (ret < 0 && flag != (O_CREAT | O_WRONLY) && flag != O_WRONLY &&
|
|
Packit Service |
e080da |
flag != (O_TRUNC | O_WRONLY)) {
|
|
Packit Service |
e080da |
close(fd);
|
|
Packit Service |
e080da |
unlink(filename);
|
|
Packit Service |
e080da |
return ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
/* Compare the rstring with wstring. But we do not want to return
|
|
Packit Service |
e080da |
* error when the flag is either O_RDONLY, O_CREAT|O_RDONLY or
|
|
Packit Service |
e080da |
* O_TRUNC|O_RDONLY. Because in that case we are not writing
|
|
Packit Service |
e080da |
* anything to the file.*/
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
ret = memcmp(wstring, rstring, strlen(wstring));
|
|
Packit Service |
e080da |
if (0 != ret && flag != (O_TRUNC | O_WRONLY) && flag != O_WRONLY &&
|
|
Packit Service |
e080da |
flag != (O_CREAT | O_WRONLY) &&
|
|
Packit Service |
e080da |
!(flag == (O_CREAT | O_RDONLY) || flag == O_RDONLY ||
|
|
Packit Service |
e080da |
flag == (O_TRUNC | O_RDONLY))) {
|
|
Packit Service |
e080da |
fprintf(stderr, "read is returning junk\n");
|
|
Packit Service |
e080da |
close(fd);
|
|
Packit Service |
e080da |
unlink(filename);
|
|
Packit Service |
e080da |
return ret;
|
|
Packit Service |
e080da |
}
|
|
Packit Service |
e080da |
|
|
Packit Service |
e080da |
close(fd);
|
|
Packit Service |
e080da |
unlink(filename);
|
|
Packit Service |
e080da |
return 0;
|
|
Packit Service |
e080da |
}
|