struct list *get_workload_specs(const char *path) {
	
	struct list *specs = list_create();
	if(!specs) {
		fprintf(stderr, "Cannot allocate memory for creating list!\n");
		return NULL;
	}

	// Read in new configuration from file
	FILE *fp;

	fp = fopen(path, "r");
	if(!fp) {
		fprintf(stderr, "Failed to open workload specification file at %s.\n", path);
		return NULL;
	}

	char *line;
	int line_count = 0;
	while((line = get_line(fp))) {
		line_count++;

		string_chomp(line);
		if(string_isspace(line)) { // skip empty lines
			continue;
		}
		if(line[0] == '#') { // skip comment lines
			continue;
		}
		
		int submit, input, exe, output, num;
		if(sscanf(line, "%d %d %d %d %d", &submit, &input, &exe, &output, &num) == 5) {
			if(submit < 0 || input <=0 || exe <=0 || output <=0 || num <=0) {
				fprintf(stderr, "Other than the submit_time field, every other field should be greater than 0.\n");
				goto fail;
			}

			struct task_series *ts = (struct task_series *)xxmalloc(sizeof(struct task_series));
			ts->submit_time = submit;
			ts->input_size = input;
			ts->execution_time = exe;
			ts->output_size = output;
			ts->num_of_tasks = num;

			list_push_priority(specs, ts, ts->submit_time);
		} else {
			fprintf(stderr, "Line %d is invalid: %s\n", line_count, line);
			goto fail;
		}
	}
	fclose(fp);
	return specs;

fail:
	// delete list
	list_free(specs);
	list_delete(specs);
	fclose(fp);
	return NULL;
}
Exemplo n.º 2
0
int pfs_resolve_external( const char *logical_name, const char *prefix, const char *redirect, char *physical_name )
{
	char cmd[PFS_PATH_MAX];
	FILE *file;
	int result;

	sprintf(cmd,"%s %s",redirect,&logical_name[strlen(prefix)]);

	debug(D_RESOLVE,"external resolver: %s\n",cmd);

	file = popen(cmd,"r");
	if(!file) {
		debug(D_RESOLVE,"couldn't execute resolver %s: %s",cmd,strerror(errno));
		return PFS_RESOLVE_FAILED;
	}

	if(fgets(physical_name,PFS_PATH_MAX,file)) {
		string_chomp(physical_name);
	} else {
		physical_name[0] = 0;
	}

	result = pclose(file);

	if(result==0) {
		return PFS_RESOLVE_CHANGED;
	} else {
		return PFS_RESOLVE_FAILED;
	}
}
Exemplo n.º 3
0
static char * variable_get( const char *name, int line, int withquotes )
{
	char buffer[100];
	int arg;

	if(!strcmp(name,"$")) {
		sprintf(buffer,"%d",(int)getpid());
		return xxstrdup(buffer);
	} else if(!strcmp(name,"#")) {
		sprintf(buffer,"%d",head->argc-1);
		return xxstrdup(buffer);
	} else if(!strcmp(name,"@")) {
		return variable_print_argv(withquotes);
	} else if(!strcmp(name,"*")) {
		return variable_print_argv(0);
	} else if( sscanf(name,"%d",&arg)==1 ) {
		if(arg>=head->argc) {
			return xxstrdup("");
		} else {
			return xxstrdup(head->argv[arg]);
		}
	} else if( isvalid(name) ) {
		char *result = getenv(name);
		if(result) return xxstrdup(result);
		result = buffer_load(name);
		if(result) string_chomp(result);
		return result;
	} else {
		ftsh_fatal(line,"${%s} is an invalid variable name!",name);
		return 0;
	}
}
Exemplo n.º 4
0
User *input_create_user()
{
    User *user = user_new();
    printf("Name> ");

    fgets(user->name, USER_NAME_LENGTH, stdin);
    string_chomp(user->name);

    return user;
}
Exemplo n.º 5
0
/** Read multiple lines connected by shell continuation symbol
 * (backslash). Return as a single line. Caller will own the
 * memory.
 * The continuation is detected when backslash is the last symbol
 * before the newline character, and the symbol last before last is
 * not the backslash (double backslash is an escape sequence for
 * backslash itself).
 */
