sd-memfd.c revision 453a0c2946da620f99825d39db335e9ea9861829
/*-*- 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 "sd-memfd.h"
struct sd_memfd {
int fd;
FILE *f;
};
int sd_memfd_new(sd_memfd **m) {
sd_memfd *n;
int fd;
if (!m)
return -EINVAL;
if (kdbus < 0)
return -errno;
return -errno;
if (!n)
return -ENOMEM;
*m = n;
return 0;
}
sd_memfd *n;
if (!m)
return -EINVAL;
if (fd < 0)
return -EINVAL;
/* Check if this is a valid memfd */
return -ENOTTY;
if (!n)
return -ENOMEM;
*m = n;
return 0;
}
void sd_memfd_free(sd_memfd *m) {
if (!m)
return;
if (m->f)
fclose(m->f);
else
close_nointr_nofail(m->fd);
free(m);
}
int sd_memfd_get_fd(sd_memfd *m) {
if (!m)
return -EINVAL;
return m->fd;
}
if (!m)
return -EINVAL;
if (!f)
return -EINVAL;
if (!m->f) {
if (!m->f)
return -errno;
}
*f = m->f;
return 0;
}
int sd_memfd_dup_fd(sd_memfd *m) {
int fd;
if (!m)
return -EINVAL;
if (fd < 0)
return -errno;
return fd;
}
void *q;
int sealed;
if (!m)
return -EINVAL;
if (size <= 0)
return -EINVAL;
if (!p)
return -EINVAL;
sealed = sd_memfd_get_sealed(m);
if (sealed < 0)
return sealed;
if (q == MAP_FAILED)
return -errno;
*p = q;
return 0;
}
int sd_memfd_set_sealed(sd_memfd *m, int b) {
int r;
if (!m)
return -EINVAL;
if (r < 0)
return -errno;
return 0;
}
int sd_memfd_get_sealed(sd_memfd *m) {
int r, b;
if (!m)
return -EINVAL;
if (r < 0)
return -errno;
return !!b;
}
int r;
if (!m)
return -EINVAL;
if (!sz)
return -EINVAL;
if (r < 0)
return -errno;
return r;
}
int r;
if (!m)
return -EINVAL;
if (r < 0)
return -errno;
return r;
}
sd_memfd *n;
int r;
r = sd_memfd_new(&n);
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;
}