int main(int argc,char *argv[]) { struct jobcmd deqcmd; int fd; if(argc!=2) { usage(); return 1; } deqcmd.type=DEQ; deqcmd.defpri=0; deqcmd.owner=getuid(); deqcmd.argnum=1; strcpy(deqcmd.data,*++argv); printf("jid %s\n",deqcmd.data); #ifdef DEBUG printf("deqcmd cmdtype\t%d (-1 means ENQ, -2 means DEQ, -3 means STAT\n" "deqcmd owner\t%d\n", deqcmd.type,deqcmd.owner); #endif if((fd=open("/tmp/server",O_WRONLY))<0) error_sys("deq open fifo failed"); if(write(fd,&deqcmd,DATALEN)<0) error_sys("deq write failed"); close(fd); return 0; }
int main(int argc, char **argv) { struct sockaddr_in serv_addr; int cfd; int n; char buf[BUFSIZ]; cfd = socket(AF_INET, SOCK_STREAM, 0); if (cfd == -1) error_sys("socket"); memset(&serv_addr, 0, sizeof(serv_addr)); //用sizeof可使更通用 serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(SERV_PORT); inet_pton(AF_INET, SERV_ADDR, &serv_addr.sin_addr.s_addr); n = connect(cfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); if (n == -1) error_sys("connect"); while (1) { n = read(STDIN_FILENO, buf, BUFSIZ); if (n == 0) //手动产生文件结束符 ctrl + d break; n = write(cfd, buf, n);//将数据放到发送缓冲区中,此时write立即返回,不管是否已经发出去了,若对方已关闭链接,则无法发送,并将置RST标志位,表示对方已经关闭链接,当下一次调用write时,write启动另一个内核函数去检查到此标志位,然后向此客户端程序发送SIGPIPE信号,默认是终止进程,若是服务器,需将此信号的处理方式改为捕捉,不致服务器出现崩溃 n = read(cfd, buf, BUFSIZ); n = write(STDOUT_FILENO, buf, n); } close(cfd); return 0; }
int main(void){ char buffer[LINE]; pid_t process_id; //parent process upon which the child process is called int status; printf("%% ");//prompt while(fgets(buffer , LINE , stdin) != NULL){ if(buffer[strlen(buffer) -1] == '\n'){ buffer[strlen(buffer) -1] == '\0'; } printf("cmd: %s \n" , buffer); if(strncmp(buffer,"quit",4) == 0) break; //fork a new process check for errors if((process_id = fork()) < 0){ error_sys("Cant fork a new process"); }else if(process_id == 0){ int wth = execv("/usr/ls/" , buffer); // executes a command printf("The process id for this is %ld %d \n" ,(long)getpid() , wth); error_sys("Couldn't execute"); exit(127); } /* parent process */ if((process_id = waitpid(process_id , &status , 0)) < 0); printf("Status :%d\n" , status); error_sys("Waitpid error"); printf("%% "); printf("The process id for this is %ld \n" ,(long)getpid()); } exit(0); }
int main(void) { int fd; int fw; if ((fd = creat("file.hole", FILE_MODE)) < 0) printf("file.hole is not create"); else printf("%d\n", fd); error_sys("creat error"); printf("--------\n"); if (fw = write(fd, buf1, 10) != 10) error_sys("buf1 write error"); else printf("fw is %d\n", fw); /* offset now = 10 */ if (lseek(fd, 16384, SEEK_SET) == -1) error_sys("lseek error"); /* offset now = 16384 */ if (write(fd, buf2, 10) != 10) error_sys("buf2 write error"); /* offset now = 16394 */ exit(0); }
int gpudata_transfer(gpudata *dst, size_t dstoff, gpudata *src, size_t srcoff, size_t sz) { gpucontext *src_ctx; gpucontext *dst_ctx; void *tmp; int res; src_ctx = ((partial_gpudata *)src)->ctx; dst_ctx = ((partial_gpudata *)dst)->ctx; if (src_ctx == dst_ctx) return src_ctx->ops->buffer_move(dst, dstoff, src, srcoff, sz); if (src_ctx->ops == dst_ctx->ops) { res = src_ctx->ops->buffer_transfer(dst, dstoff, src, srcoff, sz); if (res == GA_NO_ERROR) return res; } /* Fallback to host copy */ tmp = malloc(sz); if (tmp == NULL) { error_sys(src_ctx->err, "malloc"); return error_sys(dst_ctx->err, "malloc"); } res = src_ctx->ops->buffer_read(tmp, src, srcoff, sz); if (res != GA_NO_ERROR) { free(tmp); return res; } res = dst_ctx->ops->buffer_write(dst, dstoff, tmp, sz); free(tmp); return res; }
int main(int argc,char *argv[]) { struct jobcmd statcmd; int fd; if(argc!=1) { usage(); return 1; } statcmd.type=STAT; statcmd.defpri=0; statcmd.owner=getuid(); statcmd.argnum=0; if((fd=open("/tmp/server",O_WRONLY))<0) error_sys("stat open fifo failed"); if(write(fd,&statcmd,DATALEN)<0) error_sys("stat write failed"); close(fd); return 0; }
int main(int argc, char **argv) { struct sockaddr_in serv_addr, cliaddr; int sfd, cfd; int n, cli_len, i; char buf[BUFSIZ]; char cli_str[IPSIZE]; sfd = socket(AF_INET, SOCK_STREAM, 0); if (sfd == -1) error_sys("socket"); memset(&serv_addr, 0, sizeof(serv_addr)); //用sizeof可使更通用 serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(SERV_PORT); inet_pton(AF_INET, SERV_ADDR, (void *)&serv_addr.sin_addr.s_addr); n = bind(sfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); if (n == -1) error_sys("bind"); n = listen(sfd, 20); if (n == -1) error_sys("listen"); while (1) { cli_len = sizeof(cliaddr); cfd = accept(sfd, (struct sockaddr *)&cliaddr, (socklen_t *)&cli_len); if (cfd == -1) { if ((errno == ECONNABORTED) || (errno == EINTR)) continue; error_sys("accept"); } printf("received from %s at port :%d\n", inet_ntop(AF_INET, &cliaddr.sin_addr.s_addr, cli_str, IPSIZE), ntohs(cliaddr.sin_port)); while (1) { n = read(cfd, buf, BUFSIZ);//只有当数据结束时才跳出,去关闭链接cfd //close(cfd); // shutdown(cfd, SHUT_WR); printf("i can still received: %s\n", buf); if (n == 0) { write(STDOUT_FILENO, "connect close...\n", 17); break; } for (i = 0; i < n; i++) buf[i] = toupper(buf[i]); printf("here\n"); n = write(cfd, buf, n); printf("write number: %d\n", n); } close(cfd); } close(sfd); return 0; }
int main(int argc,char *argv[]) { struct jobcmd statcmd; int fd; char string[10000]="\0"; int length; int out; int fd1; if(argc!=1) { usage(); return 1; } statcmd.type=STAT; statcmd.defpri=0; statcmd.owner=getuid(); statcmd.argnum=0; #ifdef DEBUG printf("statcmd cmdtype\t%d (-1 means ENQ, -2 means DEQ, -3 means STAT)\n" "statcmd owner\t%d\n" "statcmd defpri\t%d\n" "statcmd argnum\t%d\n", statcmd.type,statcmd.owner,statcmd.defpri,statcmd.argnum); #endif if((fd=open("/tmp/server",O_WRONLY))<0) error_sys("stat open fifo failed"); if(write(fd,&statcmd,DATALEN)<0) error_sys("stat write failed"); if((fd1=open("/tmp/stat",O_RDONLY|O_NONBLOCK))<0){ if(mkfifo("/tmp/stat",0777)<0){ ; } } sleep(1); printf("\t\tJOBID\tPID\tOWNER\tRUNTIME\tWAITTIME\tCREATTIME\t\tSTATE\tPRIO\n"); out=read(fd1,string,10000); printf("%s\n",string); close(fd1); }
// \n or size-1 个字符时结束. char *map_fgets(char *buf, int size, int fd) { char *p_map; int off = lseek(fd, 0, SEEK_CUR); int map_start = (off / 4096) * 4096; int distanc = off - map_start; int len = lseek(fd, 0, SEEK_END); int i, j; if (len == off) return NULL; p_map = mmap(NULL, BUFSIZ, PROT_READ, MAP_PRIVATE, fd, map_start); //立即 写出 munmap(p_map, BUFSIZ); if (p_map == MAP_FAILED) error_sys("mmap error :"); for (i = distanc, j = 0; j < size - 1; i++, j++) { buf[j] = p_map[i]; if (p_map[i] == '\n') { j++; break; } } buf[j] = '\0'; lseek(fd, off + j, SEEK_SET); munmap(p_map, BUFSIZ); return buf; }
/** * \brief NCCL implementation of \ref gpucomm_new. */ static int comm_new(gpucomm **comm_ptr, gpucontext *ctx, gpucommCliqueId comm_id, int ndev, int rank) { gpucomm *comm; ncclResult_t err; ASSERT_CTX(ctx); GA_CHECK(setup_lib(ctx->err)); comm = calloc(1, sizeof(*comm)); // Allocate memory if (comm == NULL) { *comm_ptr = NULL; // Set to NULL if failed return error_sys(ctx->err, "calloc"); } comm->ctx = (cuda_context *)ctx; // convert to underlying cuda context // So that context would not be destroyed before communicator comm->ctx->refcnt++; cuda_enter(comm->ctx); // Use device err = ncclCommInitRank(&comm->c, ndev, *((ncclUniqueId *)&comm_id), rank); cuda_exit(comm->ctx); TAG_COMM(comm); if (err != ncclSuccess) { *comm_ptr = NULL; // Set to NULL if failed comm_clear(comm); return error_nccl(ctx->err, "ncclCommInitRank", err); } *comm_ptr = comm; return GA_NO_ERROR; }
void test_app() { int sfd = 0; char recvbuff[4096]; int len; int i; struct sockaddr_in servaddr; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(ECHO_PORT); inet_pton(AF_INET, dst, &servaddr.sin_addr); if ( (sfd = stupid_socket(AF_INET, SOCK_DGRAM, 0)) == -1) error_sys("error socket"); for (i = 0; i < 2; i++) { stupid_sendto(sfd, content[i], strlen(content[i]), &servaddr, sizeof(servaddr)); len = stupid_recvfrom(sfd, recvbuff, 4096, &servaddr, sizeof(servaddr)); recvbuff[len] = 0; printf("%s\n", recvbuff); } sfd_release(sfd); }
int gpucontext_props_new(gpucontext_props **res) { gpucontext_props *r = calloc(1, sizeof(gpucontext_props)); if (r == NULL) return error_sys(global_err, "calloc"); r->dev = -1; r->sched = GA_CTX_SCHED_AUTO; r->flags = 0; r->kernel_cache_path = NULL; r->initial_cache_size = 0; r->max_cache_size = (size_t)-1; *res = r; return GA_NO_ERROR; }
int list (int argc, char **argv) { char line[MAXCMDLEN + 1]; char *word_break; FILE *fp; /* No additional options or arguments. */ if (argc > 2) { error ("Extra arguments: list takes none"); return 1; } (void)argv; if (!(fp = iq_client_connect ())) return 1; if (fprintf (fp, "LIST\r\n") == EOF) { error_sys ("Could not submit command to server"); fclose (fp); return 1; } if (!fgets (line, sizeof line - 1, fp)) { error ("Server did not respond to command!"); fclose (fp); return 1; } /* Extract the first word and compare. */ word_break = line + strcspn (line, " \t\r\n"); if (*word_break) *word_break++ = 0; if (strcasecmp (line, RESPONSE_LIST_COMING)) { error ("Server responded with error: %s", word_break); fclose (fp); return 1; } while (fgets (line, sizeof line - 1, fp)) { fputs (line, stdout); } fclose (fp); return 0; }
int mem_pool_free(void) { char *p, *q; size_t size; if (head == NULL) return 0; if (((memctl_t *)head)->next == NULL) { //只有一个池 p = head; size = get_size(head); if (sfree(head, size) == -1) error_sys("sfree failed"); return 0; } for (p = head; p; p = q) { q = (char *)((memctl_t *)p)->next; size = get_size(p); if (sfree(p, size) == -1) error_sys("sfree failed"); } return 0; }
int main(){ int fifo; MemoryAccessRequest request; ptr_memAccReq = &request; while (1){ /* 在阻塞模式下打开FIFO */ puts("zzzzz"); if((fifo=open("/tmp/server",O_WRONLY))<0) error_sys("open fifo failed"); puts("yyyyy"); do_request(); puts("xxxxxxxx"); write(fifo,ptr_memAccReq,sizeof(MemoryAccessRequest)); close(fifo); } }
int main(void) { int fd; char buf[BUFSIZ]; char *d; int i = 0; if ((fd = open("hello", O_RDONLY)) == -1) error_sys("open error: "); while ((d = map_fgets(buf, BUFSIZ, fd)) != NULL) { //fputs(buf, stdin); printf("buf[%d]: %s", i, d); i++; } close(fd); return 0; }
int obtainUIDandGID(const char *name, uid_t *pw_uid, gid_t *pw_gid) { /* Obtain UID and GID from passwd entry identified by name */ struct passwd *pw_entry; char msg[100]; if ((pw_entry = getpwnam(name)) == NULL) { snprintf(msg, sizeof(msg), "failed to get password entry for %s", name); error_sys(msg); return FALSE; } else { *pw_uid = pw_entry->pw_uid; *pw_gid = pw_entry->pw_gid; return TRUE; } }
int retrieve (Job *job) { CURL *curl; CURLcode rc; FILE *outfp; JobOptions *opts; debug ("Retrieval process %d running job:", getpid()); debug_dump_job (job); if (job->options.continue_from) { outfp = fopen (job->options.save_to, "a"); } else { outfp = fopen (job->options.save_to, "w"); } if (!outfp) { error_sys ("could not open `%s' for output", job->options.save_to); return 1; } curl = curl_easy_init (); if (!curl) { error ("could not initialize libcurl"); write_error_file (job, "could not initialize libcurl"); fclose (outfp); return 1; } curl_easy_setopt (curl, CURLOPT_FILE, outfp); curl_easy_setopt (curl, CURLOPT_URL, job->source_url); curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, progress_callback); curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, (void *)job); curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, job->error); curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0); if (job->options.continue_from) { curl_easy_setopt (curl, CURLOPT_RESUME_FROM, (long)job->options.continue_from); } /* Now load the job's user-configurable parameters: */ opts = &job->options; if (opts->proxy[0]) { curl_easy_setopt (curl, CURLOPT_PROXY, opts->proxy); } if (opts->follow) { curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt (curl, CURLOPT_MAXREDIRS, opts->follow); } else { curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 0); } if (opts->user_agent[0]) { curl_easy_setopt (curl, CURLOPT_USERAGENT, opts->user_agent); } if (opts->use_ascii) { curl_easy_setopt (curl, CURLOPT_TRANSFERTEXT, 1); } if (opts->referer[0]) { curl_easy_setopt (curl, CURLOPT_REFERER, opts->referer); } if (opts->include) { curl_easy_setopt (curl, CURLOPT_HEADER, 1); } if (opts->interface[0]) { curl_easy_setopt (curl, CURLOPT_INTERFACE, opts->interface); } if (opts->proxy_auth[0]) { curl_easy_setopt (curl, CURLOPT_PROXYUSERPWD, opts->proxy_auth); } if (opts->auth[0]) { curl_easy_setopt (curl, CURLOPT_USERPWD, opts->auth); } /* If wmget is verbose, set libcurl to verbose too... */ if (output_level () > OL_NORMAL) curl_easy_setopt (curl, CURLOPT_VERBOSE, 1); /* Finally, perform the download: */ job->status = J_RUNNING; rc = curl_easy_perform (curl); if (rc) { if (job->status == J_STOPPING) { info ("aborted by user"); job->status = J_COMPLETE; } else { error (job->error); write_error_file (job, job->error); job->status = J_COMPLETE; } } else { job->status = J_COMPLETE; } curl_easy_cleanup (curl); fclose (outfp); if (rc) return 1; return 0; }
/* ./app src dest [n] */ int main(int argc, char **argv) { int n = 5, i, j; off_t m_off_s = 0, m_off_d = 0; //size_t size = 1024 * 1024; int err; size_t size = 1024 * 4; int left_size; if (argc == 4) n = atoi(argv[3]); pthread_t tid[n]; memset(tid, 0,sizeof(tid)); // tid = malloc(sizeof(*tid) * n); p = malloc(sizeof(*p)); memset((void *)p, 0, sizeof(*p)); if ((p->fd_s = open(argv[1], O_RDWR)) == -1) //以大文件方式打开 error_sys("open src"); if ((p->fd_d = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) //以大文件方式打开 error_sys("open dest"); left_size = lseek(p->fd_s, 0, SEEK_END); p->total = left_size; lseek(p->fd_d, left_size - 1, SEEK_SET); write(p->fd_d, "0", 1); while (left_size > 0) { //文件结尾处跳出 p->mmap_s = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, p->fd_s, m_off_s); if (p->mmap_s == MAP_FAILED) error_sys("mmap src"); m_off_s += size; p->mmap_d = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, p->fd_d, m_off_d); if (p->mmap_d == MAP_FAILED) error_sys("mmap dest"); m_off_d += size; PTHREAD_THREADS_MAX; p->index = 0; p->len = ((int)size)/(n); for (i = 0; i < n - 1; i++) { err = pthread_create(&tid[i], NULL, ptr_fn, (void *)p->index); //这是个在变化的值p->index, if (err != 0) { printf("%dth child\n", i); perror("pthread_create"); } p->index += p->len; } for (j = 0; j < n - 1; j++) { err = pthread_join(tid[j], NULL); /* 381 个子线程时出现段错误*/ if (err != 0) perror("pthread_join"); } copy((void *)p->index, (void *)(p->len + size%n)); munmap(p->mmap_s, size); munmap(p->mmap_d, size); left_size -= size; } close(p->fd_s); close(p->fd_d); return 0; }
int main(int argc,char *argv[]) { int p=2;//The priority is highest! int fd; char c,*offset; struct jobcmd enqcmd; if(argc==1) { usage();//print help message! return 1; } while(--argc>0 && (*++argv)[0]=='-') { while(c=*++argv[0]) switch(c) { case 'p':p=atoi(*(++argv)); argc--; break; default: printf("Illegal option %c\n",c); return 1; } } if(p<0||p>2) { printf("invalid priority:must between 0 and 2\n"); return 1; } enqcmd.type=ENQ; enqcmd.defpri=p; enqcmd.owner=getuid(); enqcmd.argnum=argc; offset=enqcmd.data; while (argc-->0) { strcpy(offset,*argv); strcat(offset,":"); offset=offset+strlen(*argv)+1; argv++; } #ifdef DEBUG printf("Task 4:\n" "enqcmd cmdtype \t%d(-1 means ENQ,-2 means DEQ,-3 means STAT)\n" "enqcmd owner\t%d\n" "enqcmd defpri\t%d\n" "enqcmd data\t%s\n" "enqcmd argnum\t%d\n", enqcmd.type,enqcmd.owner,enqcmd.defpri,enqcmd.data,enqcmd.argnum); #endif if((fd=open("/tmp/server",O_WRONLY))<0) error_sys("enq open fifo failed"); if(write(fd,&enqcmd,DATALEN)<0) error_sys("enq write failed"); close(fd); return 0; }
int main (int argc, char **argv ) { pid_t pid; uid_t UID; gid_t GID; pid_t pidwait; int waitstat; int s; int max_fd; /* Sanity check */ if (argc > MAX_ARGS) { error_sys("arg buffer too small"); exit(-1); } if (geteuid() != 0) { error_sys("must be called by root"); exit(-1); } /* fork child that will become prelude-correlator */ if ((pid = fork()) < 0) error_sys("fork error"); else { if (pid == 0) { /* We're the child */ char *args[MAX_ARGS]; unsigned int i; /* Become session leader */ setsid(); /* Change working directory to root directory. The current working directory could be a mounted filesystem; if the daemon stays on a mounted filesystem it could prevent the filesystem from being umounted. */ chdir("/"); /* Clear out file creation mask */ umask(0); /* Close unneeded file descriptors */ max_fd = (int) sysconf(_SC_OPEN_MAX); if (max_fd == -1) max_fd = getdtablesize(); for (s = 3; s < max_fd; s++) (void) close(s); if (!obtainUIDandGID(PRELUDE_CORRELATOR_USER, &UID, &GID)) exit(-1); /* Drop privileges immediately */ if (setgid(GID) < 0) { /* It is VERY important to check return value and not continue if setgid fails */ error_sys ("setgid failed"); exit (-1); } if (setuid(UID) < 0) { /* It is VERY important to check return value and not continue if setuid fails */ error_sys ("setuid failed"); exit (-1); } /* Build calling argv */ args[0] = PRELUDE_CORRELATOR_PATH; for (i=1;i<argc;i++) { args[i] = argv[i]; } args[i++] = NULL; /* Finally transform self into prelude-correlator */ if (execvp(PRELUDE_CORRELATOR_PATH, args) < 0) error_sys("execve error"); else ; /* avoid if-then ambiguity */ } else { /* We're the parent Terminate */ exit(0); } } }
/*./client up/down filename*/ int main(int argc, char *argv[]) { char *usr_name; int sfd, n, i, nsend; struct sockaddr_in serv_addr; char buf[1024]; int fd, len_argv; char path[1024]; int up_or_down = -1; /* 0:up 1:down */ char serv_ip[1024]; char src[1024]; char dest[1024]; char file_name[1024]; struct stat st; regex_t preg; char *regex = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"; int reg; regmatch_t pmatch[2]; size_t nmatch = 2; memset(pmatch, 0, sizeof(pmatch)); memset(&preg, 0, sizeof(preg)); regcomp(&preg, regex, 0); full(argv[1], argv[2], serv_ip, src, dest, &up_or_down, file_name); //reg = regexec(&preg, serv_ip, nmatch, pmatch, 0); reg = regexec(&preg, "127.0.0.1", nmatch, pmatch, 0); /* 如何做呢???? */ if (reg == 0) { printf("serv_ip is error\n"); error_sys("regexec"); } printf("serv_ip:%s\nsrc:%s\ndest:%s\n", serv_ip, src, dest); if (up_or_down == UP) printf("upload\n"); else if (up_or_down == DOWN) printf("download\n"); printf("filename:%s\n", file_name); #if 1 sfd = socket(AF_INET, SOCK_DGRAM, 0); if (sfd == -1) return -1; memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(SERV_PORT); inet_pton(AF_INET, SERV_IP, &serv_addr.sin_addr.s_addr); //sprintf(path, "%s/%s", ROOT, usr_name); if (up_or_down == UP) { /* 上传 */ lstat(src, &st); if(S_ISDIR(st.st_mode)) { printf("%s is dir\n", src); return -1; } sprintf(buf, "up %s %s", dest, file_name); /* 上传标志 */ PRINT_S(buf); PRINT_S(dest); PRINT_S(file_name); n = sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); PRINT_S("here1"); #if 0 n = recvfrom(sfd, buf, 1024, 0, NULL, NULL); buf[n] = '\0'; if (strcmp(buf, "OK")) { printf("fail to up link ...\n"); return 0; } #endif fd = open(src, O_RDONLY); /* 数据 */ if (fd == -1) error_sys("open"); do { nsend = sendto(sfd, buf, n, 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); } while ((n = read(fd, buf, 1024)) != 0); close(fd); // tryagain: n = sendto(sfd, "UP_END", strlen("UP_END"), 0, /* 结束标志 */ (struct sockaddr *)&serv_addr, sizeof(serv_addr)); #if 0 n = recvfrom(sfd, buf, 1024, 0, NULL, NULL); buf[n] = '\0'; if (strcmp(buf, "OK")) { sleep(1); /* 等待因网络原因产生的数据无法到达*/ printf("fail to logout...\n"); goto tryagain; } #endif } else if (up_or_down == DOWN) { /* 下载 */ lstat(dest, &st); if(S_ISREG(st.st_mode)) { printf("%s is reg file \n", dest); return -1; } sprintf(buf, "down %s %s", src, file_name); PRINT_S(buf); PRINT_S(src); PRINT_S(file_name); n = sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); #if 0 n = recvfrom(sfd, buf, 1024, 0, NULL, NULL); /*需添加更安全的处理*/ buf[n] = '\0'; if (strcmp(buf, "OK")) { printf("fail to up link ...\n"); return 0; } #endif if (dest[strlen(dest)] == '/') dest[strlen(dest)] = '\0'; sprintf(path, "%s/%s", dest, file_name); PRINT_S(path); PRINT_S(dest); PRINT_S(file_name); fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); do { PRINT_S("here2"); n = recvfrom(sfd, buf, 1024, 0, NULL, NULL); if (is_down_end(buf)) { close(fd); break; } write(fd, buf, n); } while (1); } else { /* 不合法 */ printf("useage: ./client up/down file1\n"); } close(sfd); #endif return 0; }
int main (int argc, char **argv ) { pid_t pid; uid_t UID; gid_t GID; pid_t pidwait; int waitstat; int maxfd; int s; /* Sanity check */ if (argc > MAX_ARGS) { error_sys("arg buffer too small"); exit(-1); } if (geteuid() != 0) { error_sys("must be called by root"); exit(-1); } /* fork child that will become prelude-manager */ if ((pid = fork()) < 0) error_sys("fork error"); else { if (pid == 0) { /* We're the child */ char *args[MAX_ARGS]; unsigned int i; /* Become session leader */ setsid(); /* Change working directory to root directory. The current working directory could be a mounted filesystem; if the daemon stays on a mounted filesystem it could prevent the filesystem from being umounted. */ chdir("/"); /* Clear out file creation mask */ umask(0); /* Close unneeded file descriptors */ maxfd = (int) sysconf(_SC_OPEN_MAX); if (maxfd == -1) maxfd = getdtablesize(); for (s = 3; s < maxfd; s++) (void) close(s); /* Increase limit on number of open file descriptors if necessary */ maxfd = fdlim_get(1); if (maxfd < 0) error_sys("fdlim_get: bad value"); if (maxfd > MAXMAXFD) maxfd = MAXMAXFD; if (maxfd > fdlim_get(0)) fdlim_set(maxfd); /* Build calling argv */ args[0] = PRELUDE_MANAGER_PATH; for (i=1;i<argc;i++) { args[i] = argv[i]; } args[i++] = NULL; /* Finally transform self into prelude-manager */ if (execvp(PRELUDE_MANAGER_PATH, args) < 0) error_sys("execve error"); else ; /* avoid if-then ambiguity */ } else { /* We're the parent Terminate */ exit(0); } } }