Esempio n. 1
0
/** @copydoc socket_command_struct::handle_func */
void socket_command_file_update(uint8_t *data, size_t len, size_t pos)
{
    char filename[MAX_BUF];
    unsigned long ucomp_len;
    unsigned char *dest;
    FILE *fp;

    if (file_updates_requested != 0) {
        file_updates_requested--;
    }

    packet_to_string(data, len, &pos, filename, sizeof(filename));
    ucomp_len = packet_to_uint32(data, len, &pos);
    len -= pos;

    /* Uncompress it. */
    dest = emalloc(ucomp_len);
    uncompress((Bytef *) dest, (uLongf *) & ucomp_len, (const Bytef *) data + pos, (uLong) len);
    data = dest;
    len = ucomp_len;

    fp = path_fopen(filename, "wb");

    if (!fp) {
        LOG(BUG, "Could not open file '%s' for writing.", filename);
        efree(dest);
        return;
    }

    /* Update the file. */
    fwrite(data, 1, len, fp);
    fclose(fp);
    efree(dest);
}
Esempio n. 2
0
bool path_read_binary(const string& path, vector<uint8_t>& binary)
{
	/* read binary file into memory */
	FILE *f = path_fopen(path, "rb");

	if(!f) {
		binary.resize(0);
		return false;
	}

	binary.resize(path_file_size(path));

	if(binary.size() == 0) {
		fclose(f);
		return false;
	}

	if(fread(&binary[0], sizeof(uint8_t), binary.size(), f) != binary.size()) {
		fclose(f);
		return false;
	}

	fclose(f);

	return true;
}
Esempio n. 3
0
/**
 * Parse the updates srv file, and request updated files as needed.
 */
void file_updates_parse(void)
{
    FILE *fp;
    char buf[HUGE_BUF];

    /* Is the feature disabled? */
    if (setting_get_int(OPT_CAT_CLIENT, OPT_DISABLE_FILE_UPDATES)) {
        return;
    }

    fp = server_file_open_name(SERVER_FILE_UPDATES);

    if (!fp) {
        return;
    }

    while (fgets(buf, sizeof(buf) - 1, fp)) {
        char filename[MAX_BUF], crc_buf[MAX_BUF], *contents;
        uint64_t size;
        size_t st_size, numread;
        FILE *fp2;
        unsigned long crc;
        struct stat sb;

        if (sscanf(buf, "%s %"PRIu64 " %s", filename, &size, crc_buf) != 3) {
            continue;
        }

        fp2 = path_fopen(filename, "rb");

        /* No such file? Then we'll want to update this. */
        if (!fp2) {
            file_updates_request(filename);
            continue;
        }

        fstat(fileno(fp2), &sb);
        st_size = sb.st_size;
        contents = emalloc(st_size);
        numread = fread(contents, 1, st_size, fp2);
        fclose(fp2);

        /* Get the CRC32... */
        crc = crc32(1L, (const unsigned char FAR *) contents, numread);
        efree(contents);

        /* If the checksum or the size doesn't match, we'll want to update it.
         * */
        if (crc != strtoul(crc_buf, NULL, 16) || st_size != (size_t) size) {
            file_updates_request(filename);
        }
    }

    fclose(fp);
}
Esempio n. 4
0
int ipc_sem_get_limits(struct ipc_limits *lim)
{
	FILE *f;
	int rc = 0;

	lim->semvmx = SEMVMX;

	f = path_fopen("r", 0, _PATH_PROC_IPC_SEM);
	if (f) {
		rc = fscanf(f, "%d\t%d\t%d\t%d",
		       &lim->semmsl, &lim->semmns, &lim->semopm, &lim->semmni);
		fclose(f);

	}

	if (rc == 4) {
		struct seminfo seminfo;
		union semun arg = { .array = (ushort *) &seminfo };

		if (semctl(0, 0, IPC_INFO, arg) < 0)
			return 1;
		lim->semmni = seminfo.semmni;
		lim->semmsl = seminfo.semmsl;
		lim->semmns = seminfo.semmns;
		lim->semopm = seminfo.semopm;
	}

	return 0;
}

