Ejemplo n.º 1
0
int main(void)
{
	printf("\n");

//工作矩阵的高度为8
	header.height = 8;
	//工作矩阵的宽度为8
	header.width = 8;
	//创建一个用于运算的工作矩阵
	M = matrix_2d_create(header.height,header.width);
	if (M==NULL)
		//如果工作矩阵分配不成功,退出
		exit(1);
	//将实验小波系数转存到工作矩阵中
	load_data(M);   
#ifdef debug
	matrix_2d_write(M);
#endif
	//准备编码后的数据流输出的文件
	header.threshold = 1 << (int)( floor (log10 (matrix_2d_abs_max(M)) / log10(2) ));
	if ((ezw_file=fopen("out.ezw","w"))==NULL)
	{
		printf("Could not open output file.\n");
		//如果输出文件没有打开,退出
		exit(1);
	};
	//往输出文件中写入文件头
	fwrite(&header,sizeof(header),1,ezw_file);
	//进行EZW编码运算
	zeroes = 0;     //初始数据流中没有比特位,所以置0
	ones = 0;
	//初始输出字节数为0
	output_byte = 0;
	//最高位为1的标志位
	mask = 0x80;
	//将工作矩阵作为输入,给定阈值进行编码
	EZW_code(M,header.threshold);
	//刷新输出区域
	if (mask!=0)
	{
		fwrite(&output_byte,sizeof(output_byte),1,ezw_file);
	}
//#ifdef debug
	printf("\n");
	printf(" bits: %d, %d zeroes, %d ones.\n",zeroes+ones,zeroes,ones);
//#endif
	//清除所有的临时文件和内存占用
	fclose(ezw_file);
	//释放工作矩阵所占的空间
	matrix_2d_destroy(M);
	//释放主表扫描过程中的临时数据
	destroy_fifo();
	//清除零树扫描过程中的临时数据
	destroy_list();
	//返回
	return 0;

}
Ejemplo n.º 2
0
static int
run_walkdir(const char *topdir)
{
	int	rv;

	if (create_fifo() == -1) {
		return (-1);
	}
	rv = walkdir(topdir);
	destroy_fifo();
	return (rv);
}
Ejemplo n.º 3
0
static void mod_destroy(void)
{
	struct ctrl_socket* cs;
	
	/* close all the opened fds & unlink the files */
	for (cs=ctrl_sock_lst; cs; cs=cs->next){
		switch(cs->transport){
			case UNIXS_SOCK:
			case UNIXD_SOCK:
				close(cs->fd);
				cs->fd=-1;
				if (cs->write_fd!=-1){
					close(cs->write_fd);
					cs->write_fd=-1;
				}
				if (cs->name){
					if (unlink(cs->name)<0){
						LOG(L_ERR, "ERROR: ctl: could not delete unix"
									" socket %s: %s (%d)\n",
									cs->name, strerror(errno), errno);
					}
				}
				break;
#ifdef USE_FIFO
			case FIFO_SOCK:
				destroy_fifo(cs->fd, cs->write_fd, cs->name);
				break;
#endif
			default:
				close(cs->fd);
				cs->fd=-1;
				if (cs->write_fd!=-1){
					close(cs->write_fd);
					cs->write_fd=-1;
				}
		}
	}
	if (listen_lst){
		free_id_list(listen_lst);
		listen_lst=0;
	}
	if (ctrl_sock_lst){
		free_ctrl_socket_list(ctrl_sock_lst);
		ctrl_sock_lst=0;
	}
}
Ejemplo n.º 4
0
Archivo: main.c Proyecto: OPSF/uClinux
/* call it before exiting; if show_status==1, mem status is displayed */
void cleanup(show_status)
{
	/*clean-up*/
	if (mem_lock) 
		shm_unlock(); /* hack: force-unlock the shared memory lock in case
					 some process crashed and let it locked; this will 
					 allow an almost gracious shutdown */
	destroy_modules();
#ifdef USE_TCP
	destroy_tcp();
#endif
#ifdef USE_TLS
	destroy_tls();
#endif
	destroy_timer();
	close_unixsock_server();
	destroy_fifo();
	destroy_script_cb();
#ifdef PKG_MALLOC
	if (show_status){
		LOG(memlog, "Memory status (pkg):\n");
		pkg_status();
	}
#endif
#ifdef SHM_MEM
	if (pt) shm_free(pt);
	pt=0;
	if (show_status){
			LOG(memlog, "Memory status (shm):\n");
			shm_status();
	}
	/* zero all shmem alloc vars that we still use */
	shm_mem_destroy();
#endif
	if (pid_file) unlink(pid_file);
	if (pgid_file) unlink(pgid_file);
}
Ejemplo n.º 5
0
static void mod_destroy(void)
{
	destroy_fifo();
}
Ejemplo n.º 6
0
int proxy_server()
{
    int rx_bytes = 0, post_data_size = 0;
    int lsock = 0, csock = 0, client_data_size = 0;
    int result_size = 0, err_code = 0, id = 0;
    int addrlen = sizeof(struct sockaddr_in);
    struct sockaddr_in clientaddr;
    char *buffer = NULL, *post_data = NULL;
    char *host = NULL, *url = NULL;
    char *query = NULL, *fifo_file = NULL;
    char *get_data = NULL;
    char *client_data = NULL, *headers = NULL;
    char *server_ip = NULL;

    memset((void *) &clientaddr,0,addrlen);

    server_ip = config_get_server_ip();

    /* Create TCP socket */
    if((lsock = create_socket(server_ip,PROXY_PORT,SOCK_STREAM)) == SOCK_FAIL) {
        glog("Failed to create TCP socket for proxy server",LOG_ERROR_TYPE);
        if(server_ip) free(server_ip);
        return EXIT_FAILURE;
    }
    if(server_ip) free(server_ip);

    /* Infinite receive loop */
    while(1) {

        /* Accept incoming connection */
        if((csock = accept(lsock,(struct sockaddr *) &clientaddr,(socklen_t *) &addrlen)) < 0) {
            glog("Failed to accept TCP connection to proxy server",LOG_ERROR_TYPE);
            if(buffer) free(buffer);
            return EXIT_FAILURE;
        }

        if(!fork()) {
            /* Receive client request */
            if((buffer = receive(lsock,SOCK_STREAM,&rx_bytes,csock,&clientaddr)) == NULL) {
                glog("Failed to read data from client request sent to proxy server",LOG_ERROR_TYPE);
                exit(EXIT_FAILURE);
            }

            if(is_using_proxy(buffer)) {

                /* Get the target's IP address */
                host = get_host_name(buffer);

                /* Get the target URL path */
                url = get_url(buffer);

                /* Get POST data, if any */
                post_data = get_post_data(buffer,rx_bytes,&post_data_size);

                /* Get HTTP headers from request */
                headers = get_headers(buffer);

                /* If the CONSOLE_HOST is requested, then display the Web console interface */
                if(memcmp(host,CONSOLE_HOST,CONSOLE_HOST_SIZE) == 0) {
                    show_web_ui(csock,url);
                    close_socket(csock);
                } else {

                    /* Make sure the requested host is in our clients list */
                    query = sqlite3_mprintf("SELECT id FROM %s WHERE strftime('%%s',callback_time) >= strftime('%%s','now') AND ip = %Q LIMIT 1",CLIENTS_TABLE,host);
                    sql_exec(query,&result_size,&err_code);
                    sqlite3_free(query);

                    if(result_size > 0) {
                        /* Don't allow requests for filtered file extensions */
                        if(!is_url_filtered(url)) {

                            fifo_file = create_fifo(host);
                            if(!fifo_file) {
                                glog("Failed to create fifo file",LOG_ERROR_TYPE);
                            } else {
                                /* Insert query into queue table */
                                query = sqlite3_mprintf("INSERT INTO %s (fifo,host,url,headers,pdata,sent) VALUES (%Q,%Q,%Q,%Q,%Q,0)",QUEUE_TABLE,fifo_file,host,url,headers,post_data);
                                sql_exec(query,&result_size,&err_code);
                                sqlite3_free(query);
                                if(err_code != SQLITE_OK) {
                                    sql_log_error();
                                } else {

                                    /* When the client data has returned, the callback server will write the ID of the callback to the FIFO */
                                    id = read_from_fifo(fifo_file);

                                    /* Extract the data from the DB */
                                    get_data = sqlite3_mprintf("SELECT rdata FROM %s WHERE id = '%d'",QUEUE_TABLE,id);
                                    client_data = sql_exec(get_data,&client_data_size,&err_code);
                                    sqlite3_free(get_data);

                                    if(err_code != SQLITE_OK) {
                                        sql_log_error();
                                    } else {

                                        /* Write data to socket */
                                        if(write(csock,client_data,client_data_size) != client_data_size) {
                                            glog("Proxy socket write failed",LOG_ERROR_TYPE);
                                        }
                                    }
                                    if(client_data) free(client_data);

                                    /* Make sure the fifo gets deleted */
                                    destroy_fifo(fifo_file);
                                }
                            }
                        }
                    }
                }
            }

            /* Exit the child process */
            close_socket(csock);
            if(fifo_file) free(fifo_file);
            if(buffer) free(buffer);
            if(host) free(host);
            if(url) free(url);
            if(post_data) free(post_data);
            if(headers) free(headers);
            exit(EXIT_SUCCESS);
        }
    }

    /* Close up shop */
    close_socket(csock);
    close_socket(lsock);

    return EXIT_FAILURE;
}