void flushtyping(int clearesc) { Text *t; ulong n; if(clearesc) typeesc = -1; if(typestart == typeend) { modified = 0; return; } t = which->user1; if(t != &cmd) modified = 1; rload(&t->rasp, typestart, typeend, &n); scratch[n] = 0; if(t==&cmd && typeend==t->rasp.nrunes && scratch[typeend-typestart-1]=='\n'){ setlock(); outcmd(); } outTslS(Ttype, t->tag, typestart, scratch); typestart = -1; typeend = -1; XFlush(_dpy); }
/* * Write the line into file (with lock) */ static int sql_log_write(rlm_sql_log_t *inst, REQUEST *request, const char *line) { int fd; FILE *fp; int locked = 0; struct stat st; char *p, path[1024]; path[0] = '\0'; radius_xlat(path, sizeof(path), inst->path, request, NULL, NULL); if (path[0] == '\0') { return RLM_MODULE_FAIL; } p = strrchr(path, '/'); if (p) { *p = '\0'; rad_mkdir(path, 0755); *p = '/'; } while (!locked) { if ((fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0666)) < 0) { radlog_request(L_ERR, 0, request, "Couldn't open file %s: %s", path, strerror(errno)); return RLM_MODULE_FAIL; } if (setlock(fd) != 0) { radlog_request(L_ERR, 0, request, "Couldn't lock file %s: %s", path, strerror(errno)); close(fd); return RLM_MODULE_FAIL; } if (fstat(fd, &st) != 0) { radlog_request(L_ERR, 0, request, "Couldn't stat file %s: %s", path, strerror(errno)); close(fd); return RLM_MODULE_FAIL; } if (st.st_nlink == 0) { RDEBUG("File %s removed by another program, retrying", path); close(fd); continue; } locked = 1; } if ((fp = fdopen(fd, "a")) == NULL) { radlog_request(L_ERR, 0, request, "Couldn't associate a stream with file %s: %s", path, strerror(errno)); close(fd); return RLM_MODULE_FAIL; } fputs(line, fp); putc('\n', fp); fclose(fp); /* and unlock */ return RLM_MODULE_OK; }
void menu2hit(void) { Text *t=(Text *)which->user1; int w = which-t->l; int m; m = menuhit(2, &mouse, t==&cmd? &menu2c : &menu2); if(lock || t->lock) return; switch(m){ case Cut: cut(t, w, true, true); break; case Paste: paste(t, w); break; case Snarf: snarf(t, w); break; case Exch: snarf(t, w); outT0(Tstartsnarf); setlock(); break; case Look: outTsll(Tlook, t->tag, which->p0, which->p1); setlock(); break; case Search: outcmd(); if(t==&cmd) outTsll(Tsend, 0 /*ignored*/, which->p0, which->p1); else outT0(Tsearch); setlock(); break; } }
void unlock(fd) { if (setlock(fd, F_UNLCK) < 0) { printf("fcntl [%d]unlock error: %s, fd:%d.\n", getpid(), strerror(errno), fd); } else { printf("fcntl [%d]unlock succ. fd:%d\n", getpid(), fd); } }
void *produce(void *arg){ int i; for(i=0;i<nitems;i++){ sem_wait(&consumed); setlock(); sched_yield(); buff[i%NBUFF]=i; /* store i into circular buffer */ setunlock(); sem_post(&produced); } if(i==(nitems/2)) return(NULL); }
int historycmd(int argc, char *argv[]) { int fd=0; int res=0; int i=0; char *line = NULL; FILE *fp; // mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; if ( cbsd_enable_history == 0 ) return 0; // do not logging history command in history if (!strcmp(argv[1],"history")) return 0; if (!strcmp(argv[1],"sudo")) return 0; if ((fd = open(cbsd_history_file, O_RDWR | O_APPEND | O_CREAT, (mode_t)0600 )) == -1) { return 0; } if (NULL == (fp = fdopen(fd, "a"))) { close(fd); return 1; } if (setlock(fd)!=0) return 1; //locked //rotate first line if necessary if (check_history_size(cbsd_history_file)!=0) removeline(fd,1); for (i = 1; i < argc; i++) res += strlen(argv[i])+1; res = res + 5; //+5: "cbsd " before and + "\n" at the end line=calloc(res,sizeof(char)); strcpy(line,"cbsd "); for (i = 1; i < argc; i++) { strcat(line, argv[i]); strcat(line," " ); } line[res-1] = '\n'; fputs(line,fp); free(line); fflush(fp); fclose(fp); return 0; }
int main(int argc, char **argv) { int fd; fd = open(argv[1],O_RDWR | O_CREAT,0666); if( fd < 0 ){ perror("open"); exit(EXIT_FAILURE); } setlock(fd,F_RDLCK); printf("PID %d unlocked %s\n",getpid(),argv[1]); getchar(); setlock(fd,F_UNLCK); printf("PID %d unlocked %s\n",getpid(),argv[1]); getchar(); setlock(fd,F_WRLCK); printf("PID %d write locked %s\n",getpid(),argv[1]); getchar(); close(fd); exit(EXIT_FAILURE); }
void *consume(void *arg){ int i; for(i=0;i<nitems;i++){ sem_wait(&produced); setlock(); sched_yield(); if(i!=buff[i%NBUFF]) printf("out of sync: buff[%d] = %d \n",i,buff[i%NBUFF]); else printf("buff[%d] = %d \n", i, buff[i % NBUFF]); setunlock(); sem_post(&consumed); } return(NULL); }
void getlock(fd) { if (setlock(fd, F_WRLCK) < 0) { if (errno == EACCES || errno == EAGAIN) { printf("fcntl :[%d]file already locked by other process, fd:%d.\n", getpid(), fd); } else { printf("fcntl [%d]locked file error, fd:%d.\n", getpid(), fd); exit(1); } } else { printf("fcntl [%d]get lock succ %d.\n", getpid(), fd); } }
void menu3hit(void) { Rectangle r; Flayer *l; int m, i; Text *t; mw = -1; switch(m = menuhit(3, &mouse, &menu3)){ case -1: break; case New: if(!lock) sweeptext(1, 0); break; case Xerox: case Reshape: if(!lock){ cursorswitch(&bullseye); buttons(Down); if((mouse.buttons&4) && (l = flwhich(mouse.xy)) && getr(&r)) duplicate(l, r, l->f.font, m==Reshape); else cursorswitch(cursor); buttons(Up); } break; case Close: if(!lock){ cursorswitch(&bullseye); buttons(Down); if((mouse.buttons&4) && (l = flwhich(mouse.xy)) && !lock){ t=(Text *)l->user1; if (t->nwin>1) closeup(l); else if(t!=&cmd) { outTs(Tclose, t->tag); setlock(); } } cursorswitch(cursor); buttons(Up); } break; case Write: if(!lock){ cursorswitch(&bullseye); buttons(Down); if((mouse.buttons&4) && (l = flwhich(mouse.xy))){ outTs(Twrite, ((Text *)l->user1)->tag); setlock(); }else cursorswitch(cursor); buttons(Up); } break; default: if(t = text[m-NMENU3]){ i = t->front; if(t->nwin==0 || t->l[i].textfn==0) return; /* not ready yet; try again later */ if(t->nwin>1 && which==&t->l[i]) do if(++i==NL) i = 0; while(i!=t->front && t->l[i].textfn==0); current(&t->l[i]); }else if(!lock) sweeptext(0, tag[m-NMENU3]); break; } }
void EffectsPanel::OnLockButtonClick(wxCommandEvent& event) { wxButton * button = (wxButton*)event.GetEventObject(); setlock(button); }
int main(int argc, char *argv[]) { GtkBuilder *builder; GString *txt = g_string_new_len("", 20); char filepad[100]; unsigned short i, mask=0; GdkScreen* screen = NULL; int xx, yy, screen_width, screen_height; InitAudio(); pad = get_real_path(); // get the path of the executable sprintf(filepad, "%s/.lock", pad); setlock(filepad, 10, TRUE); // disable multiple instances for at least 10 seconds g_thread_init (NULL); display = XOpenDisplay(0); if (display == NULL) exit(1); gtk_set_locale(); gtk_init(&argc, &argv); builder = gtk_builder_new (); sprintf(filepad, "%s/%s.glade", pad, SKIN); gtk_builder_add_from_file(builder, filepad, NULL); gtk_builder_connect_signals (builder, NULL); // set widgets to be handled by the event handlers window = GW ("window"); preferences = GW ("preferences"); lookup = GW ("lookup"); fontbutton = GW ("fontbutton1"); drag = GW ("dragbar"); debug = GW ("debug"); papier = GW ("drawingarea1"); colorbutton1 = GW ("colorbutton1"); colorbutton2 = GW ("colorbutton2"); checkbutton1 = GW ("checkbutton1"); entry1 = GW ("entry1"); entry2 = GW ("entry2"); gtktable1 = GW ("table1"); checkbutton8 = GW ("checkbutton8"); checkbutton9 = GW ("checkbutton9"); textview1 = GW ("textview1"); entry16 = GW ("entry16"); combobox1 = GW ("combobox1"); scrolled = GW ("scrolledwindow2"); opacity = GTK_ADJUSTMENT (gtk_builder_get_object (builder, "adjustment1")); speed = GTK_ADJUSTMENT (gtk_builder_get_object (builder, "adjustment2")); textbuffer1 = GTK_TEXT_BUFFER(gtk_builder_get_object (builder, "textbuffer1")); for (i=0; i<sizeof(unsigned short); i++) { // set each bit of mask mask <<= 8; mask |= 0xFF; } // fill the struct that keeps the 7 recognize buttons & checkboxes (in preferences) for (i=0; i<7; i++) { g_string_sprintf(txt, "checkbutton%d", i+2); modebits[i].check = GTK_WIDGET(gtk_builder_get_object (builder, txt->str)); g_string_sprintf(txt, "button%d", i+3); modebits[i].button = GTK_WIDGET(gtk_builder_get_object (builder, txt->str)); switch (i) { case 0: modebits[i].bit = (GB1|GB2); modebits[i].mask = ((GB1|GB2) ^ mask); break; case 1: modebits[i].bit = BIG5; modebits[i].mask = (BIG5 ^ mask); break; case 2: modebits[i].bit = DIGITS; modebits[i].mask = (DIGITS ^ mask); break; case 3: modebits[i].bit = LOWERCASE; modebits[i].mask = (LOWERCASE ^ mask); break; case 4: modebits[i].bit = UPPERCASE; modebits[i].mask = (UPPERCASE ^ mask); break; case 5: modebits[i].bit = PUNC; modebits[i].mask = (PUNC ^ mask); break; case 6: modebits[i].bit = DEFAULT; modebits[i].mask = (DEFAULT ^ mask); break; } } // fill the structure that keeps the 13 labels for the keys (preferences) for (i=0; i<13; i++) { g_string_sprintf(txt, "button%d", i+10); conf.defkey[i].button = GTK_WIDGET(gtk_builder_get_object (builder, txt->str)); g_string_sprintf(txt, "entry%d", i+3); conf.defkey[i].entry = GTK_WIDGET(gtk_builder_get_object (builder, txt->str)); conf.defkey[i].key = 0; } // place a simple combobox for the input selection (preferences) combo = gtk_combo_box_new_text(); gtk_table_attach_defaults(GTK_TABLE(gtktable1), combo, 1, 2, 2, 3); gtk_widget_show (combo); // get events and labels for the 9 candidates for (int i=0; i< 9; i++) { g_string_sprintf(txt, "knop%d", i+1); knop[i] = GTK_WIDGET (gtk_builder_get_object (builder, txt->str)); g_string_sprintf(txt, "event%d", i+1); event[i] = GTK_WIDGET (gtk_builder_get_object (builder, txt->str)); } // set events for paste and backspace entries (preferences) g_signal_connect(entry1, "key_press_event", G_CALLBACK(on_key_press), NULL); g_signal_connect(entry1, "key_release_event", G_CALLBACK(on_key_release), NULL); g_signal_connect(entry2, "key_press_event", G_CALLBACK(on_key_press), NULL); g_signal_connect(entry2, "key_release_event", G_CALLBACK(on_key_release), NULL); if (!create_wtpen_window()) exit(1); wtpen_init(); g_object_unref (G_OBJECT (builder)); gtk_widget_show_all(GTK_WIDGET(window)); for (i=0; i<NUMKEYS; i++) hotkey[i] = 0; // reset all hotkeys // load cedict from file sprintf(filepad, "%s/data", pad); import_cedict(filepad); // load settings from file sprintf(filepad, "%s/xpen.cfg", pad); load_preferences(filepad); // set background and shape sprintf(filepad, "%s/%s.png", pad, SKIN); set_window_shape(filepad); // some styles to be used in the cedict browser (lookup window)一 gtk_text_buffer_create_tag(textbuffer1, "mark", "background", "yellow", "foreground", "black", NULL); gtk_text_buffer_create_tag(textbuffer1, "bold", "weight", PANGO_WEIGHT_BOLD, NULL); gtk_text_buffer_create_tag(textbuffer1, "character", "font", conf.font, "scale", 1.3, NULL); gtk_text_buffer_create_tag(textbuffer1, "translation", "scale", 0.9, NULL); gtk_text_buffer_create_tag(textbuffer1, "pinyin", "scale", 1.1, NULL); ready = TRUE; // let_configure_event() know that it can start its setup // start monitoring system-wide keypresses g_thread_create (intercept_key_thread, NULL, FALSE, NULL); // make sure the window positions and size are legal screen = gtk_window_get_screen(GTK_WINDOW(window)); // get screen size screen_width = gdk_screen_get_width(screen); screen_height = gdk_screen_get_height(screen); xx = MAX(MIN(screen_width-width, conf.x), 0); // set the position of the main window yy = MAX(MIN(screen_height-height, conf.y), 0); gtk_widget_set_uposition(window, xx, yy); xx = MIN(screen_width, conf.dx); // set the size of the lookup window yy = MIN(screen_height, conf.dy); gtk_widget_set_usize(lookup, xx, yy); xx = MAX(MIN(screen_width-conf.dx, conf.lx), 0); // set the position of the lookup window yy = MAX(MIN(screen_height-conf.dy, conf.ly), 0); gtk_widget_set_uposition(lookup, xx, yy); g_timeout_add (100, checkfocus, NULL); // check the inputfocus (each 1/10s) ////////////////////////////////////////////////////////////// //on_full_screen_button_pressed(); // start the full screen mode ////////////////////////////////////////////////////////////// gtk_main(); // start the main loop sprintf(filepad, "%s/.lock", pad); // remove the lock setlock(filepad, 0, FALSE); // save all settings to file sprintf(filepad, "%s/xpen.cfg", pad); save_preferences (filepad); g_free(pad); free_all(); wtpen_window_done(); wtlib_done(); SDL_CloseAudio(); SDL_Quit(); //on_full_screen_button_pressed(); return 0; }
int main(int argc, char *argv[]) { setprogname(argv[0]); int lock = 0; int set = 0; int dump = 0; int restore = 0; int verbose = 0; int test = 0; bool binary = false; char* filename = NULL; char* filename2 = NULL; int error = 0; int fd; int c; while ((c = getopt(argc, argv, "ulvw:r:R:W:bt:o:")) != -1) switch (c) { case 'u' : lock = 0; set = 1; break; case 'l' : lock = 1; set = 1; break; case 'b': binary = true; break; case 'r': restore = 1; filename = optarg; break; case 'v': verbose=1; break; case 'w': dump=1; filename=optarg; break; case 'R': restore = 1; set = 1; filename = optarg; break; case 'W': dump = 1; set = 1; filename = optarg; break; case 't': test=1; filename = optarg; break; case 'o': filename2 = optarg; break; case '?' : default: usage(); } if (set == 0 && dump == 0 && restore == 0 && test == 0) usage(); if (dump == 1 && restore == 1) usage(); if (test == 1) { if (filename2 == NULL) { fprintf(stderr, "-o <file> is required when using -t\n"); err(EXIT_FAILURE, NULL); } error = test_ascii_dump(verbose, filename, filename2); } else { fd = open(pf_device, O_RDWR); if (fd == -1) err(EXIT_FAILURE, "Cannot open %s", pf_device); if (set != 0 && dump == 0 && restore == 0) error = setlock(fd, verbose, lock); if (dump) { if (set) error = setlock(fd, verbose, 1); if (binary) error = dump_states_binary(fd, verbose, filename); else error = dump_states_ascii(fd, verbose, filename); if (set) error = setlock(fd, verbose, 0); } if (restore) { if (set) error = setlock(fd, verbose, 1); if (binary) error = restore_states_binary(fd, verbose, filename); else error = restore_states_ascii(fd, verbose, filename); if (set) error = setlock(fd, verbose, 0); } close(fd); } return error; }
int main(int argc, const char **argv) { int opt; int i; unsigned long ul; progname =argv[0]; for (i =str_len(progname); i; --i) if (progname[i -1] == '/') { progname +=i; break; } if (progname[0] == 'd') ++progname; /* argv[0] */ if (str_equal(progname, "setuidgid")) setuidgid(argc, argv); if (str_equal(progname, "envuidgid")) envuidgid(argc, argv); if (str_equal(progname, "envdir")) envdir(argc, argv); if (str_equal(progname, "pgrphack")) pgrphack(argc, argv); if (str_equal(progname, "setlock")) setlock(argc, argv); if (str_equal(progname, "softlimit")) softlimit(argc, argv); while ((opt =getopt(argc, argv, "u:U:b:e:m:d:o:p:f:c:r:t:/:n:l:L:vP012V")) != opteof) switch(opt) { case 'u': set_user =(char*)optarg; break; case 'U': env_user =(char*)optarg; break; case 'b': argv0 =(char*)optarg; break; case 'e': env_dir =optarg; break; case 'm': if (optarg[scan_ulong(optarg, &ul)]) usage(); limits =limitl =limita =limitd =ul; break; case 'd': if (optarg[scan_ulong(optarg, &ul)]) usage(); limitd =ul; break; case 'o': if (optarg[scan_ulong(optarg, &ul)]) usage(); limito =ul; break; case 'p': if (optarg[scan_ulong(optarg, &ul)]) usage(); limitp =ul; break; case 'f': if (optarg[scan_ulong(optarg, &ul)]) usage(); limitf =ul; break; case 'c': if (optarg[scan_ulong(optarg, &ul)]) usage(); limitc =ul; break; case 'r': if (optarg[scan_ulong(optarg, &ul)]) usage(); limitr =ul; break; case 't': if (optarg[scan_ulong(optarg, &ul)]) usage(); limitt =ul; break; case '/': root =optarg; break; case 'n': switch (*optarg) { case '-': if (optarg[scan_ulong(++optarg, &ul)]) usage(); nicelvl =ul; nicelvl *=-1; break; case '+': ++optarg; default: if (optarg[scan_ulong(optarg, &ul)]) usage(); nicelvl =ul; break; } break; case 'l': if (lock) usage(); lock =optarg; lockdelay =1; break; case 'L': if (lock) usage(); lock =optarg; lockdelay =0; break; case 'v': verbose =1; break; case 'P': pgrp =1; break; case '0': nostdin =1; break; case '1': nostdout =1; break; case '2': nostderr =1; break; case 'V': strerr_warn1("$Id: f279d44141c981dd7535a12260efcf1ef7beed26 $", 0); case '?': usage(); } argv +=optind; if (! argv || ! *argv) usage(); if (pgrp) setsid(); if (env_dir) edir(env_dir); if (root) { if (chdir(root) == -1) fatal2("unable to change directory", root); if (chroot(".") == -1) fatal("unable to change root directory"); } if (nicelvl) { errno =0; if (nice(nicelvl) == -1) if (errno) fatal("unable to set nice level"); } if (env_user) euidgid(env_user, 1); if (set_user) suidgid(set_user, 1); if (lock) slock(lock, lockdelay, 0); if (nostdin) if (close(0) == -1) fatal("unable to close stdin"); if (nostdout) if (close(1) == -1) fatal("unable to close stdout"); if (nostderr) if (close(2) == -1) fatal("unable to close stderr"); slimit(); progname =*argv; if (argv0) *argv =argv0; pathexec_env_run(progname, argv); fatal2("unable to run", *argv); return(0); }
void flush_dir(char *ndir) { struct dirent *de; DIR *dp; FILE *fp, *inf, *ouf; faddr noden, *bestaka; fidoaddr nodenr; int flavor, mode, Attach, fage, first, bread, rc; int fsize; char *p, *temp, *fname, *arcfile, *pktfile, *ext, maxnr, nr, oldnr, *buf; time_t Now; struct tm *ptm; struct stat sbuf; fd_list *fdl = NULL; temp = calloc(PATH_MAX, sizeof(char)); snprintf(temp, PATH_MAX, "%s/%s", CFG.out_queue, ndir); if (chdir(temp) == -1) { WriteError("$Error chdir to %s", temp); free(temp); return; } /* * Get the nodenumber from the filename */ noden.domain = NULL; noden.name = NULL; noden.zone = atoi(strtok(ndir, ".")); noden.net = atoi(strtok(NULL, ".")); noden.node = atoi(strtok(NULL, ".")); noden.point = atoi(strtok(NULL, "\0")); if (SearchFidonet(noden.zone)) noden.domain = xstrcpy(fidonet.domain); memset(&nodenr, 0, sizeof(nodenr)); nodenr.zone = noden.zone; nodenr.net = noden.net; nodenr.node = noden.node; nodenr.point = noden.point; snprintf(nodenr.domain, 13, "%s", noden.domain); if (!SearchNode(nodenr)) { /* * Node not in setup, blank noderecord and fill in some details * so that we are able to send mail crash or immediate. */ Syslog('+', "Node %s not in setup, using default settings", aka2str(nodenr)); memset(&nodes, 0, sizeof(nodes)); nodes.Aka[0].zone = noden.zone; nodes.Aka[0].net = noden.net; nodes.Aka[0].node = noden.node; nodes.Aka[0].point = noden.point; snprintf(nodes.Archiver, 6, (char *)"ZIP"); } /* * If we route via another aka, change everything. */ if (nodes.RouteVia.zone) { p = xstrcpy(aka2str(nodenr)); Syslog('+', "Route to %s via %s", p, aka2str(nodes.RouteVia)); free(p); noden.zone = nodes.RouteVia.zone; noden.net = nodes.RouteVia.net; noden.node = nodes.RouteVia.node; noden.point = nodes.RouteVia.point; if (noden.domain) free(noden.domain); noden.domain = xstrcpy(nodes.RouteVia.domain); /* * Load routevia noderecord to get the correct flavor. * If there is no noderecord, reload the old one. */ if (!SearchNode(nodes.RouteVia)) SearchNode(nodenr); } /* * At this point we are ready to add everything to the real outbound. * Lock the node, if this fails because the node is busy we abort * and try to add at a later time. */ if (nodes.Session_out == S_DIR) { if (islocked(nodes.Dir_out_clock, nodes.Dir_out_chklck, nodes.Dir_out_waitclr, 'p')) { Syslog('+', "Mail and files stay in queue, will be added later"); if (noden.domain) free(noden.domain); free(temp); return; } else { if (! setlock(nodes.Dir_out_mlock, nodes.Dir_out_mklck, 'p')) { Syslog('+', "Mail and files stay in queue, will be added later"); if (noden.domain) free(noden.domain); free(temp); return; } } } else { if (nodelock(&noden, mypid)) { Syslog('+', "Mail and files stay in queue, will be added later"); if (noden.domain) free(noden.domain); free(temp); return; } } /* * Create arcmail filename for this node. */ Now = time(NULL); arcfile = calloc(PATH_MAX, sizeof(char)); if (nodes.Session_out == S_DIR) { ptm = localtime(&Now); ext = dow[ptm->tm_wday]; if (!nodes.ARCmailCompat && (nodes.Aka[0].zone != noden.zone)) { /* * Generate ARCfile name from the CRC of the ASCII string of the node address. */ snprintf(arcfile, PATH_MAX, "%s/%08x.%s0", nodes.Dir_out_path, StringCRC32(ascfnode(&noden, 0x1f)), ext); } else { bestaka = bestaka_s(&noden); if (noden.point) { snprintf(arcfile, PATH_MAX, "%s/%04x%04x.%s0", nodes.Dir_out_path, ((bestaka->net) - (noden.net)) & 0xffff, ((bestaka->node) - (noden.node) + (noden.point)) & 0xffff, ext); } else if (bestaka->point) { /* * Inserted the next code for if we are a point, * I hope this is ARCmail 0.60 compliant. 21-May-1999 */ snprintf(arcfile, PATH_MAX, "%s/%04x%04x.%s0", nodes.Dir_out_path, ((bestaka->net) - (noden.net)) & 0xffff, ((bestaka->node) - (noden.node) - (bestaka->point)) & 0xffff, ext); } else { snprintf(arcfile, PATH_MAX, "%s/%04x%04x.%s0", nodes.Dir_out_path, ((bestaka->net) - (noden.net)) & 0xffff, ((bestaka->node) - (noden.node)) &0xffff, ext); } } } else { snprintf(arcfile, PATH_MAX, "%s", arcname(&noden, nodes.Aka[0].zone, nodes.ARCmailCompat)); } /* * If there is a mailpkt.qqq file, close it and rename it. */ pktfile = calloc(PATH_MAX, sizeof(char)); fname = calloc(PATH_MAX, sizeof(char)); snprintf(fname, PATH_MAX, "%s/mailpkt.qqq", temp); if (access(fname, W_OK) == 0) { snprintf(pktfile, PATH_MAX, "%s/%08x.pkt", temp, sequencer()); if (rename(fname, pktfile)) { WriteError("$Can't rename %s to %s", fname, pktfile); } else { /* * Add zero word to end of .pkt file */ if ((fp = fopen(pktfile, "a+")) == NULL) { WriteError("$Can't open %s", pktfile); } putc('\0', fp); putc('\0', fp); fsync(fileno(fp)); fclose(fp); } } free(fname); /* * Now all mail pkts are ready to archive */ if ((dp = opendir(temp)) == NULL) { WriteError("$Can't open %s", temp); free(temp); free(arcfile); if (nodes.Session_out == S_DIR) remlock(nodes.Dir_out_mlock, nodes.Dir_out_mklck, 'p'); else nodeulock(&noden, mypid); if (noden.domain) free(noden.domain); return; } /* * Read all .pkt filenames, get the timestamp and add them * to the memory array for later sort on filedate. */ while ((de = readdir(dp))) if ((strlen(de->d_name) == 12) && (strncasecmp(de->d_name+8,".pkt",4) == 0)) { stat(de->d_name, &sbuf); Syslog('p', "Adding %s to filelist", de->d_name); fill_fdlist(&fdl, de->d_name, sbuf.st_mtime); } closedir(dp); sort_fdlist(&fdl); if (getarchiver(nodes.Archiver)) { flavor = 'f'; if (nodes.Crash) flavor = 'c'; if (nodes.Hold) flavor = 'h'; } else { WriteError("Archiver %s not found", nodes.Archiver); if (noden.domain) free(noden.domain); free(temp); free(arcfile); return; } first = TRUE; while ((fname = pull_fdlist(&fdl)) != NULL) { /* * Check the size of the existing archive if there is a size limit. * Change to new archive names if the existing is too large. * If the archive size is zero, it's an already sent archive, the * number will be bumped also. * If the archive is older then 6 days, the name is also bumped. * Do this until we find a new name or if the last digit is a '9' or 'z'. * Purge archives older then toss_days. */ nr = oldnr = '0'; if (nodes.ARCmailAlpha) maxnr = 'z'; else maxnr = '9'; Attach = FALSE; for (;;) { fsize = file_size(arcfile); fage = (int)((Now - file_time(arcfile)) / 86400); if (fsize == -1L) { Attach = TRUE; break; } if (fsize == 0L) { if ((fage > 6) && (nr < maxnr)) { /* * Remove truncated ARCmail files older then 6 days. */ unlink(arcfile); fsize = -1L; Attach = TRUE; break; } /* * Increase filename extension if there is a truncated file of today. */ if (nr < maxnr) { nr++; if (nr == ('9' +1)) nr = 'a'; arcfile[strlen(arcfile) -1] = nr; } else { Syslog('!', "Warning: archive filename extensions exhausted for today"); break; } } else if (CFG.maxarcsize && (fsize > (CFG.maxarcsize * 1024)) && (nr < maxnr)) { /* * Use a new ARCmail file if the last one is too big. */ nr++; if (nr == ('9' +1)) nr = 'a'; arcfile[strlen(arcfile) -1] = nr; } fsize = file_size(arcfile); fage = (int)((Now - file_time(arcfile)) / 86400); if ((fsize > 0L) && (fage > 6) && (nr < maxnr)) { /* * If there is ARCmail of a week old or older, add mail * to a new ARCmail bundle. */ nr++; if (nr == ('9' +1)) nr = 'a'; arcfile[strlen(arcfile) -1] = nr; } if (oldnr == nr) break; else oldnr = nr; } fsize = file_size(arcfile); /* * If arcfile names were exhausted then the file ending on a z could still * be in the outbound but truncated if it has been sent. Since we are * reusing that filename (not a good solution) we must erase it or the * archiver program will complain. */ if (fsize == 0L) { unlink(arcfile); Attach = TRUE; } if (first) { Syslog('+', "Pack ARCmail for %s via %s with %s", aka2str(nodenr), ascfnode(&noden, 0x1f), nodes.Archiver); if (!do_quiet) { printf("\rAdding ARCmail for %s ", ascfnode(&noden, 0x1f)); fflush(stdout); } first = FALSE; flushed = TRUE; } if (execute_str(archiver.marc, arcfile, fname, (char *)"/dev/null", (char *)"/dev/null", (char *)"/dev/null") == 0) { unlink(fname); } else { WriteError("Can't add %s to ARCmail archive", fname); Attach = FALSE; } /* * Change filemode so downlink has rights to the file. */ if (nodes.Session_out == S_DIR) chmod(arcfile, 0660); /* * Attach file to .flo, not for FTP or Directory sessions. */ if (Attach && nodes.Session_out == S_DIRECT) attach(noden, arcfile, TFS, flavor); } /* * Open directory again. */ if ((dp = opendir(temp)) == NULL) { WriteError("$Can't open %s", temp); free(temp); free(arcfile); if (nodes.Session_out == S_DIR) remlock(nodes.Dir_out_mlock, nodes.Dir_out_mklck, 'p'); else nodeulock(&noden, mypid); if (noden.domain) free(noden.domain); return; } /* * Read all other mail filenames, get the timestamp and add them * to the memory array for later sort on filedate. */ while ((de = readdir(dp))) if ((strlen(de->d_name) == 11) && (strncasecmp(de->d_name,"mailpkt.",8) == 0)) { stat(de->d_name, &sbuf); Syslog('p', "Adding %s to filelist", de->d_name); fill_fdlist(&fdl, de->d_name, sbuf.st_mtime); } closedir(dp); sort_fdlist(&fdl); first = TRUE; while ((fname = pull_fdlist(&fdl)) != NULL) { if (first) { Syslog('+', "Pack unpacked mail for %s via %s", aka2str(nodenr), ascfnode(&noden, 0x1f)); if (!do_quiet) { printf("\rAdding netmail for %s ", ascfnode(&noden, 0x1f)); fflush(stdout); } first = FALSE; flushed = TRUE; } snprintf(pktfile, PATH_MAX, "%s/%s", temp, fname); if (strstr(fname, ".ddd")) flavor = 'd'; else if (strstr(fname, ".ccc")) flavor = 'c'; else if (strstr(fname, ".hhh")) flavor = 'h'; else flavor = 'o'; if (nodes.Session_out == S_DIR) { snprintf(arcfile, PATH_MAX, "%s/%08x.pkt", nodes.Dir_out_path, sequencer()); } else { snprintf(arcfile, PATH_MAX, "%s", pktname(&noden, flavor)); } /* * Now we must see if there is already mail in the outbound. * If that's the case, we must skip the .pkt header from the queue * because there is already a .pkt header also, append to the * outbound 2 bytes before the end of file, this is the zero word. */ if ((inf = fopen(pktfile, "r")) != NULL) { if (access(arcfile, R_OK) == -1) { ouf = fopen(arcfile, "w"); /* create new */ Syslog('p', "Create new %s", arcfile); } else { ouf = fopen(arcfile, "r+"); /* open R/W */ fseek(ouf, -2, SEEK_END); /* b4 0 word */ fseek(inf, 58, SEEK_SET); /* skip header */ Syslog('p', "Append to %s", arcfile); } if (ouf != NULL) { buf = malloc(16384); do { bread = fread(buf, 1, 16384, inf); fwrite(buf, 1, bread, ouf); } while (bread); free(buf); putc('\0', ouf); putc('\0', ouf); fsync(fileno(ouf)); fclose(ouf); fclose(inf); unlink(pktfile); } else { WriteError("$Can't open %s", arcfile); fclose(inf); } } /* * Change filemode so downlink has rights to the file. */ if (nodes.Session_out == S_DIR) chmod(arcfile, 0660); } /* * Now add the files for the node, information is in the .filelist * file, this tells the location of the file and what to do with * it after it is sent. */ snprintf(pktfile, PATH_MAX, "%s/.filelist", temp); if ((fp = fopen(pktfile, "r")) != NULL) { Syslog('+', "Adding files for %s via %s", aka2str(nodenr), ascfnode(&noden, 0x1f)); if (!do_quiet) { printf("\rAdding files for %s ", ascfnode(&noden, 0x1f)); fflush(stdout); flushed = TRUE; } buf = calloc(PATH_MAX + 1, sizeof(char)); while (fgets(buf, PATH_MAX, fp)) { Striplf(buf); Syslog('p', ".filelist: %s", buf); flavor = buf[0]; p = strchr(buf, ' '); p++; if (strncmp(p, "LEAVE ", 6) == 0) mode = LEAVE; else if (strncmp(p, "KFS ", 4) == 0) mode = KFS; else if (strncmp(p, "TFS ", 4) == 0) mode = TFS; else { WriteError("Syntax error in filelist \"%s\"", buf); mode = LEAVE; } p = strchr(p, ' '); p++; // Here is a extra now unused keyword. p = strchr(p, ' '); p++; Syslog('p', "File attach %s to %s", p, ascfnode(&noden, 0x1f)); if (nodes.Session_out == S_DIRECT) { attach(noden, p, mode, flavor); } else if (nodes.Session_out == S_DIR) { snprintf(arcfile, PATH_MAX, "%s/%s", nodes.Dir_out_path, Basename(p)); if (mode == LEAVE) { /* * LEAVE file, so we copy this one. */ rc = file_cp(p, arcfile); Syslog('p', "file_cp(%s, %s) rc=%d", p, arcfile, rc); } else { /* * KFS or TFS, move file to node directory */ rc = file_mv(p, arcfile); Syslog('p', "file_mv(%s, %s) rc=%d", p, arcfile, rc); } chmod(arcfile, 0660); } } free(buf); fclose(fp); unlink(pktfile); } /* * We are done, the queue is flushed, unlock the node. */ if (nodes.Session_out == S_DIR) remlock(nodes.Dir_out_mlock, nodes.Dir_out_mklck, 'p'); else nodeulock(&noden, mypid); if (noden.domain) free(noden.domain); free(temp); free(arcfile); free(pktfile); return; }