char* get_continued_line(struct lexer_book *bk) {

	char *cont_line = NULL;
	char *raw_line = NULL;
	int cont = 1;

	while(cont && ((raw_line=get_line(bk->stream)) != NULL)) {

		bk->column_number = 1;
		bk->line_number++;
		if(bk->line_number % 1000 == 0) {
			debug(D_DEBUG, "read line %ld\n", bk->line_number);
		}

		/* Strip whitespace */
		string_chomp(raw_line);
		while(isspace(*raw_line)) {
			raw_line++;
			bk->column_number++;
		}

		size_t len = strlen(raw_line);

		if(len>0 && raw_line[len-1] == '\\' &&
				(len==1 || raw_line[len-2] != '\\')) {
			raw_line[len-1] = '\0';
			cont++;
		}
		else {
			cont = 0;
		}

		if(cont_line) {
			cont_line = string_combine(cont_line,raw_line);
		}
		else {
			cont_line = xxstrdup(raw_line);
		}
	}
	if(cont > 1) {
		dag_parse_error(bk, "No line found after line continuation backslash");
		free(cont_line);
		cont_line = NULL;
		fatal("Unable to parse the makeflow file");
	}
	return cont_line;
}
Exemplo n.º 6
0
static void do_debug(int is_fatal, INT64_T flags, const char *fmt, va_list args)
{
	char newfmt[65536];
	char buffer[65536];
	int length;

	struct timeval tv;
	struct tm *tm;

	gettimeofday(&tv, 0);
	tm = localtime(&tv.tv_sec);

	sprintf(newfmt, "%04d/%02d/%02d %02d:%02d:%02d.%02ld [%d] %s: %s: %s", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (long) tv.tv_usec / 10000, (int) debug_getpid(), program_name,
		is_fatal ? "fatal " : flag_to_name(flags), fmt);

	vsprintf(buffer, newfmt, args);
	string_chomp(buffer);
	strcat(buffer, "\n");
	length = strlen(buffer);

	if(debug_file) {
		struct stat info;

		fstat(debug_fd, &info);
		if(info.st_size >= debug_file_size && debug_file_size != 0) {
			close(debug_fd);

			if(stat(debug_file, &info) == 0) {
				if(info.st_size >= debug_file_size) {
					char *newname = malloc(strlen(debug_file) + 5);
					sprintf(newname, "%s.old", debug_file);
					rename(debug_file, newname);
					free(newname);
				}
			}

			debug_fd = open(debug_file, O_CREAT | O_TRUNC | O_WRONLY, 0660);
			if(debug_fd == -1){
				debug_fd = STDERR_FILENO;
				fatal("could not open log file `%s': %s", debug_file, strerror(errno));
			}
		}
	}

	full_write(debug_fd, buffer, length);
}
Exemplo n.º 7
0
struct text_list *text_list_load(const char *path)
{
	char line[1024];

	FILE *file = fopen(path, "r");
	if(!file)
		return 0;

	struct text_list *t = text_list_create();

	while(fgets(line, sizeof(line), file)) {
		string_chomp(line);
		text_list_append(t, line);
	}

	fclose(file);

	return t;
}
Exemplo n.º 8
0
static int do_getline(const char *prompt, char *buffer, int length, int echo)
{
	int fd;
	char *result;
	FILE *stream;

	fd = open("/dev/tty", O_RDWR);
	if(fd < 0)
		return 0;

	if(!echo) {
		if(!setecho(fd, 0)) {
			close(fd);
			return 0;
		}
	}

	stream = fdopen(fd, "r+");
	if(!stream) {
		if(!echo) {
			setecho(fd, 1);
		}
		close(fd);
		return 0;
	}

	fprintf(stream, "%s", prompt);
	fflush(stream);

	result = fgets(buffer, length, stream);

	string_chomp(buffer);

	if(!echo) {
		fprintf(stream, "\n");
		fflush(stream);
		setecho(fd, 1);
	}

	fclose(stream);

	return result != 0;
}
Exemplo n.º 9
0
void task_complete( struct work_queue_task *t )
{
	FILE *output = stdout;
	if(output_filename)
	{
		output = fopen(output_filename, "a");
		if(!output)
		{
			fprintf(stderr, "Cannot open %s for writing. Output to stdout instead.\n", output_filename);
			output = stdout;
		}
	}

	string_chomp(t->output);
	fprintf(output, "%s\n",t->output);

	if(output != stdout)
		fclose(output);

	work_queue_task_delete(t);
}
Exemplo n.º 10
0
struct text_list *allpairs_remote_create(const char *path, const char *set)
{
    char line[1024];

    FILE *file = fopen(path, "r");
    if(!file)
        return 0;

    struct text_list *t = text_list_create();
    int i = 0;
    while(fgets(line, sizeof(line), file)) {
        string_chomp(line);
        char *name = string_format("%s_%d_%s", set, i, path_basename(line));
        text_list_append(t, name);
        i++;
    }

    fclose(file);

    return t;
}
Exemplo n.º 11
0
struct nvpair *catalog_query_read(struct catalog_query *q, time_t stoptime)
{
	struct nvpair *nv = nvpair_create();
	char line[65536];
	int lines = 0;

	while(link_readline(q->link, line, sizeof(line), stoptime)) {
		string_chomp(line);
		if(!line[0])
			break;
		nvpair_parse(nv, line);
		lines++;
	}

