16 #include <sys/param.h> 17 #include <sys/types.h> 19 #include <sys/resource.h> 47 int offset = 1, len = 0;
49 char *path = strdup(path_c);
54 for (len = strlen(path); offset < len; offset++) {
55 if (path[offset] ==
'/') {
57 if ((mkdir(path, mode) < 0) && (errno != EEXIST)) {
64 if ((mkdir(path, mode) < 0) && (errno != EEXIST)) {
86 crm_err(
"Could not create directory '%s': %s",
106 CRM_CHECK((path != NULL) && (resolved_path != NULL),
return EINVAL);
108 #if _POSIX_VERSION >= 200809L 110 *resolved_path = realpath(path, NULL);
111 return (*resolved_path == NULL)? errno :
pcmk_rc_ok;
113 #elif defined(PATH_MAX) 116 *resolved_path = malloc(PATH_MAX);
117 if ((*resolved_path == NULL) || (realpath(path, *resolved_path) == NULL)) {
122 *resolved_path = NULL;
143 CRM_ASSERT((directory != NULL) && (series != NULL));
145 (bzip?
"bz2" :
"raw"));
164 char *series_file = NULL;
166 if ((directory == NULL) || (series == NULL) || (seq == NULL)) {
171 fp = fopen(series_file,
"r");
174 crm_debug(
"Could not open series file %s: %s",
180 if (fscanf(fp,
"%u", seq) != 1) {
182 crm_debug(
"Could not read sequence number from series file %s: %s",
188 crm_trace(
"Found last sequence number %u in series file %s",
207 unsigned int sequence,
int max)
210 FILE *file_strm = NULL;
211 char *series_file = NULL;
219 if (max > 0 && sequence >= max) {
224 file_strm = fopen(series_file,
"w");
225 if (file_strm != NULL) {
226 rc = fprintf(file_strm,
"%u", sequence);
228 crm_perror(LOG_ERR,
"Cannot write to series file %s", series_file);
232 crm_err(
"Cannot open series file %s for writing", series_file);
235 if (file_strm != NULL) {
240 crm_trace(
"Wrote %d to %s", sequence, series_file);
257 uid_t uid, gid_t gid)
259 char *series_file = NULL;
262 if ((directory == NULL) || (series == NULL)) {
266 if (chown(series_file, uid, gid) < 0) {
274 pcmk__daemon_user_can_write(
const char *target_name,
struct stat *target_stat)
276 struct passwd *sys_user = NULL;
280 if (sys_user == NULL) {
285 if (target_stat->st_uid != sys_user->pw_uid) {
288 target_stat->st_uid);
291 if ((target_stat->st_mode & (S_IRUSR | S_IWUSR)) == 0) {
292 crm_notice(
"%s is not readable and writable by user %s " 295 (
unsigned long) target_stat->st_mode);
302 pcmk__daemon_group_can_write(
const char *target_name,
struct stat *target_stat)
304 struct group *sys_grp = NULL;
308 if (sys_grp == NULL) {
314 if (target_stat->st_gid != sys_grp->gr_gid) {
317 sys_grp->gr_gid, target_stat->st_gid);
321 if ((target_stat->st_mode & (S_IRGRP | S_IWGRP)) == 0) {
322 crm_notice(
"%s is not readable and writable by group %s " 325 (
unsigned long) target_stat->st_mode);
350 char *full_file = NULL;
351 const char *
target = NULL;
361 s_res = stat(full_file, &buf);
368 }
else if (S_ISREG(buf.st_mode) == FALSE) {
370 target, (
unsigned long) buf.st_mode);
379 s_res = stat(dir, &buf);
384 }
else if (S_ISDIR(buf.st_mode) == FALSE) {
386 dir, (
unsigned long) buf.st_mode);
391 if (!pcmk__daemon_user_can_write(
target, &buf)
392 && !pcmk__daemon_group_can_write(
target, &buf)) {
394 crm_err(
"%s must be owned and writable by either user %s or group %s " 397 (
unsigned long) buf.st_mode);
419 directory = opendir(
name);
420 if (directory == NULL) {
425 fd = dirfd(directory);
427 crm_perror(LOG_ERR,
"Could not obtain file descriptor for %s",
name);
434 if (closedir(directory) < 0) {
453 int length, read_len;
456 if ((filename == NULL) || (contents == NULL)) {
460 fp = fopen(filename,
"r");
461 if ((fp == NULL) || (fseek(fp, 0L, SEEK_END) < 0)) {
475 *contents = calloc(length + 1,
sizeof(
char));
476 if (*contents == NULL) {
481 read_len = fread(*contents, 1, length, fp);
482 if (read_len != length) {
509 FILE *fp = fdopen(fd,
"w");
514 if ((contents != NULL) && (fprintf(fp,
"%s", contents) < 0)) {
517 if (fflush(fp) != 0) {
520 if (fsync(fileno(fp)) < 0) {
538 int flag = fcntl(fd, F_GETFL);
543 if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
561 const char *dir = getenv(
"TMPDIR");
563 return (dir && (*dir ==
'/'))? dir :
"/tmp";
582 int min_fd = (all? 0 : (STDERR_FILENO + 1));
587 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
588 max_fd = rlim.rlim_cur - 1;
590 long conf_max = sysconf(_SC_OPEN_MAX);
592 max_fd = (conf_max > 0)? conf_max : 1024;
601 dir = opendir(
"/proc/self/fd");
603 dir = opendir(
"/dev/fd");
606 dir = opendir(
"/dev/fd");
609 struct dirent *entry;
610 int dir_fd = dirfd(dir);
612 while ((entry = readdir(dir)) != NULL) {
613 int lpc = atoi(entry->d_name);
621 if ((lpc >= min_fd) && (lpc <= max_fd) && (lpc != dir_fd)) {
632 for (
int lpc = max_fd; lpc >= min_fd; lpc--) {
#define CRM_CHECK(expr, failure_action)
#define crm_notice(fmt, args...)
const char * pcmk_strerror(int rc)
int pcmk__real_path(const char *path, char **resolved_path)
char * pcmk__series_filename(const char *directory, const char *series, int sequence, bool bzip)
const char * pcmk__get_tmpdir()
int pcmk__chown_series_sequence(const char *directory, const char *series, uid_t uid, gid_t gid)
void pcmk__write_series_sequence(const char *directory, const char *series, unsigned int sequence, int max)
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
char * strerror(int errnum)
bool pcmk__daemon_can_write(const char *dir, const char *file)
#define crm_debug(fmt, args...)
void crm_build_path(const char *path_c, mode_t mode)
Create a directory, including any parent directories needed.
#define crm_trace(fmt, args...)
int pcmk__build_path(const char *path_c, mode_t mode)
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
#define crm_err(fmt, args...)
int pcmk__read_series_sequence(const char *directory, const char *series, unsigned int *seq)
void pcmk__close_fds_in_child(bool all)
void pcmk__sync_directory(const char *name)
int pcmk__write_sync(int fd, const char *contents)
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
int pcmk__set_nonblocking(int fd)
int pcmk__file_contents(const char *filename, char **contents)