14 #include <sys/param.h> 15 #include <sys/types.h> 41 int offset = 1, len = 0;
42 char *path = strdup(path_c);
45 for (len = strlen(path); offset < len; offset++) {
46 if (path[offset] ==
'/') {
48 if (mkdir(path, mode) < 0 && errno != EEXIST) {
49 crm_perror(LOG_ERR,
"Could not create directory '%s'", path);
55 if (mkdir(path, mode) < 0 && errno != EEXIST) {
56 crm_perror(LOG_ERR,
"Could not create directory '%s'", path);
77 const char *ext =
"raw";
79 CRM_CHECK(directory != NULL,
return NULL);
104 FILE *file_strm = NULL;
105 int start = 0, length = 0, read_len = 0;
106 char *series_file = NULL;
114 file_strm = fopen(series_file,
"r");
115 if (file_strm == NULL) {
116 crm_debug(
"Series file %s does not exist", series_file);
122 start = ftell(file_strm);
123 fseek(file_strm, 0L, SEEK_END);
124 length = ftell(file_strm);
125 fseek(file_strm, 0L, start);
131 crm_info(
"%s was not valid", series_file);
136 crm_trace(
"Reading %d bytes from file", length);
137 buffer = calloc(1, (length + 1));
138 read_len = fread(buffer, 1, length, file_strm);
139 if (read_len != length) {
140 crm_err(
"Calculated and read bytes differ: %d vs. %d", length, read_len);
149 crm_trace(
"Found %d in %s", seq, series_file);
171 FILE *file_strm = NULL;
172 char *series_file = NULL;
180 if (max > 0 && sequence >= max) {
185 file_strm = fopen(series_file,
"w");
186 if (file_strm != NULL) {
187 rc = fprintf(file_strm,
"%d", sequence);
189 crm_perror(LOG_ERR,
"Cannot write to series file %s", series_file);
193 crm_err(
"Cannot open series file %s for writing", series_file);
196 if (file_strm != NULL) {
201 crm_trace(
"Wrote %d to %s", sequence, series_file);
219 char *series_file = NULL;
222 CRM_CHECK((directory != NULL) && (series != NULL), errno = EINVAL;
return -1);
225 CRM_CHECK(series_file != NULL,
return -1);
227 rc = chown(series_file, uid, gid);
233 pcmk__daemon_user_can_write(
const char *target_name,
struct stat *target_stat)
235 struct passwd *sys_user = NULL;
239 if (sys_user == NULL) {
244 if (target_stat->st_uid != sys_user->pw_uid) {
247 target_stat->st_uid);
250 if ((target_stat->st_mode & (S_IRUSR | S_IWUSR)) == 0) {
251 crm_notice(
"%s is not readable and writable by user %s " 254 (
unsigned long) target_stat->st_mode);
261 pcmk__daemon_group_can_write(
const char *target_name,
struct stat *target_stat)
263 struct group *sys_grp = NULL;
267 if (sys_grp == NULL) {
273 if (target_stat->st_gid != sys_grp->gr_gid) {
276 sys_grp->gr_gid, target_stat->st_gid);
280 if ((target_stat->st_mode & (S_IRGRP | S_IWGRP)) == 0) {
281 crm_notice(
"%s is not readable and writable by group %s " 284 (
unsigned long) target_stat->st_mode);
309 char *full_file = NULL;
310 const char *target = NULL;
317 full_file = crm_concat(dir, file,
'/');
320 s_res = stat(full_file, &buf);
327 }
else if (S_ISREG(buf.st_mode) == FALSE) {
329 target, (
unsigned long) buf.st_mode);
336 if (target == NULL) {
338 s_res = stat(dir, &buf);
343 }
else if (S_ISDIR(buf.st_mode) == FALSE) {
345 dir, (
unsigned long) buf.st_mode);
350 if (!pcmk__daemon_user_can_write(target, &buf)
351 && !pcmk__daemon_group_can_write(target, &buf)) {
353 crm_err(
"%s must be owned and writable by either user %s or group %s " 356 (
unsigned long) buf.st_mode);
378 directory = opendir(name);
379 if (directory == NULL) {
380 crm_perror(LOG_ERR,
"Could not open %s for syncing", name);
384 fd = dirfd(directory);
386 crm_perror(LOG_ERR,
"Could not obtain file descriptor for %s", name);
391 crm_perror(LOG_ERR,
"Could not sync %s", name);
393 if (closedir(directory) < 0) {
394 crm_perror(LOG_ERR,
"Could not close %s after fsync", name);
412 char *contents = NULL;
414 int length, read_len;
418 fp = fopen(filename,
"r");
423 fseek(fp, 0L, SEEK_END);
427 contents = calloc(length + 1,
sizeof(
char));
428 if (contents == NULL) {
433 crm_trace(
"Reading %d bytes from %s", length, filename);
435 read_len = fread(contents, 1, length, fp);
436 if (read_len != length) {
459 FILE *fp = fdopen(fd,
"w");
464 if ((contents != NULL) && (fprintf(fp,
"%s", contents) < 0)) {
467 if (fflush(fp) != 0) {
470 if (fsync(fileno(fp)) < 0) {
488 int flag = fcntl(fd, F_GETFL);
493 if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
502 const char *dir = getenv(
"TMPDIR");
504 return (dir && (*dir ==
'/'))? dir :
"/tmp";
#define CRM_CHECK(expr, failure_action)
void write_last_sequence(const char *directory, const char *series, int sequence, int max)
#define crm_notice(fmt, args...)
const char * pcmk_strerror(int rc)
int crm_parse_int(const char *text, const char *default_text)
Parse an integer value from a string.
bool pcmk__daemon_can_write(const char *dir, const char *file)
int crm_chown_last_sequence(const char *directory, const char *series, uid_t uid, gid_t gid)
#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 get_last_sequence(const char *directory, const char *series)
int crm_write_sync(int fd, const char *contents)
#define crm_perror(level, fmt, args...)
Log a system error message.
const char * crm_get_tmpdir()
#define crm_err(fmt, args...)
void crm_sync_directory(const char *name)
char * crm_read_contents(const char *filename)
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
int crm_set_nonblocking(int fd)
#define crm_info(fmt, args...)
char * generate_series_filename(const char *directory, const char *series, int sequence, gboolean bzip)