sd-memfd.c revision a6278b88305b237b02eabff0d870b57fe851822d
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <fcntl.h>
#include "util.h"
#include "kdbus.h"
#include "bus-label.h"
#include "sd-memfd.h"
#include "sd-bus.h"
struct sd_memfd {
int fd;
FILE *f;
};
struct kdbus_cmd_memfd_make *cmd;
struct kdbus_item *item;
_cleanup_free_ char *g = NULL;
sd_memfd *n;
assert_return(m, -EINVAL);
if (kdbus < 0)
return -errno;
if (name) {
/* The kernel side is pretty picky about the character
* set here, let's do the usual bus escaping to deal
* with that. */
g = bus_label_escape(name);
if (!g)
return -ENOMEM;
name = g;
} else {
char pr[17] = {};
/* If no name is specified we generate one. We include
* a hint indicating our library implementation, and
* add the thread name to it */
name = "sd";
else {
_cleanup_free_ char *e = NULL;
e = bus_label_escape(pr);
if (!e)
return -ENOMEM;
g = strappend("sd-", e);
if (!g)
return -ENOMEM;
name = g;
}
}
l + 1;
return -errno;
if (!n) {
return -ENOMEM;
}
*m = n;
return 0;
}
sd_memfd *n;
assert_return(m, -EINVAL);
/* Check if this is a valid memfd */
return -ENOTTY;
if (!n)
return -ENOMEM;
*m = n;
return 0;
}
if (!m)
return;
if (m->f)
fclose(m->f);
else
close_nointr_nofail(m->fd);
free(m);
}
assert_return(m, -EINVAL);
return m->fd;
}
assert_return(m, -EINVAL);
assert_return(f, -EINVAL);
if (!m->f) {
if (!m->f)
return -errno;
}
*f = m->f;
return 0;
}
int fd;
assert_return(m, -EINVAL);
if (fd < 0)
return -errno;
return fd;
}
void *q;
int sealed;
assert_return(m, -EINVAL);
assert_return(p, -EINVAL);
sealed = sd_memfd_get_sealed(m);
if (sealed < 0)
return sealed;
if (q == MAP_FAILED)
return -errno;
*p = q;
return 0;
}
int r;
assert_return(m, -EINVAL);
if (r < 0)
return -errno;
return 0;
}
int r, b;
assert_return(m, -EINVAL);
if (r < 0)
return -errno;
return !!b;
}
int r;
assert_return(m, -EINVAL);
if (r < 0)
return -errno;
return r;
}
int r;
assert_return(m, -EINVAL);
if (r < 0)
return -errno;
return r;
}
sd_memfd *n;
int r;
r = sd_memfd_new(&n, name);
if (r < 0)
return r;
r = sd_memfd_set_size(n, sz);
if (r < 0) {
sd_memfd_free(n);
return r;
}
r = sd_memfd_map(n, 0, sz, p);
if (r < 0) {
sd_memfd_free(n);
return r;
}
*m = n;
return 0;
}
_cleanup_free_ char *n = NULL;
ssize_t k;
assert_return(m, -EINVAL);
if (k < 0)
return -errno;
return -E2BIG;
buf[k] = 0;
if (!delim)
return -EIO;
if (!delim)
return -EIO;
delim++;
if (!end)
return -EIO;
if (!n)
return -ENOMEM;
e = bus_label_unescape(n);
if (!e)
return -ENOMEM;
*name = e;
return 0;
}