Re: [PATCH v4 02/16] Move crypto.c into libutil
[notmuch-archives.git] / 8e / ab5670c69dbf0828a17be23ef1c64cb4a4e8df
1 Return-Path: <too@guru-group.fi>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 60B4B431FAF\r
6         for <notmuch@notmuchmail.org>; Tue, 18 Sep 2012 07:23:13 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id qhNImnqC3i5Y for <notmuch@notmuchmail.org>;\r
16         Tue, 18 Sep 2012 07:23:12 -0700 (PDT)\r
17 Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
18         by olra.theworths.org (Postfix) with ESMTP id 30F80431FAE\r
19         for <notmuch@notmuchmail.org>; Tue, 18 Sep 2012 07:23:12 -0700 (PDT)\r
20 Received: by guru.guru-group.fi (Postfix, from userid 501)\r
21         id 2956E1002A4; Tue, 18 Sep 2012 17:23:17 +0300 (EEST)\r
22 From: Tomi Ollila <tomi.ollila@iki.fi>\r
23 To: notmuch@notmuchmail.org\r
24 Subject: [PATCH V3 1/2] test/smtp-dummy: add --background option and\r
25         functionality\r
26 Date: Tue, 18 Sep 2012 17:23:01 +0300\r
27 Message-Id: <1347978182-8771-1-git-send-email-tomi.ollila@iki.fi>\r
28 X-Mailer: git-send-email 1.7.1\r
29 Cc: Tomi Ollila <too@iki.fi>\r
30 X-BeenThere: notmuch@notmuchmail.org\r
31 X-Mailman-Version: 2.1.13\r
32 Precedence: list\r
33 List-Id: "Use and development of the notmuch mail system."\r
34         <notmuch.notmuchmail.org>\r
35 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
36         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
37 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
38 List-Post: <mailto:notmuch@notmuchmail.org>\r
39 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
40 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
42 X-List-Received-Date: Tue, 18 Sep 2012 14:23:13 -0000\r
43 \r
44 From: Tomi Ollila <too@iki.fi>\r
45 \r
46 When shell executes background process using '&' the scheduling of\r
47 that new process is arbitrary. It could be that smtp-dummy doesn't\r
48 get execution time to listen() it's server socket until some other\r
49 process attempts to connect() to it. The --background option in\r
50 smtp-dummy makes it to go background *after* it started to listen\r
51 its server socket.\r
52 \r
53 When --background option is used, the line "smtp_dummy_pid='<pid>'"\r
54 is printed to stdout from where shell can eval it.\r
55 ---\r
56 \r
57 This is v3 of id:"1323766883-17607-1-git-send-email-tomi.ollila@iki.fi"\r
58 \r
59 addressing (some) Dmitry's comments.\r
60 \r
61  test/smtp-dummy.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-\r
62  1 file changed, 54 insertions(+), 1 deletion(-)\r
63 \r
64 diff --git a/test/smtp-dummy.c b/test/smtp-dummy.c\r
65 index 86d4316..bb13668 100644\r
66 --- a/test/smtp-dummy.c\r
67 +++ b/test/smtp-dummy.c\r
68 @@ -119,6 +119,7 @@ do_smtp_to_file (FILE *peer, FILE *output)\r
69  int\r
70  main (int argc, char *argv[])\r
71  {\r
72 +       const char * progname;\r
73         char *output_filename;\r
74         FILE *peer_file, *output;\r
75         int sock, peer, err;\r
76 @@ -126,9 +127,31 @@ main (int argc, char *argv[])\r
77         struct hostent *hostinfo;\r
78         socklen_t peer_addr_len;\r
79         int reuse;\r
80 +       int background;\r
81 +\r
82 +       progname = argv[0];\r
83 +\r
84 +       background = 0;\r
85 +       for (; argc >= 2; argc--, argv++) {\r
86 +               if (argv[1][0] != '-')\r
87 +                       break;\r
88 +               if (strcmp (argv[1], "--") == 0) {\r
89 +                       argc--;\r
90 +                       argv++;\r
91 +                       break;\r
92 +               }\r
93 +               if (strcmp (argv[1], "--background") == 0) {\r
94 +                       background = 1;\r
95 +                       continue;\r
96 +               }\r
97 +               fprintf(stderr, "%s: unregognized option '%s'\n",\r
98 +                       progname, argv[1]);\r
99 +               return 1;\r
100 +       }\r
101  \r
102         if (argc != 2) {\r
103 -               fprintf (stderr, "Usage: %s <output-file>\n", argv[0]);\r
104 +               fprintf (stderr,\r
105 +                        "Usage: %s [--background] <output-file>\n", progname);\r
106                 return 1;\r
107         }\r
108  \r
109 @@ -181,6 +204,36 @@ main (int argc, char *argv[])\r
110                 return 1;\r
111         }\r
112  \r
113 +       if (background) {\r
114 +               int pid = fork ();\r
115 +               if (pid > 0) {\r
116 +                       printf ("smtp_dummy_pid='%d'\n", pid);\r
117 +                       fflush (stdout);\r
118 +                       close (sock);\r
119 +                       return 0;\r
120 +               }\r
121 +               if (pid < 0) {\r
122 +                       fprintf (stderr, "Error: fork() failed: %s\n",\r
123 +                                strerror (errno));\r
124 +                       close (sock);\r
125 +                       return 1;\r
126 +               }\r
127 +               /* Reached if pid == 0 (the child process). */\r
128 +               /* Close stdout so that the one interested in pid value will\r
129 +                  also get EOF. */\r
130 +               close (STDOUT_FILENO);\r
131 +               /* dup2() will re-reserve fd of stdout (1) (opportunistically),\r
132 +                  in case fd of stderr (2) is open. If that was not open we\r
133 +                  don't care fd of stdout (1) either. */\r
134 +               dup2 (STDERR_FILENO, STDOUT_FILENO);\r
135 +\r
136 +               /* This process is now out of reach of shell's job control.\r
137 +                  To resolve the rare but possible condition where this\r
138 +                  "daemon" is started but never connected this process will\r
139 +                  (only) have 30 seconds to exist. */\r
140 +               alarm (30);\r
141 +       }\r
142 +\r
143         peer_addr_len = sizeof (peer_addr);\r
144         peer = accept (sock, (struct sockaddr *) &peer_addr, &peer_addr_len);\r
145         if (peer == -1) {\r
146 -- \r
147 1.7.11.4\r
148 \r