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);
239 uid_t uid, gid_t gid)
241 char *series_file = NULL;
244 if ((directory == NULL) || (series == NULL)) {
248 if (chown(series_file, uid, gid) < 0) {
256 pcmk__daemon_user_can_write(
const char *target_name,
struct stat *target_stat)
258 struct passwd *sys_user = NULL;
262 if (sys_user == NULL) {
267 if (target_stat->st_uid != sys_user->pw_uid) {
270 target_stat->st_uid);
273 if ((target_stat->st_mode & (S_IRUSR | S_IWUSR)) == 0) {
274 crm_notice(
"%s is not readable and writable by user %s " 277 (
unsigned long) target_stat->st_mode);
284 pcmk__daemon_group_can_write(
const char *target_name,
struct stat *target_stat)
286 struct group *sys_grp = NULL;
290 if (sys_grp == NULL) {
296 if (target_stat->st_gid != sys_grp->gr_gid) {
299 sys_grp->gr_gid, target_stat->st_gid);
303 if ((target_stat->st_mode & (S_IRGRP | S_IWGRP)) == 0) {
304 crm_notice(
"%s is not readable and writable by group %s " 307 (
unsigned long) target_stat->st_mode);
332 char *full_file = NULL;
333 const char *
target = NULL;
343 s_res = stat(full_file, &buf);
350 }
else if (S_ISREG(buf.st_mode) == FALSE) {
352 target, (
unsigned long) buf.st_mode);
361 s_res = stat(dir, &buf);
366 }
else if (S_ISDIR(buf.st_mode) == FALSE) {
368 dir, (
unsigned long) buf.st_mode);
373 if (!pcmk__daemon_user_can_write(
target, &buf)
374 && !pcmk__daemon_group_can_write(
target, &buf)) {
376 crm_err(
"%s must be owned and writable by either user %s or group %s " 379 (
unsigned long) buf.st_mode);
401 directory = opendir(
name);
402 if (directory == NULL) {
407 fd = dirfd(directory);
409 crm_perror(LOG_ERR,
"Could not obtain file descriptor for %s",
name);
416 if (closedir(directory) < 0) {
435 int length, read_len;
438 if ((filename == NULL) || (contents == NULL)) {
442 fp = fopen(filename,
"r");
443 if ((fp == NULL) || (fseek(fp, 0L, SEEK_END) < 0)) {
457 *contents = calloc(length + 1,
sizeof(
char));
458 if (*contents == NULL) {
463 read_len = fread(*contents, 1, length, fp);
464 if (read_len != length) {
491 FILE *fp = fdopen(fd,
"w");
496 if ((contents != NULL) && (fprintf(fp,
"%s", contents) < 0)) {
499 if (fflush(fp) != 0) {
502 if (fsync(fileno(fp)) < 0) {
520 int flag = fcntl(fd, F_GETFL);
525 if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
543 const char *dir = getenv(
"TMPDIR");
545 return (dir && (*dir ==
'/'))? dir :
"/tmp";
564 int min_fd = (all? 0 : (STDERR_FILENO + 1));
569 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
570 max_fd = rlim.rlim_cur - 1;
572 long conf_max = sysconf(_SC_OPEN_MAX);
574 max_fd = (conf_max > 0)? conf_max : 1024;
582 #if HAVE_LINUX_PROCFS 583 dir = opendir(
"/proc/self/fd");
585 dir = opendir(
"/dev/fd");
588 dir = opendir(
"/dev/fd");
589 #endif // HAVE_LINUX_PROCFS 591 struct dirent *entry;
592 int dir_fd = dirfd(dir);
594 while ((entry = readdir(dir)) != NULL) {
595 int lpc = atoi(entry->d_name);
603 if ((lpc >= min_fd) && (lpc <= max_fd) && (lpc != dir_fd)) {
614 for (
int lpc = max_fd; lpc >= min_fd; lpc--) {
634 if (filename[0] ==
'/') {
635 path = strdup(filename);
657 crm_err(
"Could not create directory '%s': %s",
#define CRM_CHECK(expr, failure_action)
#define crm_notice(fmt, args...)
int pcmk__real_path(const char *path, char **resolved_path)
char * pcmk__series_filename(const char *directory, const char *series, int sequence, bool bzip)
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)
const char * pcmk__get_tmpdir(void)
#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...)
Deprecated Pacemaker utilities.
int pcmk__read_series_sequence(const char *directory, const char *series, unsigned int *seq)
char * pcmk__full_path(const char *filename, const char *dirname)
Duplicate a file path, inserting a prefix if not absolute.
void pcmk__close_fds_in_child(bool all)
void pcmk__sync_directory(const char *name)
int pcmk__write_sync(int fd, const char *contents)
int pcmk__set_nonblocking(int fd)
int pcmk__file_contents(const char *filename, char **contents)