int ipc_shm_get_limits(struct ipc_limits *lim)
{
	lim->shmmin = SHMMIN;

	if (path_exist(_PATH_PROC_IPC_SHMALL) &&
	    path_exist(_PATH_PROC_IPC_SHMMAX) &&
	    path_exist(_PATH_PROC_IPC_SHMMNI)) {

		lim->shmall = path_read_u64(_PATH_PROC_IPC_SHMALL);
		lim->shmmax = path_read_u64(_PATH_PROC_IPC_SHMMAX);
		lim->shmmni = path_read_u64(_PATH_PROC_IPC_SHMMNI);

	} else {
		struct shminfo shminfo;

		if (shmctl(0, IPC_INFO, (struct shmid_ds *) &shminfo) < 0)
			return 1;
		lim->shmmni = shminfo.shmmni;
		lim->shmall = shminfo.shmall;
	}

	return 0;
}
Esempio n. 5
0
bool path_write_binary(const string& path, const vector<uint8_t>& binary)
{
	path_create_directories(path);

	/* write binary file from memory */
	FILE *f = path_fopen(path, "wb");

	if(!f)
		return false;

	if(binary.size() > 0)
		fwrite(&binary[0], sizeof(uint8_t), binary.size(), f);

	fclose(f);

	return true;
}
Esempio n. 6
0
/**
 * Save the blacklist data to file.
 * @param list
 * The music list.
 */
