int css_logger_start() { int r = 0; if (css_logger.loop == NULL) return -1; if (css_logger.status == CSS_LOGGER_UNINIT) { LOGGER_UV_MUTEX_LOCK(&css_logger_mutex); css_logger.no_file = 0; css_logger.root_dir = DEFAULT_LOG_DIR; css_logger.timeout = DEFAULT_LOG_TIME_TIMEOUT; ensure_dir(css_logger.root_dir); //#ifdef CSS_TEST css_logger.level_priority = LL_DEBUG; //#else // css_logger.level_priority = LL_INFO; //#endif if (!css_logger.no_file) { r = open_logger_file(); buffer = buffers[bindex++]; if (r == 0) { css_logger.status = CSS_LOGGER_INIT; } } LOGGER_UV_MUTEX_UNLOCK(&css_logger_mutex); } else { printf("already init logger.\n"); } return r; }
int write_file (const char *out, const char *data, const size_t data_length) { if (!ensure_dir (out)) { return -1; } char tmp_path[PATH_MAX]; snprintf (tmp_path, sizeof (tmp_path), "%s.part", out); FILE *fp = fopen (tmp_path, "w+b"); if (!fp) { trace ("artwork: failed to open %s for writing\n", tmp_path); return -1; } int err = 0; if (fwrite (data, 1, data_length, fp) != data_length) { trace ("artwork: failed to write picture into %s\n", tmp_path); err = -1; } fclose (fp); if (!err) { err = rename (tmp_path, out); if (err) { trace ("Failed to move %s to %s: %s\n", tmp_path, out, strerror (errno)); } } unlink (tmp_path); return err; }
int copy_file (const char *in, const char *out) { trace ("copying %s to %s\n", in, out); if (!ensure_dir (out)) { return -1; } char tmp_out[PATH_MAX]; snprintf (tmp_out, PATH_MAX, "%s.part", out); FILE *fout = fopen (tmp_out, "w+b"); if (!fout) { trace ("artwork: failed to open file %s for writing\n", tmp_out); return -1; } DB_FILE *request = new_http_request (in); if (!request) { fclose (fout); trace ("artwork: failed to open file %s for reading\n", in); return -1; } int err = 0; int bytes_read; size_t file_bytes = 0; do { char buffer[BUFFER_SIZE]; bytes_read = deadbeef->fread (buffer, 1, BUFFER_SIZE, request); if (bytes_read < 0 || errno) { trace ("artwork: failed to read file %s: %s\n", tmp_out, strerror (errno)); err = -1; } else if (bytes_read > 0 && fwrite (buffer, bytes_read, 1, fout) != 1) { trace ("artwork: failed to write file %s: %s\n", tmp_out, strerror (errno)); err = -1; } file_bytes += bytes_read; } while (!err && bytes_read == BUFFER_SIZE); close_http_request (request); fclose (fout); if (file_bytes > 0 && !err) { err = rename (tmp_out, out); if (err) { trace ("artwork: failed to move %s to %s: %s\n", tmp_out, out, strerror (errno)); } } unlink (tmp_out); return err; }
void test_css_util_other() { char str[24]; char test[10]; char* s = NULL; char* t = NULL; assert(strcmp("922337203685477587",lltoa(922337203685477587,str,10)) == 0); strcpy(test," abc"); str_trim(test); assert( 0 == strcmp(test,"abc")); strcpy(test," abc "); str_trim(test); assert( 0 == strcmp(test,"abc")); strcpy(test,"abc "); str_trim(test); assert( 0 == strcmp(test,"abc")); strcpy(test," a b c "); str_trim(test); assert( 0 == strcmp(test,"a b c")); strcpy(test," "); str_trim(test); assert( 0 == strcmp(test,"")); strcpy(test," abc"); str_ltrim(test); assert( 0 == strcmp(test,"abc")); strcpy(test," abc "); str_rtrim(test); assert( 0 == strcmp(test," abc")); strcpy(test," abc "); str_ltrim(test); assert( 0 == strcmp(test,"abc ")); strcpy(test," a b c "); str_ltrim(test); assert( 0 == strcmp(test,"a b c ")); strcpy(test," "); str_ltrim(test); assert( 0 == strcmp(test,"")); strcpy(test," "); str_rtrim(test); assert( 0 == strcmp(test,"")); memset(test,' ',10); strcpy(test,"a=b"); get_split_str(test,"=",1,&s); assert( 0 == strcmp(s,"b")); FREE(s); get_split_str(test,"=",0,&s); assert( 0 == strcmp(s,"a")); FREE(s); get_split_strs(test,"=",&s,&t); assert( 0 == strcmp(s,"a")); assert( 0 == strcmp(t,"b")); FREE(s); FREE(t); memset(test,' ',10); strcpy(test," a = b "); get_split_str(test,"=",1,&s); str_trim(s); assert( 0 == strcmp(s,"b")); FREE(s); get_split_str(test,"=",0,&s); str_trim(s); assert( 0 == strcmp(s,"a")); FREE(s); get_split_strs(test,"=",&s,&t); str_trim(s); str_trim(t); assert( 0 == strcmp(s,"a")); assert( 0 == strcmp(t,"b")); FREE(s); FREE(t); assert(0 == ensure_dir("./test/dir")); assert(0 == ensure_dir("./test/dir/")); }
void add_temp_swap(long page_size) { char path[PATH_MAX]; char file[PATH_MAX]; char mkswap_cmd[PATH_MAX]; FILE *f; char *buf; long swap_size; long pid; int ret; int num_pages; if (geteuid() != 0) { ERROR("Swap can only be manipulated by root\n"); exit(EXIT_FAILURE); } pid = getpid(); snprintf(path, PATH_MAX, "%s/swap/temp", MOUNT_DIR); snprintf(file, PATH_MAX, "%s/swapfile-%ld", path, pid); /* swapsize is 5 hugepages */ if (opt_temp_swap == -1) num_pages = 5; else num_pages = opt_temp_swap; swap_size = num_pages * page_size; if (ensure_dir(path, S_IRWXU | S_IRGRP | S_IXGRP, 0, 0)) exit(EXIT_FAILURE); if (opt_dry_run) { printf("dd bs=1024 count=%ld if=/dev/zero of=%s\n", swap_size / 1024, file); printf("mkswap %s\nswapon %s\n", file, file); return; } f = fopen(file, "wx"); if (!f) { WARNING("Couldn't open %s: %s\n", file, strerror(errno)); opt_temp_swap = 0; return; } buf = malloc(swap_size); memset(buf, 0, swap_size); fwrite(buf, sizeof(char), swap_size, f); free(buf); fclose(f); snprintf(mkswap_cmd, PATH_MAX, "mkswap %s", file); ret = system(mkswap_cmd); if (WIFSIGNALED(ret)) { WARNING("Call to mkswap failed\n"); opt_temp_swap = 0; return; } else if (WIFEXITED(ret)) { ret = WEXITSTATUS(ret); if (ret) { WARNING("Call to mkswap failed\n"); opt_temp_swap = 0; return; } } DEBUG("swapon %s\n", file); if (swapon(file, 0)) { WARNING("swapon on %s failed: %s\n", file, strerror(errno)); opt_temp_swap = 0; } }
void create_mounts(char *user, char *group, char *base, mode_t mode) { struct hpage_pool pools[MAX_POOLS]; char path[PATH_MAX]; char options[OPT_MAX]; char limits[OPT_MAX]; char scaled[OPT_MAX]; int cnt, pos; struct passwd *pwd; struct group *grp; uid_t uid = 0; gid_t gid = 0; if (geteuid() != 0) { ERROR("Mounts can only be created by root\n"); exit(EXIT_FAILURE); } if (user) { pwd = getpwnam(user); if (!pwd) { ERROR("Could not find specified user %s\n", user); exit(EXIT_FAILURE); } uid = pwd->pw_uid; } else if (group) { grp = getgrnam(group); if (!grp) { ERROR("Could not find specified group %s\n", group); exit(EXIT_FAILURE); } gid = grp->gr_gid; } if (ensure_dir(base, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH, 0, 0)) exit(EXIT_FAILURE); cnt = hpool_sizes(pools, MAX_POOLS); if (cnt < 0) { ERROR("Unable to obtain pools list\n"); exit(EXIT_FAILURE); } for (pos=0; cnt--; pos++) { scaled[0] = 0; scale_size(scaled, pools[pos].pagesize); if (user) snprintf(path, PATH_MAX, "%s/%s/pagesize-%s", base, user, scaled); else if (group) snprintf(path, PATH_MAX, "%s/%s/pagesize-%s", base, group, scaled); else snprintf(path, PATH_MAX, "%s/pagesize-%s", base, scaled); snprintf(options, OPT_MAX, "pagesize=%ld", pools[pos].pagesize); /* Yes, this could be cleverer */ if (opt_limit_mount_size && opt_limit_mount_inodes) snprintf(limits, OPT_MAX, ",size=%lu,nr_inodes=%d", opt_limit_mount_size, opt_limit_mount_inodes); else { if (opt_limit_mount_size) snprintf(limits, OPT_MAX, ",size=%lu", opt_limit_mount_size); if (opt_limit_mount_inodes) snprintf(limits, OPT_MAX, ",nr_inodes=%d", opt_limit_mount_inodes); } /* Append limits if specified */ if (limits[0] != 0) { size_t maxlen = OPT_MAX - strlen(options); if (maxlen > strlen(limits)) strcat(options, limits); else WARNING("String limitations met, cannot append limitations onto mount options string. Increase OPT_MAX"); } if (ensure_dir(path, mode, uid, gid)) exit(EXIT_FAILURE); if (mount_dir(path, options, mode, uid, gid)) exit(EXIT_FAILURE); } }