void *handle_request(void *desc) { int sockd = ((struct client *)desc)->sockd; char *addr = &(((struct client *)desc)->addr); u_short port = ((struct client *)desc)->port; //check ip printf("Serving %s:%d\n\n", addr, port); while(get_packet(sockd) == 0); raw_close(sockd); pthread_exit(0); //exit(0); }
main() { long t1, told, rec_offset; long indrec[2]; short int indata[8192]; int rind_file; RAW_FILE *raw_file; size_t iostat; struct rawdata *raddat; short int etst; long int ltemp; int i; char raw_name[80], rind_name[80], filename[80]; raddat = (struct rawdata *) calloc(1, sizeof(struct rawdata)); rec_offset = 0; /* initialize the byte number */ told = 0; printf("Ready to create an index file to the raw RADOPS 386 radar data\n"); printf("\nEnter the file name (no extension) of the raw data file: "); scanf("%s",raw_name); getchar(); /* get rid of the <cr> at the end */ strcpy(filename,raw_name); strcat(rind_name,raw_name); if ((raw_file = rawropen(filename)) == 0) { printf("Unable to open raw data file\n"); exit(ENOENT); } /* Now open the index file for output */ strcat(rind_name,".rin"); rind_file = open(rind_name,O_RDWR|O_CREAT|O_TRUNC,0664); if(rind_file <= 0) { printf("Unable to open output file %s, fp=%x\n",rind_name,rind_file); exit(ENOENT); } else { printf("The index file will be created in the current directory\n"); } /* now start reading the raw data file and filing in the index */ while (1) { rec_offset = raw_file->raw_offset; iostat = raw_read(raw_file, 0, raddat); if (iostat == EOF) { close(rind_file); raw_close(raw_file); exit(0); } t1 = cnv_mdhms_sec(&(raddat->PARMS.YEAR), &(raddat->PARMS.MONTH), &(raddat->PARMS.DAY), &(raddat->PARMS.HOUR), &(raddat->PARMS.MINUT), &(raddat->PARMS.SEC)); indrec[0]=t1; indrec[1]=rec_offset; if (endian(&etst) == 0) { for (i=0; i < 2; ++i) { ltemp = indrec[i]; swab_dword(&indrec[i],<emp); } } write(rind_file, indrec, sizeof(indrec[0])*2); if (t1-told > 900) { printf("%d %d\n",t1,rec_offset); told = t1; } } }
int cmd_ping(FILE * f, int argc, char ** argv) { struct raw_pcb * raw; uint8_t buf[BUF_LEN]; in_addr_t ip_addr; struct sockaddr_in sin; struct iphdr * ip; struct icmphdr * icmp; uint8_t * data; int iphdrlen; int datalen; char s[16]; int len; int id; int seq; int32_t dt; uint32_t ts; uint32_t now; int i; int n; int ret = 0; if (argc < 2) { fprintf(f, msg_ping_usage); return SHELL_ERR_ARG_MISSING; } if (argc > 3) { fprintf(f, msg_ping_usage); return SHELL_ERR_EXTRA_ARGS; } if (strcmp(argv[1], "help") == 0) { fprintf(f, "ping - send ICMP ECHO_REQUEST to network host\n"); fprintf(f, msg_ping_usage); return 0; } if (inet_aton(argv[1], (struct in_addr *)&ip_addr) == 0) { fprintf(f, "ip address invalid.\n"); return SHELL_ERR_ARG_INVALID; } if (argc > 2) { n = strtol(argv[2], NULL, 0); } else { n = 5; } raw = raw_pcb_new(IPPROTO_ICMP); id = thinkos_thread_self(); datalen = DATA_LEN; fprintf(f, "PING %s: %d octets data.\n", inet_ntop(AF_INET, (void *)&ip_addr, s, 16), datalen); for (seq = 1; seq <= n; seq++) { icmp = (struct icmphdr *)(void *)buf; icmp->type = ICMP_ECHO; icmp->un.echo.id = id; icmp->un.echo.sequence = seq; data = buf + sizeof(struct icmphdr); for (i = 0; i < datalen; i++) { data[i] = i; } len = datalen + sizeof(struct icmphdr); sin.sin_addr.s_addr = ip_addr; sin.sin_family = AF_INET; ts = thinkos_clock(); // gettimeofday(&tv, NULL); memcpy(data, &ts, sizeof(uint32_t)); icmp->chksum = 0; icmp->chksum = ~in_chksum(0, icmp, len); raw_sendto(raw, buf, len, (struct sockaddr_in *)&sin); len = raw_recvfrom_tmo(raw, buf, BUF_LEN, (struct sockaddr_in *)&sin, 1000); if (len < 0) { if (len != -ETIMEDOUT) { ret = -1; break; } fprintf(f, "timed out.\n"); continue; } now = thinkos_clock(); ip = (struct iphdr *)buf; iphdrlen = ip->hlen * 4; icmp = (struct icmphdr *)(buf + iphdrlen); if ((icmp->type == ICMP_ECHOREPLY) && (icmp->un.echo.id == id)) { memcpy(&ts, buf + iphdrlen + sizeof(struct icmphdr), sizeof(uint32_t)); len -= iphdrlen + sizeof(struct icmphdr); dt = (int32_t)(now - ts); fprintf(f, "%d octets from %s: icmp_seq=%d " "ttl=%d time=%d ms\n", len, inet_ntop(AF_INET, (void *)&sin.sin_addr, s, 16), icmp->un.echo.sequence, ip->ttl, dt); } else { fprintf(f, "icmp: %d\n", icmp->type); } thinkos_sleep(250); } raw_close(raw); return ret; }
static int hdev_open(BlockDriverState *bs, QDict *options, int flags) { BDRVRawState *s = bs->opaque; int ret; const char *filename = qdict_get_str(options, "filename"); #if defined(__APPLE__) && defined(__MACH__) if (strstart(filename, "/dev/cdrom", NULL)) { kern_return_t kernResult; io_iterator_t mediaIterator; char bsdPath[ MAXPATHLEN ]; int fd; kernResult = FindEjectableCDMedia( &mediaIterator ); kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) ); if ( bsdPath[ 0 ] != '\0' ) { strcat(bsdPath,"s0"); /* some CDs don't have a partition 0 */ fd = qemu_open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); if (fd < 0) { bsdPath[strlen(bsdPath)-1] = '1'; } else { qemu_close(fd); } filename = bsdPath; qdict_put(options, "filename", qstring_from_str(filename)); } if ( mediaIterator ) IOObjectRelease( mediaIterator ); } #endif s->type = FTYPE_FILE; #if defined(__linux__) { char resolved_path[ MAXPATHLEN ], *temp; temp = realpath(filename, resolved_path); if (temp && strstart(temp, "/dev/sg", NULL)) { bs->sg = 1; } } #endif ret = raw_open_common(bs, options, flags, 0); if (ret < 0) { return ret; } if (flags & BDRV_O_RDWR) { ret = check_hdev_writable(s); if (ret < 0) { raw_close(bs); return ret; } } return ret; }
int main ( int argc, char *argv[] ) { if( argc < 2 ){ printf( "usage: %s <input.bin> [<input.bin>]\n", argv[0] ); return 0; } int i; for(i = 1; i < argc; i++){ /* read from input */ FILE *fin; if( !(fin = fopen(argv[i], "rb") ) ){ printf("Error opening file: %s\n", argv[i]); break; } /*** energy ***/ raw storage = load_data(fin,0,1000); int t; int n_states = storage.hdr.q; int lattice_size = storage.hdr.l; double beta_value = storage.hdr.b; int bin_size = 50; double n_bins = storage.hdr.size / bin_size; double *binned_data, mean, error; /* open output and write header */ char output[50]; sprintf(output, "data/%d_%s.obs", lattice_size, storage.hdr.algorithm ); FILE *fout = fopen(output,"a"); fprintf(fout, "%e\t", beta_value ); /* header */ /* mean */ binned_data = jackknife(storage.data, storage.hdr.size, bin_size ); mean = 0.0f; for(t = 0; t < n_bins; t++) mean += binned_data[t]; mean /= n_bins; free(binned_data); fprintf(fout, "%e\t", mean ); /* energy */ /* variance */ for(t = 0; t < storage.hdr.size; t++) storage.data[t] = (storage.data[t]-mean)*(storage.data[t]-mean); binned_data = jackknife(storage.data, storage.hdr.size, bin_size ); raw_close(&storage); mean = 0.0f; for(t = 0; t < n_bins; t++) mean += binned_data[t]; mean /= n_bins; error = 0.0f; for(t = 0; t < n_bins; t++) error += (binned_data[t]-mean)*(binned_data[t]-mean); error *= (n_bins-1.0f)/n_bins; free(binned_data); mean *= beta_value * beta_value * lattice_size * lattice_size; error = beta_value * beta_value * lattice_size * lattice_size * sqrt(error); fprintf(fout, "%e\t%e\t", mean, error ); /* heat capacity w error */ /*** magnetization ***/ storage = load_data(fin,1+n_states,1000); /* mean */ binned_data = jackknife(storage.data, storage.hdr.size, bin_size ); mean = 0.0f; for(t = 0; t < n_bins; t++) mean += binned_data[t]; mean /= n_bins; free(binned_data); fprintf(fout, "%e\t", mean ); /* magnetization */ /* variance */ for(t = 0; t < storage.hdr.size; t++) storage.data[t] = (storage.data[t]-mean)*(storage.data[t]-mean); binned_data = jackknife(storage.data, storage.hdr.size, bin_size ); raw_close(&storage); mean = 0.0f; for(t = 0; t < n_bins; t++) mean += binned_data[t]; mean /= n_bins; error = 0.0f; for(t = 0; t < n_bins; t++) error += (binned_data[t]-mean)*(binned_data[t]-mean); error *= (n_bins-1.0f)/n_bins; free(binned_data); mean *= beta_value * lattice_size * lattice_size; error = beta_value * lattice_size * lattice_size * sqrt(error); fprintf(fout, "%e\t%e\t%d\n", mean, error, lattice_size ); /* susceptibility w error */ printf("Written to: %s\t\tβ -> %f\n", output, beta_value); fclose(fout); } return 0; }
static void __attribute__((constructor)) startup(void) { #else #define RETURN_VALUE 0 static void *ignore_ud2_addr; // scratch test code int main(void) { #endif char *debug_level_str = getenv("TRAP_SYSCALLS_DEBUG"); char *footprint_fd_str = getenv("TRAP_SYSCALLS_FOOTPRINT_FD"); char *trace_fd_str = getenv("TRAP_SYSCALLS_TRACE_FD"); char *sleep_for_seconds_str = getenv("TRAP_SYSCALLS_SLEEP_FOR_SECONDS"); char *stop_self_str = getenv("TRAP_SYSCALLS_STOP_SELF"); stop_self = (stop_self_str != NULL); footprints_spec_filename = getenv("TRAP_SYSCALLS_FOOTPRINT_SPEC_FILENAME"); struct timespec one_second = { /* seconds */ 1, /* nanoseconds */ 0 }; if (debug_level_str) debug_level = atoi(debug_level_str); if (trace_fd_str) trace_fd = atoi(trace_fd_str); if (footprint_fd_str) footprint_fd = atoi(footprint_fd_str); if (sleep_for_seconds_str) sleep_for_seconds = atoi(sleep_for_seconds_str); debug_printf(0, "Debug level is %s=%d.\n", debug_level_str, debug_level); if (stop_self) { self_pid = raw_getpid(); debug_printf(0, "TRAP_SYSCALLS_STOP_SELF is set, sending SIGSTOP to self (pid %d)\n", self_pid); raw_kill(self_pid, SIGSTOP); } debug_printf(0, "TRAP_SYSCALLS_SLEEP_FOR_SECONDS is %s, pausing for %d seconds", sleep_for_seconds_str, sleep_for_seconds); for (int i = 0; i < sleep_for_seconds; i++) { raw_nanosleep(&one_second, NULL); debug_printf(0, "."); } debug_printf(0, "\n"); /* Is fd open? If so, it's the input fd for our sanity check info * from systemtap. */ debug_printf(0, "TRAP_SYSCALLS_FOOTPRINT_FD is %s, ", footprint_fd_str); if (footprint_fd > 2) { struct stat buf; int stat_ret = raw_fstat(footprint_fd, &buf); if (stat_ret == 0) { debug_printf(0, "fd %d is open; outputting systemtap cross-check info.\n", footprint_fd); /* PROBLEM: ideally we'd read in the stap script's output ourselves, and process * it at every system call. But by reading in stuff from stap, we're doing more * copying to/from userspace, so creating a feedback loop which would blow up. * * Instead we write out what we think we touched, and do a diff outside the process. * This also adds noise to stap's output, but without the feedback cycle: we ourselves * won't read the extra output, hence won't write() more stuff in response. */ __write_footprints = 1; footprints_out = fdopen(footprint_fd, "a"); if (!footprints_out) { debug_printf(0, "Could not open footprints output stream for writing!\n"); } if (footprints_spec_filename) { footprints = parse_footprints_from_file(footprints_spec_filename, &footprints_env); } else { debug_printf(0, "no footprints spec filename provided\n", footprints_spec_filename); } } else { debug_printf(0, "fd %d is closed; skipping systemtap cross-check info.\n", footprint_fd); } } else { debug_printf(0, "skipping systemtap cross-check info\n"); } debug_printf(0, "TRAP_SYSCALLS_TRACE_FD is %s, ", trace_fd_str); if (!trace_fd_str || trace_fd == 2) { debug_printf(0, "dup'ing stderr, "); trace_fd = dup(2); } if (trace_fd >= 0) { struct stat buf; int stat_ret = raw_fstat(trace_fd, &buf); if (stat_ret == 0) { debug_printf(0, "fd %d is open; outputting traces there.\n", trace_fd); __write_traces = 1; traces_out = fdopen(trace_fd, "a"); if (!traces_out) { debug_printf(0, "Could not open traces output stream for writing!\n"); } } else { debug_printf(0, "fd %d is closed; not outputting traces.\n", trace_fd); } } else { debug_printf(0, "not outputting traces.\n"); } int fd = raw_open("/proc/self/maps", O_RDONLY); if (fd != -1) { // we use a simple buffer and a read loop char buf[8192]; unsigned int ret; char *buf_pos = &buf[0]; // the next position to fill in the buffer char *entry_start_pos = &buf[0]; // the position size_t size_requested; do { // read some stuff, perhaps filling up the buffer size_requested = sizeof buf - (buf_pos - buf); ret = raw_read(fd, buf_pos, size_requested); char *buf_limit = buf_pos + ret; assert(buf_limit <= &buf[sizeof buf]); // we have zero or more complete entries in the buffer; iterate over them char *seek_pos; while (1) { seek_pos = entry_start_pos; // search forward for a newline while (seek_pos != buf_limit && *seek_pos != '\n') { ++seek_pos; } // did we find one? if (seek_pos == buf_limit) { // no! // but we have a partial entry in the buffer // between entry_start_pos and seek_pos; // copy it to the start, re-set and continue __builtin_memmove(&buf[0], entry_start_pos, seek_pos - entry_start_pos); buf_pos = &buf[seek_pos - entry_start_pos]; entry_start_pos = &buf[0]; break; } else { assert(*seek_pos == '\n'); // we have a complete entry; read it and advance entry_start_pos char debug_buf1[seek_pos - entry_start_pos + 1]; strncpy(debug_buf1, entry_start_pos, seek_pos - entry_start_pos); debug_buf1[sizeof debug_buf1 - 1] = '\0'; debug_printf(1, "DEBUG: entry is: %s\n", debug_buf1); char debug_buf2[buf_pos - buf]; strncpy(debug_buf2, buf, buf_pos - buf); debug_buf2[sizeof debug_buf2 - 1] = '\0'; debug_printf(1, "DEBUG: buffer is: %s", debug_buf2); saw_mapping(entry_start_pos, seek_pos); entry_start_pos = seek_pos + 1; // if the newline was the last in the buffer, break and read more if (entry_start_pos == buf_pos + sizeof buf) { buf_pos = entry_start_pos = &buf[0]; break; } // else we might have another entry; go round again continue; } } } while (ret > 0); raw_close(fd); } /* Install our SIGILL (was SIGTRAP, but that interferes with gdb) handler. * Linux seems to require us to provide a restorer; the code is in restore_rt. */ struct sigaction action = { //.sa_sigaction = &handle_sigtrap, .sa_handler = &handle_sigill, .sa_mask = 0, .sa_flags = /*SA_SIGINFO |*/ 0x04000000u /* SA_RESTORER */ | /*SA_RESTART |*/ SA_NODEFER, .sa_restorer = restore_rt }; struct sigaction oldaction; raw_rt_sigaction(SIGILL, &action, &oldaction); /* Un-executablize our own code, except for the signal handler and the remainder of * this function and those afterwards. * * For this, we need our load address. How can we get this? We've already seen it! */ // long int len = &&exit_and_return - our_text_begin_address; // long int ret; // long int longprot = PROT_NONE; // long int op = SYS_mprotect; // __asm__ (".align 4096"); exit_and_return: //__asm__ volatile ("movq %0, %%rdi # \n\ // movq %1, %%rsi # \n\ // movq %2, %%rdx # \n\ // "FIX_STACK_ALIGNMENT " \n\ // movq %3, %%rax # \n\ // syscall # do the syscall \n\ // "UNFIX_STACK_ALIGNMENT " \n" // : /* no output*/ : "rm"(our_text_begin_address), "rm"(len), "rm"(longprot), "rm"(op) : "%rax", "r12", SYSCALL_CLOBBER_LIST); #ifdef EXECUTABLE // HACK for testing: do a ud2 right now! ignore_ud2_addr = &&ud2_addr; ud2_addr: __asm__ ("ud2\n"); // we must also exit without running any libdl exit handlers, // because we're an executable so our csu/startfiles include some cleanup // that will now cause traps (this isn't necessary in the shared library case) raw_exit(0); #endif return RETURN_VALUE; } // For debug printing inside handle_sigill we have to know // that it's our own debug printing in order to filter it // out of the footprints, hence this noinline function // rather than using the normal macro __attribute__ ((noinline)) static void _handle_sigill_debug_printf(int level, const char *fmt, ...) { va_list vl; va_start(vl, fmt); if ((level) <= debug_level) { vfprintf(*p_err_stream, fmt, vl); fflush(*p_err_stream); } va_end(vl); }