static void mplayer_blacklist_save(list_struct *list)
{
    FILE *fp;
    size_t row;

    fp = path_fopen(FILE_MPLAYER_BLACKLIST, "w");

    if (fp == NULL) {
        LOG(ERROR, "Failed to open file: %s",
                FILE_MPLAYER_BLACKLIST);
        return;
    }

    for (row = 0; row < list->rows; row++) {
        if (shuffle_blacklist[row]) {
            fprintf(fp, "%s\n", list->text[row][0]);
        }
    }

    fclose(fp);
}
Esempio n. 7
0
bool MD5Hash::append_file(const string& filepath)
{
	FILE *f = path_fopen(filepath, "rb");

	if(!f) {
		fprintf(stderr, "MD5: failed to open file %s\n", filepath.c_str());
		return false;
	}

	const size_t buffer_size = 1024;
	uint8_t buffer[buffer_size];
	size_t n;

	do {
		n = fread(buffer, 1, buffer_size, f);
		append(buffer, n);
	} while(n == buffer_size);

	bool success = (ferror(f) == 0);

	fclose(f);
	
	return success;
}
Esempio n. 8
0
int ipc_shm_get_info(int id, struct shm_data **shmds)
{
	FILE *f;
	int i, maxid;
	struct shm_data *p;
	struct shm_info dummy;

	p = *shmds = xcalloc(1, sizeof(struct shm_data));
	p->next = NULL;

	f = path_fopen("r", 0, _PATH_PROC_SYSV_SHM);
	if (!f)
		goto fallback;

	while (fgetc(f) != '\n');		/* skip header */

	while (feof(f) == 0) {
		if (fscanf(f,
			  "%d %d  %o %"SCNu64 " %u %u  "
			  "%"SCNu64 " %u %u %u %u %"SCNu64 " %"SCNu64 " %"SCNu64
			  " %"SCNu64 " %"SCNu64 "\n",
			   &p->shm_perm.key,
			   &p->shm_perm.id,
			   &p->shm_perm.mode,
			   &p->shm_segsz,
			   &p->shm_cprid,
			   &p->shm_lprid,
			   &p->shm_nattch,
			   &p->shm_perm.uid,
			   &p->shm_perm.gid,
			   &p->shm_perm.cuid,
			   &p->shm_perm.cgid,
			   &p->shm_atim,
			   &p->shm_dtim,
			   &p->shm_ctim,
			   &p->shm_rss,
			   &p->shm_swp) != 16)
			continue;

		if (id > -1) {
			/* ID specified */
			if (id == p->shm_perm.id) {
				i = 1;
				break;
			} else
				continue;
		}

		p->next = xcalloc(1, sizeof(struct shm_data));
		p = p->next;
		p->next = NULL;
		i++;
	}

	if (i == 0)
		free(*shmds);
	fclose(f);
	return i;

	/* Fallback; /proc or /sys file(s) missing. */
fallback:
	i = id < 0 ? 0 : id;

	maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) &dummy);
	if (maxid < 0)
		return 0;

	while (i <= maxid) {
		int shmid;
		struct shmid_ds shmseg;
		struct ipc_perm *ipcp = &shmseg.shm_perm;

		shmid = shmctl(i, SHM_STAT, &shmseg);
		if (shmid < 0) {
			if (-1 < id) {
				free(*shmds);
				return 0;
			}
			i++;
			continue;
		}

		p->shm_perm.key = ipcp->KEY;
		p->shm_perm.id = shmid;
		p->shm_perm.mode = ipcp->mode;
		p->shm_segsz = shmseg.shm_segsz;
		p->shm_cprid = shmseg.shm_cpid;
		p->shm_lprid = shmseg.shm_lpid;
		p->shm_nattch = shmseg.shm_nattch;
		p->shm_perm.uid = ipcp->uid;
		p->shm_perm.gid = ipcp->gid;
		p->shm_perm.cuid = ipcp->cuid;
		p->shm_perm.cgid = ipcp->cuid;
		p->shm_atim = shmseg.shm_atime;
		p->shm_dtim = shmseg.shm_dtime;
		p->shm_ctim = shmseg.shm_ctime;
		p->shm_rss = 0xdead;
		p->shm_swp = 0xdead;

		if (id < 0) {
			p->next = xcalloc(1, sizeof(struct shm_data));
			p = p->next;
			p->next = NULL;
			i++;
		} else
			return 1;
	}

	return i;
}
Esempio n. 9
0
int ipc_msg_get_info(int id, struct msg_data **msgds)
{
	FILE *f;
	int i, maxid;
	struct msg_data *p;
	struct msqid_ds dummy;
	struct msqid_ds msgseg;

	p = *msgds = xcalloc(1, sizeof(struct msg_data));
	p->next = NULL;

	f = path_fopen("r", 0, _PATH_PROC_SYSV_MSG);
	if (!f)
		goto msg_fallback;

	while (fgetc(f) != '\n') ;	/* skip header */

	while (feof(f) == 0) {
		if (fscanf(f,
			   "%d %d  %o  %" SCNu64 " %" SCNu64 " %u %u %u %u %u %u %" SCNu64 " %" SCNu64 " %" SCNu64 "\n",
			   &p->msg_perm.key,
			   &p->msg_perm.id,
			   &p->msg_perm.mode,
			   &p->q_cbytes,
			   &p->q_qnum,
			   &p->q_lspid,
			   &p->q_lrpid,
			   &p->msg_perm.uid,
			   &p->msg_perm.gid,
			   &p->msg_perm.cuid,
			   &p->msg_perm.cgid,
			   &p->q_stime,
			   &p->q_rtime,
			   &p->q_ctime) != 14)
			continue;

		if (id > -1) {
			/* ID specified */
			if (id == p->msg_perm.id) {
				/*
				 * FIXME: q_qbytes are not in /proc
				 *
				 */
				if (msgctl(id, IPC_STAT, &msgseg) != -1)
					p->q_qbytes = msgseg.msg_qbytes;
				i = 1;
				break;
			} else
				continue;
		}

		p->next = xcalloc(1, sizeof(struct msg_data));
		p = p->next;
		p->next = NULL;
		i++;
	}

	if (i == 0)
		free(*msgds);
	fclose(f);
	return i;

	/* Fallback; /proc or /sys file(s) missing. */
 msg_fallback:
	i = id < 0 ? 0 : id;

	maxid = msgctl(id, MSG_STAT, &dummy);
	if (maxid < 0)
		return 0;

	while (i <= maxid) {
		int msgid;
		struct ipc_perm *ipcp = &msgseg.msg_perm;

		msgid = msgctl(i, MSG_STAT, &msgseg);
		if (msgid < 0) {
			if (-1 < id) {
				free(*msgds);
				return 0;
			}
			i++;
			continue;
		}

		p->msg_perm.key = ipcp->KEY;
		p->msg_perm.id = msgid;
		p->msg_perm.mode = ipcp->mode;
		p->q_cbytes = msgseg.msg_cbytes;
		p->q_qnum = msgseg.msg_qnum;
		p->q_lspid = msgseg.msg_lspid;
		p->q_lrpid = msgseg.msg_lrpid;
		p->msg_perm.uid = ipcp->uid;
		p->msg_perm.gid = ipcp->gid;
		p->msg_perm.cuid = ipcp->cuid;
		p->msg_perm.cgid = ipcp->cgid;
		p->q_stime = msgseg.msg_stime;
		p->q_rtime = msgseg.msg_rtime;
		p->q_ctime = msgseg.msg_ctime;
		p->q_qbytes = msgseg.msg_qbytes;

		if (id < 0) {
			p->next = xcalloc(1, sizeof(struct msg_data));
			p = p->next;
			p->next = NULL;
			i++;
		} else
			return 1;
	}

	return i;
}
Esempio n. 10
0
static void get_sem_elements(struct sem_data *p)
{
	size_t i;

	if (!p || !p->sem_nsems || p->sem_perm.id < 0)
		return;

	p->elements = xcalloc(p->sem_nsems, sizeof(struct sem_elem));

	for (i = 0; i < p->sem_nsems; i++) {
		struct sem_elem *e = &p->elements[i];
		union semun arg = { .val = 0 };

		e->semval = semctl(p->sem_perm.id, i, GETVAL, arg);
		if (e->semval < 0)
			err(EXIT_FAILURE, _("%s failed"), "semctl(GETVAL)");

		e->ncount = semctl(p->sem_perm.id, i, GETNCNT, arg);
		if (e->ncount < 0)
			err(EXIT_FAILURE, _("%s failed"), "semctl(GETNCNT)");

		e->zcount = semctl(p->sem_perm.id, i, GETZCNT, arg);
		if (e->zcount < 0)
			err(EXIT_FAILURE, _("%s failed"), "semctl(GETZCNT)");

		e->pid = semctl(p->sem_perm.id, i, GETPID, arg);
		if (e->pid < 0)
			err(EXIT_FAILURE, _("%s failed"), "semctl(GETPID)");
	}
}