	if(lines) {
		return nv;
	} else {
		nvpair_delete(nv);
		return 0;
	}
}
Exemplo n.º 12
0
void work_queue_mainloop( struct work_queue *q )
{
	char line[1024];

	int sleep_time, run_time, input_size, output_size, count;

	while(1) {
		printf("work_queue_test > ");
		fflush(stdout);

		if(!fgets(line,sizeof(line),stdin)) break;

		if(line[0]=='#') continue;

		string_chomp(line);

		if(sscanf(line,"sleep %d",&sleep_time)==1) {
			printf("sleeping %d seconds...\n",sleep_time);
			sleep(sleep_time);
		} else if(!strcmp(line,"wait")) {
			printf("waiting for all tasks...\n");
			wait_for_all_tasks(q);
		} else if(sscanf(line, "submit %d %d %d %d",&input_size, &run_time, &output_size, &count)==4) {
			printf("submitting %d tasks...\n",count);
			submit_tasks(q,input_size,run_time,output_size,count);
		} else if(!strcmp(line,"quit") || !strcmp(line,"exit")) {
			break;
		} else if(!strcmp(line,"help")) {
			printf("Available commands are:\n");
			printf("sleep <n>               Sleep for n seconds.\n");
			printf("wait                    Wait for all submitted tasks to finish.\n");
			printf("submit <I> <T> <O> <N>  Submit N tasks that read I MB input,\n");
			printf("                        run for T seconds, and produce O MB of output.\n");
			printf("quit, exit              Wait for all tasks to complete, then exit.\n");
			printf("\n");
		} else {
			fprintf(stderr,"ignoring badly formatted line: %s\n",line);			continue;
		}
	}
}
Exemplo n.º 13
0
struct passwd *fgetpwent(FILE * file)
{
	static struct passwd p;
	static char line[1024];

	if(!fgets(line, sizeof(line), file))
		return 0;
	string_chomp(line);

	p.pw_name = strtok(line, ":");
	p.pw_passwd = strtok(0, ":");
	char *uid = strtok(0, ":");
	char *gid = strtok(0, ":");
	p.pw_gecos = strtok(0, ":");
	p.pw_dir = strtok(0, ":");
	p.pw_shell = strtok(0, ":");

	p.pw_uid = atoi(uid);
	p.pw_gid = atoi(gid);

	return &p;
}
Exemplo n.º 14
0
void pfs_resolve_file_config( const char *filename )
{
	FILE *file;
	char line[PFS_LINE_MAX];
	char prefix[PFS_LINE_MAX];
	char redirect[PFS_LINE_MAX];
	int fields;
	int linenum=0;

	file = fopen(filename,"r");
	if(!file) fatal("couldn't open mountfile %s: %s\n",filename,strerror(errno));

	while(1) {
		if(!fgets(line,sizeof(line),file)) {
			if(errno==EINTR) {
				continue;
			} else {
				break;
			}
		}
		linenum++;
		if(line[0]=='#') continue;
		string_chomp(line);
		if(!line[0]) continue;
		fields = sscanf(line,"%s %s",prefix,redirect);

		if(fields==0) {
			continue;
		} else if(fields<2) {
			fatal("%s has an error on line %d\n",filename,linenum);
		} else {
			add_mount_entry(prefix,redirect);
		}
	}

	fclose(file);
}
Exemplo n.º 15
0
struct link *http_query_size_via_proxy(const char *proxy, const char *urlin, const char *action, INT64_T * size, time_t stoptime, int cache_reload)
{
	char url[HTTP_LINE_MAX];
	char newurl[HTTP_LINE_MAX];
	char line[HTTP_LINE_MAX];
	char addr[LINK_ADDRESS_MAX];
	struct link *link;
	int save_errno;
	int response;
	char actual_host[HTTP_LINE_MAX];
	int actual_port;
	*size = 0;

	url_encode(urlin, url, sizeof(url));

	if(proxy && !strcmp(proxy, "DIRECT"))
		proxy = 0;

	if(proxy) {
		int fields = sscanf(proxy, "http://%[^:]:%d", actual_host, &actual_port);
		if(fields == 2) {
			/* host and port are good */
		} else if(fields == 1) {
			actual_port = HTTP_PORT;
		} else {
			debug(D_HTTP, "invalid proxy syntax: %s", proxy);
			return 0;
		}
	} else {
		int fields = sscanf(url, "http://%[^:]:%d", actual_host, &actual_port);
		if(fields != 2) {
			fields = sscanf(url, "http://%[^/]", actual_host);
			if(fields == 1) {
				actual_port = HTTP_PORT;
			} else {
				debug(D_HTTP, "malformed url: %s", url);
				return 0;
			}
		}
	}

	debug(D_HTTP, "connect %s port %d", actual_host, actual_port);
	if(!domain_name_lookup(actual_host, addr))
		return 0;

	link = link_connect(addr, actual_port, stoptime);
	if(!link) {
		errno = ECONNRESET;
		return 0;
	}


	{
		buffer_t B;

		buffer_init(&B);
		buffer_abortonfailure(&B, 1);

		buffer_printf(&B, "%s %s HTTP/1.1\r\n", action, url);
		if(cache_reload)
			buffer_putliteral(&B, "Cache-Control: max-age=0\r\n");
		buffer_putliteral(&B, "Connection: close\r\n");
		buffer_printf(&B, "Host: %s\r\n", actual_host);
		if(getenv("HTTP_USER_AGENT"))
			buffer_printf(&B, "User-Agent: Mozilla/5.0 (compatible; CCTools %d.%d.%s Parrot; http://www.nd.edu/~ccl/ %s)\r\n", CCTOOLS_VERSION_MAJOR, CCTOOLS_VERSION_MINOR, CCTOOLS_VERSION_MICRO, getenv("HTTP_USER_AGENT"));
		else
			buffer_printf(&B, "User-Agent: Mozilla/5.0 (compatible; CCTools %d.%d.%s Parrot; http://www.nd.edu/~ccl/)\r\n", CCTOOLS_VERSION_MAJOR, CCTOOLS_VERSION_MINOR, CCTOOLS_VERSION_MICRO);
		buffer_putliteral(&B, "\r\n"); /* header terminator */

		debug(D_HTTP, "%s", buffer_tostring(&B, NULL));
		link_putstring(link, buffer_tostring(&B, NULL), stoptime);

		buffer_free(&B);
	}

