1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/xattr.h>
24 #include "alloc-util.h"
26 #include "sparse-endian.h"
27 #include "stdio-util.h"
29 #include "xattr-util.h"
31 int getxattr_malloc(const char *path, const char *name, char **value, bool allow_symlink) {
40 for (l = 100; ; l = (size_t) n + 1) {
46 n = lgetxattr(path, name, v, l);
48 n = getxattr(path, name, v, l);
50 if (n >= 0 && (size_t) n < l) {
57 if (n < 0 && errno != ERANGE)
61 n = lgetxattr(path, name, NULL, 0);
63 n = getxattr(path, name, NULL, 0);
69 int fgetxattr_malloc(int fd, const char *name, char **value) {
78 for (l = 100; ; l = (size_t) n + 1) {
83 n = fgetxattr(fd, name, v, l);
85 if (n >= 0 && (size_t) n < l) {
92 if (n < 0 && errno != ERANGE)
95 n = fgetxattr(fd, name, NULL, 0);
101 #if 0 /// UNNEEDED by elogind
102 ssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags) {
103 char fn[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1];
104 _cleanup_close_ int fd = -1;
107 /* The kernel doesn't have a fgetxattrat() command, hence let's emulate one */
109 fd = openat(dirfd, filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_PATH|(flags & AT_SYMLINK_NOFOLLOW ? O_NOFOLLOW : 0));
113 xsprintf(fn, "/proc/self/fd/%i", fd);
115 l = getxattr(fn, attribute, value, size);
122 static int parse_crtime(le64_t le, usec_t *usec) {
128 if (u == 0 || u == (uint64_t) -1)
135 int fd_getcrtime(int fd, usec_t *usec) {
142 /* Until Linux gets a real concept of birthtime/creation time,
143 * let's fake one with xattrs */
145 n = fgetxattr(fd, "user.crtime_usec", &le, sizeof(le));
151 return parse_crtime(le, usec);
154 int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags) {
158 n = fgetxattrat_fake(dirfd, name, "user.crtime_usec", &le, sizeof(le), flags);
164 return parse_crtime(le, usec);
167 int path_getcrtime(const char *p, usec_t *usec) {
174 n = getxattr(p, "user.crtime_usec", &le, sizeof(le));
180 return parse_crtime(le, usec);
183 int fd_setcrtime(int fd, usec_t usec) {
189 usec = now(CLOCK_REALTIME);
191 le = htole64((uint64_t) usec);
192 if (fsetxattr(fd, "user.crtime_usec", &le, sizeof(le), 0) < 0)