int init() { int c_init(); user_list_fd=c_open("./user_list",O_RDWR|O_CREAT|0660); USER usr; memset(&usr,0,sizeof(USER)); c_lseek(user_list_fd,0,SEEK_SET); c_write(user_list_fd,(char *)&usr,USR_LEN); int on = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) perror("setsockopt error"); c_bind(sockfd,(struct sockaddr*)&server,ADDR_LEN); listen(sockfd,7);//两个队列之和为7 CHIS ch_his; bzero(&ch_his,HSTR_LEN); c_lseek(mainfd,0,SEEK_SET); c_write(mainfd,&ch_his,HSTR_LEN); c_lseek(mainfd,-HSTR_LEN,SEEK_END); c_read(mainfd,&ch_his,HSTR_LEN); count=ch_his.count;//initialize the value by the last count in the local file printf("initialization finished\n"); return 0; }
RawFile *rawFileCreate(const uint8_t *file_name, const uint32_t file_size, const word_t cdfs_md_id) { RawFile * raw_file; int fd; MEM_CHECK(file_name); #if 0 if(!__mkbasedir((char *)file_name)) { dbg_log(SEC_0132_RAW, 0)(LOGSTDOUT, "error:rawFileCreate: mkbasedir of file %s failed\n", (char *)file_name); return NULL; } #endif fd = c_open((char *)file_name, O_RDWR | O_CREAT, 0666); if(-1 == fd) { dbg_log(SEC_0132_RAW, 0)(LOGSTDOUT,"error:rawFileCreate: create %s failed\n", file_name); return NULL; } raw_file = rawFileNew(file_name, fd, O_RDWR | O_CREAT, file_size, cdfs_md_id); if(NULL == raw_file) { dbg_log(SEC_0132_RAW, 0)(LOGSTDOUT, "error:rawFileCreate: new raw file failed\n"); close(fd); return NULL; } return raw_file; }
RawFile * rawFileOpen(const uint8_t *file_name, const int flags, const uint32_t file_size, const word_t cdfs_md_id) { MEM_CHECK(file_name); if(flags & O_RDWR) { RawFile * raw_file; int fd; fd = c_open((char *)file_name, O_RDWR, 0666); if(-1 != fd) { raw_file = rawFileNew(file_name, fd, O_RDWR, file_size, cdfs_md_id); if(NULL == raw_file) { dbg_log(SEC_0132_RAW, 0)(LOGSTDOUT, "error:rawFileOpen: new raw file failed\n"); close(fd); return NULL; } if(RAW_FILE_SUCC != rawFileLoad(raw_file)) { dbg_log(SEC_0132_RAW, 0)(LOGSTDOUT, "error:rawFileOpen: load %s failed\n", (char *)file_name); close(raw_file->fd); raw_file->fd = -1; rawFileFree(raw_file); return NULL; } return raw_file; } } if(flags & O_CREAT) { return rawFileCreate(file_name, file_size, cdfs_md_id); } //dbg_log(SEC_0132_RAW, 1)(LOGSTDOUT, "warn:rawFileOpen: open %s failed which neither exist nor need to create\n", (char *)file_name); return NULL; }
static void grammar_pre(struct s_node *n) { int i, r = 0; struct s_node *p; static int cooked = 0; g_node = n; if (arg_defines()) { c_str("#include \"");c_str(arg_defines());c_strln("\""); if (arg_feed() && cooked) { c_strln("#undef PACC_NAME"); c_strln("#define PACC_NAME PACC_FEED_NAME"); } } else c_defines(); ++cooked; pre_decl(); /* We slightly simplify both building & walking the tree and insist * that every grammar starts with a preamble, which may be null. * It's a bit odd to represent no preamble with an empty preamble * node. */ p = n->first; assert(p->type == preamble); if (!arg_defines() && p->text) c_raw(p->text); p = p->next; for ( ; p; p = p->next) { assert(p->type == rule); ++r; } c_str("static const int n_rules = "); c_int(r); c_semi(); c_str("static const int start_rule_id = "); c_long(n->first->next->id); c_semi(); g_name = n->text; /* type of start rule is always u0 */ type_list(n->first->next->first->text); for (p = n->first; p; p = p->next) if (p->type == rule) type_list(p->first->text); c_str("union PACC_SYM(vals)"); c_open(); for (i = 0; i < t_max; ++i) { c_str(t_list[i]); c_str(" u"); c_int(i); c_semi(); } c_close(); c_semi(); /* XXX just for debugging */ c_str("#define TYPE_PRINTF "); if (strcmp(n->first->next->first->text, "int") == 0) c_str("\"%d\""); else if (strcmp(n->first->next->first->text, "char *") == 0) c_str("\"%s\""); else c_str("\"%p\""); c_strln(""); c_str("#define PACC_TYPE "); c_strln(n->first->next->first->text); pre_engine(); c_str("_st="); c_long(n->first->type == preamble ? n->first->next->id : n->first->id); c_semi(); c_strln("goto top;"); c_strln("contin:"); c_strln("_st=_cont;"); c_strln("PACC_TRACE fprintf(stderr, \"continuing in state %d\\n\", _cont);"); c_strln("top:"); c_strln("PACC_TRACE fprintf(stderr, \"switch to state %d\\n\", _st);"); c_str("switch(_st)"); c_open(); }
static void ListerThread(struct ListerParams *args) { int found_parent = 0; pid_t clone_pid = sys_gettid(), ppid = sys_getppid(); char proc_self_task[80], marker_name[48], *marker_path; const char *proc_paths[3]; const char *const *proc_path = proc_paths; int proc = -1, marker = -1, num_threads = 0; int max_threads = 0, sig; struct kernel_stat marker_sb, proc_sb; stack_t altstack; /* Create "marker" that we can use to detect threads sharing the same * address space and the same file handles. By setting the FD_CLOEXEC flag * we minimize the risk of misidentifying child processes as threads; * and since there is still a race condition, we will filter those out * later, anyway. */ if ((marker = sys_socket(PF_LOCAL, SOCK_DGRAM, 0)) < 0 || sys_fcntl(marker, F_SETFD, FD_CLOEXEC) < 0) { failure: args->result = -1; args->err = errno; if (marker >= 0) NO_INTR(sys_close(marker)); sig_marker = marker = -1; if (proc >= 0) NO_INTR(sys_close(proc)); sig_proc = proc = -1; sys__exit(1); } /* Compute search paths for finding thread directories in /proc */ local_itoa(strrchr(strcpy(proc_self_task, "/proc/"), '\000'), ppid); strcpy(marker_name, proc_self_task); marker_path = marker_name + strlen(marker_name); strcat(proc_self_task, "/task/"); proc_paths[0] = proc_self_task; /* /proc/$$/task/ */ proc_paths[1] = "/proc/"; /* /proc/ */ proc_paths[2] = NULL; /* Compute path for marker socket in /proc */ local_itoa(strcpy(marker_path, "/fd/") + 4, marker); if (sys_stat(marker_name, &marker_sb) < 0) { goto failure; } /* Catch signals on an alternate pre-allocated stack. This way, we can * safely execute the signal handler even if we ran out of memory. */ memset(&altstack, 0, sizeof(altstack)); altstack.ss_sp = args->altstack_mem; altstack.ss_flags = 0; altstack.ss_size = ALT_STACKSIZE; sys_sigaltstack(&altstack, (const stack_t *)NULL); /* Some kernels forget to wake up traced processes, when the * tracer dies. So, intercept synchronous signals and make sure * that we wake up our tracees before dying. It is the caller's * responsibility to ensure that asynchronous signals do not * interfere with this function. */ sig_marker = marker; sig_proc = -1; for (sig = 0; sig < sizeof(sync_signals)/sizeof(*sync_signals); sig++) { struct kernel_sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_sigaction_ = SignalHandler; sys_sigfillset(&sa.sa_mask); sa.sa_flags = SA_ONSTACK|SA_SIGINFO|SA_RESETHAND; sys_sigaction(sync_signals[sig], &sa, (struct kernel_sigaction *)NULL); } /* Read process directories in /proc/... */ for (;;) { /* Some kernels know about threads, and hide them in "/proc" * (although they are still there, if you know the process * id). Threads are moved into a separate "task" directory. We * check there first, and then fall back on the older naming * convention if necessary. */ if ((sig_proc = proc = c_open(*proc_path, O_RDONLY|O_DIRECTORY, 0)) < 0) { if (*++proc_path != NULL) continue; goto failure; } if (sys_fstat(proc, &proc_sb) < 0) goto failure; /* Since we are suspending threads, we cannot call any libc * functions that might acquire locks. Most notably, we cannot * call malloc(). So, we have to allocate memory on the stack, * instead. Since we do not know how much memory we need, we * make a best guess. And if we guessed incorrectly we retry on * a second iteration (by jumping to "detach_threads"). * * Unless the number of threads is increasing very rapidly, we * should never need to do so, though, as our guestimate is very * conservative. */ if (max_threads < proc_sb.st_nlink + 100) max_threads = proc_sb.st_nlink + 100; /* scope */ { pid_t pids[max_threads]; int added_entries = 0; sig_num_threads = num_threads; sig_pids = pids; for (;;) { struct kernel_dirent *entry; char buf[4096]; ssize_t nbytes = sys_getdents(proc, (struct kernel_dirent *)buf, sizeof(buf)); if (nbytes < 0) goto failure; else if (nbytes == 0) { if (added_entries) { /* Need to keep iterating over "/proc" in multiple * passes until we no longer find any more threads. This * algorithm eventually completes, when all threads have * been suspended. */ added_entries = 0; sys_lseek(proc, 0, SEEK_SET); continue; } break; } for (entry = (struct kernel_dirent *)buf; entry < (struct kernel_dirent *)&buf[nbytes]; entry = (struct kernel_dirent *)((char *)entry+entry->d_reclen)) { if (entry->d_ino != 0) { const char *ptr = entry->d_name; pid_t pid; /* Some kernels hide threads by preceding the pid with a '.' */ if (*ptr == '.') ptr++; /* If the directory is not numeric, it cannot be a * process/thread */ if (*ptr < '0' || *ptr > '9') continue; pid = local_atoi(ptr); /* Attach (and suspend) all threads */ if (pid && pid != clone_pid) { struct kernel_stat tmp_sb; char fname[entry->d_reclen + 48]; strcat(strcat(strcpy(fname, "/proc/"), entry->d_name), marker_path); /* Check if the marker is identical to the one we created */ if (sys_stat(fname, &tmp_sb) >= 0 && marker_sb.st_ino == tmp_sb.st_ino) { long i, j; /* Found one of our threads, make sure it is no duplicate */ for (i = 0; i < num_threads; i++) { /* Linear search is slow, but should not matter much for * the typically small number of threads. */ if (pids[i] == pid) { /* Found a duplicate; most likely on second pass */ goto next_entry; } } /* Check whether data structure needs growing */ if (num_threads >= max_threads) { /* Back to square one, this time with more memory */ NO_INTR(sys_close(proc)); goto detach_threads; } /* Attaching to thread suspends it */ pids[num_threads++] = pid; sig_num_threads = num_threads; if (sys_ptrace(PTRACE_ATTACH, pid, (void *)0, (void *)0) < 0) { /* If operation failed, ignore thread. Maybe it * just died? There might also be a race * condition with a concurrent core dumper or * with a debugger. In that case, we will just * make a best effort, rather than failing * entirely. */ num_threads--; sig_num_threads = num_threads; goto next_entry; } while (sys_waitpid(pid, (int *)0, __WALL) < 0) { if (errno != EINTR) { sys_ptrace_detach(pid); num_threads--; sig_num_threads = num_threads; goto next_entry; } } if (sys_ptrace(PTRACE_PEEKDATA, pid, &i, &j) || i++ != j || sys_ptrace(PTRACE_PEEKDATA, pid, &i, &j) || i != j) { /* Address spaces are distinct, even though both * processes show the "marker". This is probably * a forked child process rather than a thread. */ sys_ptrace_detach(pid); num_threads--; sig_num_threads = num_threads; } else { found_parent |= pid == ppid; added_entries++; } } } } next_entry:; } } NO_INTR(sys_close(proc)); sig_proc = proc = -1; /* If we failed to find any threads, try looking somewhere else in * /proc. Maybe, threads are reported differently on this system. */ if (num_threads > 1 || !*++proc_path) { NO_INTR(sys_close(marker)); sig_marker = marker = -1; /* If we never found the parent process, something is very wrong. * Most likely, we are running in debugger. Any attempt to operate * on the threads would be very incomplete. Let's just report an * error to the caller. */ if (!found_parent) { ResumeAllProcessThreads(num_threads, pids); sys__exit(3); } /* Now we are ready to call the callback, * which takes care of resuming the threads for us. */ args->result = args->callback(args->parameter, num_threads, pids, args->ap); args->err = errno; /* Callback should have resumed threads, but better safe than sorry */ if (ResumeAllProcessThreads(num_threads, pids)) { /* Callback forgot to resume at least one thread, report error */ args->err = EINVAL; args->result = -1; } sys__exit(0); } detach_threads: /* Resume all threads prior to retrying the operation */ ResumeAllProcessThreads(num_threads, pids); sig_pids = NULL; num_threads = 0; sig_num_threads = num_threads; max_threads += 100; } } }