	if(link_readline(link, line, HTTP_LINE_MAX, stoptime)) {
		string_chomp(line);
		debug(D_HTTP, "%s", line);
		if(sscanf(line, "HTTP/%*d.%*d %d", &response) == 1) {
			newurl[0] = 0;
			while(link_readline(link, line, HTTP_LINE_MAX, stoptime)) {
				string_chomp(line);
				debug(D_HTTP, "%s", line);
				sscanf(line, "Location: %s", newurl);
				sscanf(line, "Content-Length: %" SCNd64, size);
				if(strlen(line) <= 2) {
					break;
				}
			}

			switch (response) {
			case 200:
				return link;
				break;
			case 301:
			case 302:
			case 303:
			case 307:
				link_close(link);
				if(newurl[0]) {
					if(!strcmp(url, newurl)) {
						debug(D_HTTP, "error: server gave %d redirect from %s back to the same url!", response, url);
						errno = EIO;
						return 0;
					} else {
						return http_query_size_via_proxy(proxy,newurl,action,size,stoptime,cache_reload);
					}
				} else {
					errno = ENOENT;
					return 0;
				}
				break;
			default:
				link_close(link);
				errno = http_response_to_errno(response);
				return 0;
				break;
			}
		} else {
			debug(D_HTTP, "malformed response");
			save_errno = ECONNRESET;
		}
	} else {
		debug(D_HTTP, "malformed response");
		save_errno = ECONNRESET;
	}

