Mercurial > hg > mlmmj
view src/mlmmj-recieve.c @ 52:e8b0cd47fe1e
mlmmj-recieve fork()
author | mmj |
---|---|
date | Wed, 28 Apr 2004 01:47:43 +1000 |
parents | 4a80ff540431 |
children | 990e1a4ae6d0 |
line wrap: on
line source
/* Copyright (C) 2002, 2003 Mads Martin Joergensen <mmj at mmj.dk> * * $Id$ * * This file is redistributable under version 2 of the GNU General * Public License as described at http://www.gnu.org/licenses/gpl.txt */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <libgen.h> #include "mlmmj.h" #include "wrappers.h" #include "mygetline.h" #include "strgen.h" #include "log_error.h" extern char *optarg; static void print_help(const char *prg) { printf("Usage: %s -L /path/to/chat-list [-V] [-P]\n", prg); exit(EXIT_SUCCESS); } int main(int argc, char **argv) { char *infilename = NULL, *listdir = NULL, *line = NULL; char *randomstr = random_str(); char *mlmmjprocess, *argv0 = strdup(argv[0]); int fd, opt, noprocess = 0; pid_t childpid; log_set_name(argv[0]); mlmmjprocess = concatstr(2, dirname(argv0), "/mlmmj-process"); free(argv0); while ((opt = getopt(argc, argv, "hPVL:")) != -1) { switch(opt) { case 'h': print_help(argv[0]); break; case 'L': listdir = optarg; break; case 'P': noprocess = 1; break; case 'V': print_version(argv[0]); exit(0); } } if(listdir == NULL) { fprintf(stderr, "You have to specify -L\n"); fprintf(stderr, "%s -h for help\n", argv[0]); exit(EXIT_FAILURE); } infilename = concatstr(3, listdir, "/incoming/", randomstr); free(randomstr); fd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); while(fd < 0 && errno == EEXIST) { free(infilename); randomstr = random_str(); infilename = concatstr(3, listdir, "/incoming/", randomstr); free(randomstr); fd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); } if(fd < 0) { log_error(LOG_ARGS, "could not create mail file in incoming directory"); free(infilename); exit(EXIT_FAILURE); } while((line = myfgetline(stdin))) { writen(fd, line, strlen(line)); fsync(fd); free(line); } printf("mlmmj-recieve: wrote %s\n", infilename); close(fd); if(noprocess) { free(infilename); exit(EXIT_SUCCESS); } /* * Now we fork so we can exit with success since it could potentially * take a long time for mlmmj-send to finish delivering the mails and * returning, making it susceptible to getting a SIGKILL from the * mailserver invoking mlmmj-recieve. */ childpid = fork(); if(childpid < 0) log_error(LOG_ARGS, "fork() failed! Proceeding anyway"); if(childpid) exit(EXIT_SUCCESS); /* Parent says: "bye bye kids!"*/ execlp(mlmmjprocess, mlmmjprocess, "-L", listdir, "-m", infilename, 0); log_error(LOG_ARGS, "execlp() of '%s' failed", mlmmjprocess); return EXIT_FAILURE; }