int ipc_sem_get_info(int id, struct sem_data **semds)
{
	FILE *f;
	int i, maxid;
	struct sem_data *p;
	struct seminfo dummy;
	union semun arg;

	p = *semds = xcalloc(1, sizeof(struct sem_data));
	p->next = NULL;

	f = path_fopen("r", 0, _PATH_PROC_SYSV_SEM);
	if (!f)
		goto sem_fallback;

	while (fgetc(f) != '\n') ;	/* skip header */

	while (feof(f) == 0) {
		if (fscanf(f,
			   "%d %d  %o %" SCNu64 " %u %u %u %u %" SCNu64 " %" SCNu64 "\n",
			   &p->sem_perm.key,
			   &p->sem_perm.id,
			   &p->sem_perm.mode,
			   &p->sem_nsems,
			   &p->sem_perm.uid,
			   &p->sem_perm.gid,
			   &p->sem_perm.cuid,
			   &p->sem_perm.cgid,
			   &p->sem_otime,
			   &p->sem_ctime) != 10)
			continue;

		if (id > -1) {
			/* ID specified */
			if (id == p->sem_perm.id) {
				get_sem_elements(p);
				i = 1;
				break;
			} else
				continue;
		}

		p->next = xcalloc(1, sizeof(struct sem_data));
		p = p->next;
		p->next = NULL;
		i++;
	}

	if (i == 0)
		free(*semds);
	fclose(f);
	return i;

	/* Fallback; /proc or /sys file(s) missing. */
 sem_fallback:
	i = id < 0 ? 0 : id;

	arg.array = (ushort *) (void *)&dummy;
	maxid = semctl(0, 0, SEM_INFO, arg);
	if (maxid < 0)
		return 0;

	while (i <= maxid) {
		int semid;
		struct semid_ds semseg;
		struct ipc_perm *ipcp = &semseg.sem_perm;
		arg.buf = (struct semid_ds *)&semseg;

		semid = semctl(i, 0, SEM_STAT, arg);
		if (semid < 0) {
			if (-1 < id) {
				free(*semds);
				return 0;
			}
			i++;
			continue;
		}

		p->sem_perm.key = ipcp->KEY;
		p->sem_perm.id = semid;
		p->sem_perm.mode = ipcp->mode;
		p->sem_nsems = semseg.sem_nsems;
		p->sem_perm.uid = ipcp->uid;
		p->sem_perm.gid = ipcp->gid;
		p->sem_perm.cuid = ipcp->cuid;
		p->sem_perm.cgid = ipcp->cuid;
		p->sem_otime = semseg.sem_otime;
		p->sem_ctime = semseg.sem_ctime;

		if (id < 0) {
			p->next = xcalloc(1, sizeof(struct sem_data));
			p = p->next;
			p->next = NULL;
			i++;
		} else {
			get_sem_elements(p);
			return 1;
		}
	}

	return i;
}
Esempio n. 11
0
/** @copydoc widgetdata::draw_func */
static void widget_draw(widgetdata *widget)
{
    SDL_Rect box;
    char buf[HUGE_BUF];
    size_t i;

    /* The list doesn't exist yet, create it. */
    if (!list_mplayer) {
        char version[MAX_BUF];

        /* Create the list and set up settings. */
        list_mplayer = list_create(12, 1, 8);
        list_mplayer->handle_enter_func = list_handle_enter;
        list_mplayer->text_color_hook = list_text_color_hook;
        list_mplayer->surface = widget->surface;
        list_scrollbar_enable(list_mplayer);
        list_set_column(list_mplayer, 0, 130, 7, NULL, -1);
        list_set_font(list_mplayer, FONT_ARIAL10);

        /* Add default media directory songs. */
        get_data_dir_file(buf, sizeof(buf), DIRECTORY_MEDIA);
        mplayer_list_init(list_mplayer, buf, 0);

        /* Now add custom ones, but ignore duplicates. */
        snprintf(buf, sizeof(buf), "%s/.atrinik/%s/"DIRECTORY_MEDIA, get_config_dir(), package_get_version_partial(version, sizeof(version)));
        mplayer_list_init(list_mplayer, buf, 1);

        /* If we added any, sort the list alphabetically and add an entry
         * to disable background music. */
        if (list_mplayer->rows) {
            FILE *fp;

            /* Allocate the blacklist. + 1 is for the last entry added
             * further down. It is not actually used by the blacklist as
             * it's not possible to toggle it on/off using the button, but
             * it simplifies other logic checks. */
            shuffle_blacklist = ecalloc(1, sizeof(*shuffle_blacklist) * (list_mplayer->rows + 1));

            /* Sort the list. */
            list_sort(list_mplayer, LIST_SORT_ALPHA);

            /* Read the blacklist file contents. */
            fp = path_fopen(FILE_MPLAYER_BLACKLIST, "r");

            if (fp) {
                size_t row;

                while (fgets(buf, sizeof(buf) - 1, fp)) {
                    for (row = 0; row < list_mplayer->rows; row++) {
                        if (!strncmp(buf, list_mplayer->text[row][0], strlen(buf) - 1)) {
                            shuffle_blacklist[row] = 1;
                            break;
                        }
                    }
                }

                fclose(fp);
            }

            list_add(list_mplayer, list_mplayer->rows, 0, "Disable music");
        }

        scrollbar_create(&scrollbar_progress, 130, 11, &scrollbar_progress_info.scroll_offset, &scrollbar_progress_info.num_lines, 1);
        scrollbar_progress.redraw = &scrollbar_progress_info.redraw;
    }

    if (widget->redraw) {
        const char *bg_music;

        box.h = 0;
        box.w = widget->w;
        text_show(widget->surface, FONT_SERIF12, "Music Player", 0, 3, COLOR_HGOLD, TEXT_ALIGN_CENTER, &box);
        list_set_parent(list_mplayer, widget->x, widget->y);
        list_show(list_mplayer, 10, 2);
        box.w /= 2;
        text_show(widget->surface, FONT_SANS10, "Currently playing:", widget->w / 2, 22, COLOR_WHITE, TEXT_ALIGN_CENTER, &box);

        bg_music = sound_get_bg_music_basename();
        box.h = 0;
        box.w = widget->w / 2;

        /* Store the background music file name in temporary buffer and
         * make sure it won't overflow by truncating it if necessary. */
        if (bg_music) {
            strncpy(buf, bg_music, sizeof(buf) - 1);
            buf[sizeof(buf) - 1] = '\0';
            text_truncate_overflow(FONT_SANS11, buf, 150);
        }

        /* Show the music that is being played. */
        text_show(widget->surface, FONT_SANS11, bg_music ? buf : "No music", widget->w / 2 - 5, 34, COLOR_HGOLD, TEXT_ALIGN_CENTER, &box);

        scrollbar_progress.px = widget->x;
        scrollbar_progress.py = widget->y;
        scrollbar_show(&scrollbar_progress, widget->surface, 170, 50);

        box.h = 120;
        box.w -= 6 * 2;
        text_show(widget->surface, FONT_ARIAL10, "You can use the music player to play your favorite tunes from the game, or play them all one-by-one in random order (shuffle).\n\nNote that if you use the music player, in-game areas won't change your music until you click [b]Stop[/b].", widget->w / 2 + 6, 62, COLOR_WHITE, TEXT_WORD_WRAP | TEXT_MARKUP, &box);

        for (i = 0; i < BUTTON_NUM; i++) {
            buttons[i].surface = widget->surface;
            button_set_parent(&buttons[i], widget->x, widget->y);
        }

        buttons[BUTTON_PLAY].x = 10;
        buttons[BUTTON_PLAY].y = widget->h - TEXTURE_CLIENT("button")->h - 4;
        button_show(&buttons[BUTTON_PLAY], sound_map_background(-1) ? "Stop" : "Play");

        buttons[BUTTON_SHUFFLE].x = 10 + TEXTURE_CLIENT("button")->w + 5;
        buttons[BUTTON_SHUFFLE].y = widget->h - TEXTURE_CLIENT("button")->h - 4;
        buttons[BUTTON_SHUFFLE].pressed_forced = shuffle;
        button_show(&buttons[BUTTON_SHUFFLE], "Shuffle");

        buttons[BUTTON_BLACKLIST].x = 10 + TEXTURE_CLIENT("button")->w * 2 + 5 * 2;
        buttons[BUTTON_BLACKLIST].y = widget->h - TEXTURE_CLIENT("button_round")->h - 5;
        buttons[BUTTON_BLACKLIST].disabled = list_mplayer->row_selected == list_mplayer->rows;
        button_show(&buttons[BUTTON_BLACKLIST], mplayer_blacklisted(list_mplayer) ? "+" : "-");

        /* Show close button. */
        buttons[BUTTON_CLOSE].x = widget->w - TEXTURE_CLIENT("button_round")->w - 4;
        buttons[BUTTON_CLOSE].y = 4;
        button_show(&buttons[BUTTON_CLOSE], "X");

        /* Show help button. */
        buttons[BUTTON_HELP].x = widget->w - TEXTURE_CLIENT("button_round")->w * 2 - 4;
        buttons[BUTTON_HELP].y = 4;
        button_show(&buttons[BUTTON_HELP], "?");
    }
}