	link_close(link);
	errno = save_errno;
	return 0;
}
Exemplo n.º 16
0
void task_complete( struct work_queue_task *t )
{
	string_chomp(t->output);
	printf("%s\n",t->output);
	work_queue_task_delete(t);
}
Exemplo n.º 17
0
static void handle_query(struct link *query_link)
{
	FILE *stream;
	char line[LINE_MAX];
	char url[LINE_MAX];
	char path[LINE_MAX];
	char action[LINE_MAX];
	char version[LINE_MAX];
	char hostport[LINE_MAX];
	char addr[LINK_ADDRESS_MAX];
	char key[LINE_MAX];
	int port;
	time_t current;

	char *hkey;
	struct nvpair *nv;
	int i, n;

	link_address_remote(query_link, addr, &port);
	debug(D_DEBUG, "www query from %s:%d", addr, port);

	if(link_readline(query_link, line, LINE_MAX, time(0) + HANDLE_QUERY_TIMEOUT)) {
		string_chomp(line);
		if(sscanf(line, "%s %s %s", action, url, version) != 3) {
			return;
		}

		// Consume the rest of the query
		while(1) {
			if(!link_readline(query_link, line, LINE_MAX, time(0) + HANDLE_QUERY_TIMEOUT)) {
				return;
			}

			if(line[0] == 0) {
				break;
			}
		}
	} else {
		return;
	}

	// Output response
	stream = fdopen(link_fd(query_link), "w");
	if(!stream) {
		return;
	}
	link_nonblocking(query_link, 0);

	current = time(0);
	fprintf(stream, "HTTP/1.1 200 OK\n");
	fprintf(stream, "Date: %s", ctime(&current));
	fprintf(stream, "Server: catalog_server\n");
	fprintf(stream, "Connection: close\n");

	if(sscanf(url, "http://%[^/]%s", hostport, path) == 2) {
		// continue on
	} else {
		strcpy(path, url);
	}

	/* load the hash table entries into one big array */

	n = 0;
	nvpair_database_firstkey(table);
	while(nvpair_database_nextkey(table, &hkey, &nv)) {
		array[n] = nv;
		n++;
	}

	/* sort the array by name before displaying */

	qsort(array, n, sizeof(struct nvpair *), compare_nvpair);

	if(!strcmp(path, "/query.text")) {
		fprintf(stream, "Content-type: text/plain\n\n");
		for(i = 0; i < n; i++)
			nvpair_print_text(array[i], stream);
	} else if(!strcmp(path, "/query.json")) {
		fprintf(stream, "Content-type: text/plain\n\n");
		fprintf(stream,"[\n");
		for(i = 0; i < n; i++) {
			nvpair_print_json(array[i], stream);
			fprintf(stream,",\n");
		}
		fprintf(stream,"]\n");
	} else if(!strcmp(path, "/query.oldclassads")) {
		fprintf(stream, "Content-type: text/plain\n\n");
		for(i = 0; i < n; i++)
			nvpair_print_old_classads(array[i], stream);
	} else if(!strcmp(path, "/query.newclassads")) {
		fprintf(stream, "Content-type: text/plain\n\n");
		for(i = 0; i < n; i++)
			nvpair_print_new_classads(array[i], stream);
	} else if(!strcmp(path, "/query.xml")) {
		fprintf(stream, "Content-type: text/xml\n\n");
		fprintf(stream, "<?xml version=\"1.0\" standalone=\"yes\"?>\n");
		fprintf(stream, "<catalog>\n");
		for(i = 0; i < n; i++)
			nvpair_print_xml(array[i], stream);
		fprintf(stream, "</catalog>\n");
	} else if(sscanf(path, "/detail/%s", key) == 1) {
		struct nvpair *nv;
		fprintf(stream, "Content-type: text/html\n\n");
		nv = nvpair_database_lookup(table, key);
		if(nv) {
			const char *name = nvpair_lookup_string(nv, "name");
			if(!name)
				name = "unknown";
			fprintf(stream, "<title>%s catalog server: %s</title>\n", preferred_hostname, name);
			fprintf(stream, "<center>\n");
			fprintf(stream, "<h1>%s catalog server</h1>\n", preferred_hostname);
			fprintf(stream, "<h2>%s</h2>\n", name);
			fprintf(stream, "<p><a href=/>return to catalog view</a><p>\n");
			nvpair_print_html_solo(nv, stream);
			fprintf(stream, "</center>\n");
		} else {
			fprintf(stream, "<title>%s catalog server</title>\n", preferred_hostname);
			fprintf(stream, "<center>\n");
			fprintf(stream, "<h1>%s catalog server</h1>\n", preferred_hostname);
			fprintf(stream, "<h2>Unknown Item!</h2>\n");
			fprintf(stream, "</center>\n");
		}
	} else {
		char avail_line[LINE_MAX];
		char total_line[LINE_MAX];
		INT64_T sum_total = 0;
		INT64_T sum_avail = 0;
		INT64_T sum_devices = 0;

		fprintf(stream, "Content-type: text/html\n\n");
		fprintf(stream, "<title>%s catalog server</title>\n", preferred_hostname);
		fprintf(stream, "<center>\n");
		fprintf(stream, "<h1>%s catalog server</h1>\n", preferred_hostname);
		fprintf(stream, "<a href=/query.text>text</a> - ");
		fprintf(stream, "<a href=/query.html>html</a> - ");
		fprintf(stream, "<a href=/query.xml>xml</a> - ");
		fprintf(stream, "<a href=/query.json>json</a> - ");
		fprintf(stream, "<a href=/query.oldclassads>oldclassads</a> - ");
		fprintf(stream, "<a href=/query.newclassads>newclassads</a>");
		fprintf(stream, "<p>\n");

		for(i = 0; i < n; i++) {
			nv = array[i];
			sum_total += nvpair_lookup_integer(nv, "total");
			sum_avail += nvpair_lookup_integer(nv, "avail");
			sum_devices++;
		}

		string_metric(sum_avail, -1, avail_line);
		string_metric(sum_total, -1, total_line);
		fprintf(stream, "<b>%sB available out of %sB on %d devices</b><p>\n", avail_line, total_line, (int) sum_devices);

		nvpair_print_html_header(stream, html_headers);
		for(i = 0; i < n; i++) {
			nv = array[i];
			make_hash_key(nv, key);
			sprintf(url, "/detail/%s", key);
			nvpair_print_html_with_link(nv, stream, html_headers, "name", url);
		}
		nvpair_print_html_footer(stream, html_headers);
		fprintf(stream, "</center>\n");
	}
	fclose(stream);
}
Exemplo n.º 18
0
struct link *http_query_size_via_proxy(const char *proxy, const char *urlin, const char *action, INT64_T * size, time_t stoptime, int cache_reload)
{
    char url[HTTP_LINE_MAX];
    char newurl[HTTP_LINE_MAX];
    char line[HTTP_LINE_MAX];
    char addr[LINK_ADDRESS_MAX];
    struct link *link;
    int save_errno;
    int response;
    char actual_host[HTTP_LINE_MAX];
    int actual_port;
    *size = 0;

    url_encode(urlin, url, sizeof(url));

    if(proxy && !strcmp(proxy, "DIRECT"))
        proxy = 0;

    if(proxy) {
        int fields = sscanf(proxy, "http://%[^:]:%d", actual_host, &actual_port);
        if(fields == 2) {
            /* host and port are good */
        } else if(fields == 1) {
            actual_port = HTTP_PORT;
        } else {
            debug(D_HTTP, "invalid proxy syntax: %s", proxy);
            return 0;
        }
    } else {
        int fields = sscanf(url, "http://%[^:]:%d", actual_host, &actual_port);
        if(fields != 2) {
            fields = sscanf(url, "http://%[^/]", actual_host);
            if(fields == 1) {
                actual_port = HTTP_PORT;
            } else {
                debug(D_HTTP, "malformed url: %s", url);
                return 0;
            }
        }
    }

    debug(D_HTTP, "connect %s port %d", actual_host, actual_port);
    if(!domain_name_lookup(actual_host, addr))
        return 0;

    link = link_connect(addr, actual_port, stoptime);
    if(!link) {
        errno = ECONNRESET;
        return 0;
    }

    if(cache_reload == 0) {
        debug(D_HTTP, "%s %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", action, url, actual_host);
        link_putfstring(link, "%s %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", stoptime, action, url, actual_host);
    } else {
        //  force refresh of cache end-to-end (RFC 2616)
        debug(D_HTTP, "%s %s HTTP/1.1\r\nHost: %s\r\nCache-Control: max-age=0\r\nConnection: close\r\n\r\n", action, url, actual_host);
        link_putfstring(link, "%s %s HTTP/1.1\r\nHost: %s\r\nCache-Control: max-age=0\r\nConnection: close\r\n\r\n", stoptime, action, url, actual_host);
    }

