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)) {
87 CRM_CHECK((path != NULL) && (resolved_path != NULL),
return EINVAL);
89 #if _POSIX_VERSION >= 200809L 91 *resolved_path = realpath(path, NULL);
92 return (*resolved_path == NULL)? errno :
pcmk_rc_ok;
94 #elif defined(PATH_MAX) 97 *resolved_path = malloc(PATH_MAX);
98 if ((*resolved_path == NULL) || (realpath(path, *resolved_path) == NULL)) {
103 *resolved_path = NULL;
124 CRM_ASSERT((directory != NULL) && (series != NULL));
126 (bzip?
"bz2" :
"raw"));
145 char *series_file = NULL;
147 if ((directory == NULL) || (series == NULL) || (seq == NULL)) {
152 fp = fopen(series_file,
"r");
155 crm_debug(
"Could not open series file %s: %s",
161 if (fscanf(fp,
"%u", seq) != 1) {
163 crm_debug(
"Could not read sequence number from series file %s: %s",
169 crm_trace(
"Found last sequence number %u in series file %s",
188 unsigned int sequence,
int max)
191 FILE *file_strm = NULL;
192 char *series_file = NULL;
200 if (max > 0 && sequence >= max) {
205 file_strm = fopen(series_file,
"w");
206 if (file_strm != NULL) {
207 rc = fprintf(file_strm,
"%u", sequence);
209 crm_perror(LOG_ERR,
"Cannot write to series file %s", series_file);
213 crm_err(
"Cannot open series file %s for writing", series_file);
216 if (file_strm != NULL) {
221 crm_trace(
"Wrote %d to %s", sequence, series_file);
238 uid_t uid, gid_t gid)
240 char *series_file = NULL;
243 if ((directory == NULL) || (series == NULL)) {
247 if (chown(series_file, uid, gid) < 0) {
255 pcmk__daemon_user_can_write(
const char *target_name,
struct stat *target_stat)
257 struct passwd *sys_user = NULL;
261 if (sys_user == NULL) {
266 if (target_stat->st_uid != sys_user->pw_uid) {
269 target_stat->st_uid);
272 if ((target_stat->st_mode & (S_IRUSR | S_IWUSR)) == 0) {
273 crm_notice(
"%s is not readable and writable by user %s " 276 (
unsigned long) target_stat->st_mode);
283 pcmk__daemon_group_can_write(
const char *target_name,
struct stat *target_stat)
285 struct group *sys_grp = NULL;
289 if (sys_grp == NULL) {
295 if (target_stat->st_gid != sys_grp->gr_gid) {
298 sys_grp->gr_gid, target_stat->st_gid);
302 if ((target_stat->st_mode & (S_IRGRP | S_IWGRP)) == 0) {
303 crm_notice(
"%s is not readable and writable by group %s " 306 (
unsigned long) target_stat->st_mode);
331 char *full_file = NULL;
332 const char *
target = NULL;
342 s_res = stat(full_file, &buf);
349 }
else if (S_ISREG(buf.st_mode) == FALSE) {
351 target, (
unsigned long) buf.st_mode);
360 s_res = stat(dir, &buf);
365 }
else if (S_ISDIR(buf.st_mode) == FALSE) {
367 dir, (
unsigned long) buf.st_mode);
372 if (!pcmk__daemon_user_can_write(
target, &buf)
373 && !pcmk__daemon_group_can_write(
target, &buf)) {
375 crm_err(
"%s must be owned and writable by either user %s or group %s " 378 (
unsigned long) buf.st_mode);
400 directory = opendir(
name);
401 if (directory == NULL) {
406 fd = dirfd(directory);
408 crm_perror(LOG_ERR,
"Could not obtain file descriptor for %s",
name);
415 if (closedir(directory) < 0) {
434 int length, read_len;
437 if ((filename == NULL) || (contents == NULL)) {
441 fp = fopen(filename,
"r");
442 if ((fp == NULL) || (fseek(fp, 0L, SEEK_END) < 0)) {
456 *contents = calloc(length + 1,
sizeof(
char));
457 if (*contents == NULL) {
462 read_len = fread(*contents, 1, length, fp);
463 if (read_len != length) {
490 FILE *fp = fdopen(fd,
"w");
495 if ((contents != NULL) && (fprintf(fp,
"%s", contents) < 0)) {
498 if (fflush(fp) != 0) {
501 if (fsync(fileno(fp)) < 0) {
519 int flag = fcntl(fd, F_GETFL);
524 if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
542 const char *dir = getenv(
"TMPDIR");
544 return (dir && (*dir ==
'/'))? dir :
"/tmp";
563 int min_fd = (all? 0 : (STDERR_FILENO + 1));
568 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
569 max_fd = rlim.rlim_cur - 1;
571 long conf_max = sysconf(_SC_OPEN_MAX);
573 max_fd = (conf_max > 0)? conf_max : 1024;
582 dir = opendir(
"/proc/self/fd");
584 dir = opendir(
"/dev/fd");
587 dir = opendir(
"/dev/fd");
590 struct dirent *entry;
591 int dir_fd = dirfd(dir);
593 while ((entry = readdir(dir)) != NULL) {
594 int lpc = atoi(entry->d_name);
602 if ((lpc >= min_fd) && (lpc <= max_fd) && (lpc != dir_fd)) {
613 for (
int lpc = max_fd; lpc >= min_fd; lpc--) {
628 crm_err(
"Could not create directory '%s': %s",
#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)
#define crm_trace(fmt, args...)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
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)
Deprecated Pacemaker utilities.
int pcmk__set_nonblocking(int fd)
int pcmk__file_contents(const char *filename, char **contents)