]>
git.proxmox.com Git - systemd.git/blob - src/basic/chattr-util.c
2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/ioctl.h>
26 #include "chattr-util.h"
30 int chattr_fd(int fd
, unsigned value
, unsigned mask
) {
31 unsigned old_attr
, new_attr
;
36 if (fstat(fd
, &st
) < 0)
39 /* Explicitly check whether this is a regular file or
40 * directory. If it is anything else (such as a device node or
41 * fifo), then the ioctl will not hit the file systems but
42 * possibly drivers, where the ioctl might have different
43 * effects. Notably, DRM is using the same ioctl() number. */
45 if (!S_ISDIR(st
.st_mode
) && !S_ISREG(st
.st_mode
))
51 if (ioctl(fd
, FS_IOC_GETFLAGS
, &old_attr
) < 0)
54 new_attr
= (old_attr
& ~mask
) | (value
& mask
);
55 if (new_attr
== old_attr
)
58 if (ioctl(fd
, FS_IOC_SETFLAGS
, &new_attr
) < 0)
64 int chattr_path(const char *p
, unsigned value
, unsigned mask
) {
65 _cleanup_close_
int fd
= -1;
72 fd
= open(p
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
76 return chattr_fd(fd
, value
, mask
);
79 int read_attr_fd(int fd
, unsigned *ret
) {
84 if (fstat(fd
, &st
) < 0)
87 if (!S_ISDIR(st
.st_mode
) && !S_ISREG(st
.st_mode
))
90 if (ioctl(fd
, FS_IOC_GETFLAGS
, ret
) < 0)
96 int read_attr_path(const char *p
, unsigned *ret
) {
97 _cleanup_close_
int fd
= -1;
102 fd
= open(p
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
106 return read_attr_fd(fd
, ret
);