    if(link_readline(link, line, HTTP_LINE_MAX, stoptime)) {
        string_chomp(line);
        debug(D_HTTP, "%s", line);
        if(sscanf(line, "HTTP/%*d.%*d %d", &response) == 1) {
            newurl[0] = 0;
            while(link_readline(link, line, HTTP_LINE_MAX, stoptime)) {
                string_chomp(line);
                debug(D_HTTP, "%s", line);
                sscanf(line, "Location: %s", newurl);
                sscanf(line, "Content-Length: %lld", size);
                if(strlen(line) <= 2) {
                    break;
                }
            }

            switch (response) {
            case 200:
                return link;
                break;
            case 301:
            case 302:
            case 303:
            case 307:
                link_close(link);
                if(newurl[0]) {
                    if(!strcmp(url, newurl)) {
                        debug(D_HTTP, "error: server gave %d redirect from %s back to the same url!", response, url);
                        errno = EIO;
                        return 0;
                    } else {
                        return http_query(newurl, action, stoptime);
                    }
                } else {
                    errno = ENOENT;
                    return 0;
                }
                break;
            default:
                link_close(link);
                errno = http_response_to_errno(response);
                return 0;
                break;
            }
        } else {
            debug(D_HTTP, "malformed response");
            save_errno = ECONNRESET;
        }
    } else {
        debug(D_HTTP, "malformed response");
        save_errno = ECONNRESET;
    }

    link_close(link);
    errno = save_errno;
    return 0;
}
Exemplo n.º 19
0
static batch_job_id_t batch_job_wq_wait (struct batch_queue * q, struct batch_job_info * info, time_t stoptime)
{
	static int try_open_log = 0;
	int timeout, taskid = -1;

	if(!try_open_log)
	{
		try_open_log = 1;
		if(!work_queue_specify_log(q->data, q->logfile))
		{
			return -1;
		}

		const char *transactions = batch_queue_get_option(q, "batch_log_transactions_name");
		if(transactions) {
			work_queue_specify_transactions_log(q->data, transactions);
		}
	}

	if(stoptime == 0) {
		timeout = WORK_QUEUE_WAITFORTASK;
	} else {
		timeout = MAX(0, stoptime - time(0));
	}

	struct work_queue_task *t = work_queue_wait(q->data, timeout);
	if(t) {
		info->submitted = t->time_when_submitted / 1000000;
		info->started   = t->time_when_commit_end / 1000000;
		info->finished  = t->time_when_done / 1000000;
		info->exited_normally = 1;
		info->exit_code = t->return_status;
		info->exit_signal = 0;
		info->disk_allocation_exhausted = t->disk_allocation_exhausted;

		/*
		   If the standard ouput of the job is not empty,
		   then print it, because this is analogous to a Unix
		   job, and would otherwise be lost.  Important for
		   capturing errors from the program.
		 */

		if(t->output && t->output[0]) {
			if(t->output[1] || t->output[0] != '\n') {
				string_chomp(t->output);
				printf("%s\n", t->output);
			}
		}

		char *outfile = itable_remove(q->output_table, t->taskid);
		if(outfile) {
			FILE *file = fopen(outfile, "w");
			if(file) {
				fwrite(t->output, strlen(t->output), 1, file);
				fclose(file);
			}
			free(outfile);
		}

		taskid = t->taskid;
		work_queue_task_delete(t);
	}

	if(taskid >= 0) {
		return taskid;
	}

	if(work_queue_empty(q->data)) {
		return 0;
	} else {
		return -1;
	}
}
Exemplo n.º 20
0
int exec_command(s_exec_options *eopt)
{
	pid_t pid = 0;
	int status = 0;
	time_t starttime = 0;
	time_t timer = 0;
	char *tempfile = NULL;
	struct sigaction new_chld;
	struct sigaction old_chld;
	sigset_t new_mask;
	sigset_t old_mask;
	
	if( eopt->command == NULL )
		return -1;
	
	if( eopt->options & EXEC_OPT_LOGOUT )
	{
		tempfile = file_gettmp();
		if( !tempfile )
			log("cannot generate temporary file name");
	}

	/* Block SIGCHLD */
	sigemptyset(&new_mask);
	sigaddset(&new_mask, SIGCHLD);
	sigprocmask(SIG_BLOCK, &new_mask, NULL);
	
	/* Set the default SIGCHLD handler */
	new_chld.sa_handler = SIG_DFL;
	sigaction(SIGCHLD, &new_chld, &old_chld);

	if( (pid = fork()) == -1 )
	{
		logerr("exec error: cannot fork()");
		return -1;
	}

	if( !pid )
	{
		if( exec_redirect_descriptor(0, "/dev/null", O_RDONLY) == -1 )
			exit(128);
		
		if( tempfile )
		{
			if( exec_redirect_descriptor(1, tempfile, O_WRONLY|O_CREAT|O_TRUNC) == -1
			 || exec_redirect_descriptor(2, tempfile, O_WRONLY) == -1 )
				exit(128);
		}
		else
		{
			if( exec_redirect_descriptor(1, "/dev/null", O_WRONLY) == -1
			 || exec_redirect_descriptor(2, "/dev/null", O_WRONLY) == -1 )
				exit(128);
		}

		if( eopt->options & EXEC_OPT_SETSID )
			setsid();
		
		if( eopt->options & EXEC_OPT_USESHELL )
			execle("/bin/sh", "sh", "-c", eopt->command, NULL, eopt->envp);
		else
		{
			char *argv[EXEC_MAX_NUM_ARGS+1];
			int argc = string_parse_regular(argv, EXEC_MAX_NUM_ARGS,
			                                eopt->command);
			if( argc > 0 )
			{
				argv[argc] = NULL;
				execve(argv[0], argv, eopt->envp);
			}
			/* eopt->command is corrupted now */
		}
		
		/* We get here only in case of errors */
		logerr("cannot execute \"%s\"", eopt->command);
		
		exit(128);
	}
	
	/*
	 * We are inside parent process, do:
	 * 
	 * 1) if OUTMODE_LOGPIPE is used than log child process ouput
	 * 2) wait for child process to exit
	 */

	log("running \"%s\", PID %d",
		string_printable(eopt->command), (int)pid);

	if( !(eopt->options & EXEC_OPT_NOWAIT) )
	{
		starttime = time(NULL); /* Set process start time */
		
		if( waitpid(pid, &status, 0) > 0 )
		{
			eopt->runtime = time(NULL) - starttime;
	
			if( eopt->runtime < 0 )
				eopt->runtime = 0;

			/*
			 * Write process's output to the log file
			 */
			if( tempfile )
			{
				char buf[256];
				FILE *fp = file_open(tempfile, "r");

				if( !fp )
					logerr("cannot open temporary file \"%s\"", tempfile);
				else
				{
					while( fgets(buf, sizeof(buf), fp) )
					{
						string_chomp(buf);
						log("[%d] %s", pid, string_printable(buf));
					}
					file_close(fp);
				}
			}

			if( WIFEXITED(status) )
			{
				eopt->retc = WEXITSTATUS(status);
				log("process %d exit with code %d (%d seconds)",
					(int)pid, eopt->retc, eopt->runtime);
			}
			else if( WIFSIGNALED(status) )
			{
				eopt->retc = -1;
				log("process %d terminated on signal %d (%d seconds)",
					(int)pid, WTERMSIG(status), eopt->runtime);
			}
			else
			{
				eopt->retc = -1;
				log("process %d return with unknown status (%d seconds)",
					(int)pid, eopt->runtime);
			}
		}
		else
		{
			eopt->retc = -1;
			logerr("waitpid return error for PID %d", (int)pid);
		}
	}

	if( tempfile )
	{
		if( unlink(tempfile) == -1 && errno != ENOENT )
			logerr("cannot unlink temporary file \"%s\"", tempfile);
		free(tempfile);
	}

	/* Restore the original SIGCHLD handler */
	sigaction(SIGCHLD, &old_chld, NULL);

	/* Unblock SIGCHLD */
	sigprocmask(SIG_UNBLOCK, &new_mask, NULL);
	
	return eopt->retc;
}
Exemplo n.º 21
0
int chirp_group_lookup(const char *group, const char *subject)
{
	char url[CHIRP_PATH_MAX];
	char cachedir[CHIRP_PATH_MAX];
	char cachepath[CHIRP_PATH_MAX];
	char line[CHIRP_PATH_MAX];
	struct stat info;

	if(!chirp_group_base_url)
		return 0;

	int fetch_group = 1;

	sprintf(cachedir, "%s/.__groups", chirp_transient_path);
	sprintf(cachepath, "%s/%s", cachedir, &group[6]);

	if(stat(cachepath, &info) == 0) {
		int age = time(0) - info.st_mtime;
		if(age < chirp_group_cache_time) {
			fetch_group = 0;
		}
	}

	if(fetch_group) {
		sprintf(url, "%s%s", chirp_group_base_url, &group[6]);
		debug(D_DEBUG, "fetching group %s from %s", group, url);
		mkdir(cachedir, 0777);
		sprintf(line, "wget --no-check-certificate -q %s -O %s", url, cachepath);
		if(system(line) != 0) {
			debug(D_NOTICE, "failed to fetch group using: %s", line);
			unlink(cachepath);
			return 0;
		}
	}

	FILE *file = fopen(cachepath, "r");
	if(!file)
		return 0;

	while(fgets(line, sizeof(line), file)) {
		string_chomp(line);

		// If it matches exactly, return.
		if(!strcmp(line, subject)) {
			fclose(file);
			return 1;
		}
		// If the group entry does not have an auth method,
		// and the subject is unix, look for equivalence after the colon.

		if(!strchr(line, ':') && !strncmp(subject, "unix:", 5)) {
			if(!strcmp(line, &subject[5])) {
				fclose(file);
				return 1;
			}
		}

	}

	fclose(file);
	return 0;
}
Exemplo n.º 22
0
batch_job_id_t batch_job_wait_mpi_queue( struct batch_queue *q, struct batch_job_info *info, time_t stoptime )
{
	static FILE *logfile = 0;
//	struct work_queue_stats s;

	int timeout, taskid = -1;

	if(!logfile) {
		logfile = fopen(q->logfile, "a");
		if(!logfile) {
			debug(D_NOTICE, "couldn't open logfile %s: %s\n", q->logfile, strerror(errno));
			return -1;
		}
	}

	if(stoptime == 0) {
		timeout = MPI_QUEUE_WAITFORTASK;
	} else {
		timeout = MAX(0, stoptime - time(0));
	}

	struct mpi_queue_task *t = mpi_queue_wait(q->mpi_queue, timeout);
	if(t) {
		info->submitted = t->submit_time / 1000000;
		info->started = t->start_time / 1000000;
		info->finished = t->finish_time / 1000000;
		info->exited_normally = 1;
		info->exit_code = t->return_status;
		info->exit_signal = 0;

		/*
		   If the standard ouput of the job is not empty,
		   then print it, because this is analogous to a Unix
		   job, and would otherwise be lost.  Important for
		   capturing errors from the program.
		 */

		if(t->output && t->output[0]) {
			if(t->output[1] || t->output[0] != '\n') {
				string_chomp(t->output);
				printf("%s\n", t->output);
			}
		}

		char *outfile = itable_remove(q->output_table, t->taskid);
		if(outfile) {
			FILE *file = fopen(outfile, "w");
			if(file) {
				fwrite(t->output, strlen(t->output), 1, file);
				fclose(file);
			}
			free(outfile);
		}
		fprintf(logfile, "TASK %llu %d %d %d %llu %llu \"%s\" \"%s\"\n", timestamp_get(), t->taskid, t->result, t->return_status, t->submit_time, t->finish_time, t->tag ? t->tag : "", t->command_line);

		taskid = t->taskid;
		mpi_queue_task_delete(t);
	}
	// Print to work queue log since status has been changed.
//	mpi_queue_get_stats(q->mpi_queue, &s);
//	fprintf(logfile, "QUEUE %llu %d %d %d %d %d\n", timestamp_get(), s.tasks_running, s.tasks_waiting, s.tasks_complete, s.total_tasks_dispatched, s.total_tasks_complete);
//	fflush(logfile);
//	fsync(fileno(logfile));

	if(taskid >= 0) {
		return taskid;
	}

	if(mpi_queue_empty(q->mpi_queue)) {
		return 0;
	} else {
		return -1;
	}
}
Exemplo n.º 23
0
struct chirp_volume *chirp_volume_open(const char *volume, time_t stoptime)
{
	struct chirp_volume *v;
	char filename[CHIRP_PATH_MAX];
	char *buffer;
	char *name;
	char *c;
	int result;

	debug(D_MULTI, "opening volume %s", volume);
	/*
	   The volume name must have at least one
	   at-sign in order to indicate the volname.
	 */

	c = strchr(volume, '@');
	if(!c) {
		errno = ENOENT;
		return 0;
	}

	v = xxmalloc(sizeof(*v));
	strcpy(v->name, volume);

	/*
	   The stuff preceding the first at-sign is
	   the name of the host containing the directory.
	 */

	strcpy(v->host, v->name);
	c = strchr(v->host, '@');

	/* The rest is the logical name of the volume. */
	strcpy(v->root, c);
	*c = 0;

	/* Convert any remaning at-signs to slashes */
	for(c = v->root; *c; c++) {
		if(*c == '@')
			*c = '/';
	}

	/* Fetch the filesystem key */
	sprintf(filename, "%s/key", v->root);
	result = chirp_reli_getfile_buffer(v->host, filename, &buffer, stoptime);
	if(result < 0) {
		debug(D_CHIRP, "couldn't open %s: %s", filename, strerror(errno));
		free(v);
		errno = ENOENT;
		return 0;
	}
	strcpy(v->key, buffer);
	string_chomp(v->key);
	free(buffer);

	/* Fetch the list of hosts */
	sprintf(filename, "%s/hosts", v->root);
	result = chirp_reli_getfile_buffer(v->host, filename, &buffer, stoptime);
	if(result < 0) {
		debug(D_CHIRP, "couldn't open %s: %s", filename, strerror(errno));
		free(v);
		errno = ENOENT;
		return 0;
	}

	/* Get an upper bound on the servers by counting the whitespace */
	int maxservers = 0;
	for(c = buffer; *c; c++)
		if(isspace((int) *c))
			maxservers++;

	/* Allocate space for an array of servers */
	v->servers = malloc(sizeof(struct chirp_server *) * maxservers);
	v->nservers = 0;
	debug(D_MULTI, "estimating %d servers", maxservers);

	/* Break into whitespace and initialize the servers. */

	name = strtok(buffer, " \t\n");
	while(name) {
		debug(D_MULTI, "server: %s", name);
		struct chirp_server *s = xxmalloc(sizeof(*s));
		strcpy(s->name, name);
		s->priority = 0;
		s->prepared = 0;
		v->servers[v->nservers++] = s;
		name = strtok(0, " \t\n");
	}

	/* randomly initialize the priority of the servers */
	int i;
	int n = rand() % v->nservers;
	for(i = 0; i < n; i++)
		v->servers[i]->priority++;

	free(buffer);

	return v;
}