introduce INIT_STDERR_LOCKING macro
[paraslash.git] / recv.c
1 /*
2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file recv.c the stand-alone receiver */
20
21 #include "para.h"
22
23 #include "list.h"
24 #include "sched.h"
25 #include "recv.h"
26 #include "recv.cmdline.h"
27 #include "fd.h"
28 #include "error.h"
29 #include "stdout.h"
30
31 struct recv_args_info conf;
32
33 INIT_RECV_ERRLISTS;
34
35
36 INIT_STDERR_LOGGING(conf.loglevel_arg);
37 static void *parse_config(int argc, char *argv[], int *receiver_num)
38 {
39 int i;
40
41 if (recv_cmdline_parser(argc, argv, &conf))
42 return NULL;
43 if (conf.list_receivers_given) {
44 printf("available receivers: ");
45 for (i = 0; receivers[i].name; i++)
46 printf("%s%s", i? " " : "", receivers[i].name);
47 printf("\nTry\n\tpara_recv -r '<receivername> -h'\n"
48 "for help on <receivername>.\n");
49 exit(EXIT_SUCCESS);
50 }
51 return check_receiver_arg(conf.receiver_arg, receiver_num);
52 }
53
54 static void rn_event_handler(struct task *t)
55 {
56 struct receiver_node *rn = t->private_data;
57 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
58 rn->eof = 1;
59 unregister_task(t);
60 }
61
62 int main(int argc, char *argv[])
63 {
64 int ret, r_opened = 0, receiver_num;
65 struct receiver *r = NULL;
66 struct receiver_node rn;
67 struct stdout_task sot;
68 struct sched s;
69
70 s.default_timeout.tv_sec = 1;
71 s.default_timeout.tv_usec = 0;
72
73 memset(&rn, 0, sizeof(struct receiver_node));
74 for (ret = 0; receivers[ret].name; ret++)
75 receivers[ret].init(&receivers[ret]);
76 ret = -E_RECV_SYNTAX;
77 rn.conf = parse_config(argc, argv, &receiver_num);
78 if (!rn.conf) {
79 PARA_EMERG_LOG("%s", "parse failed\n");
80 goto out;
81 }
82 r = &receivers[receiver_num];
83 rn.receiver = r;
84 ret = r->open(&rn);
85 if (ret < 0)
86 goto out;
87 r_opened = 1;
88
89 stdout_set_defaults(&sot);
90 sot.buf = rn.buf;
91 sot.loaded = &rn.loaded;
92 sot.input_eof = &rn.eof;
93 register_task(&sot.task);
94
95 rn.task.private_data = &rn;
96 rn.task.pre_select = r->pre_select;
97 rn.task.post_select = r->post_select;
98 rn.task.event_handler = rn_event_handler;
99 sprintf(rn.task.status, "receiver node");
100 register_task(&rn.task);
101
102 ret = sched(&s);
103 out:
104 if (r_opened)
105 r->close(&rn);
106 if (r)
107 r->shutdown();
108 if (ret < 0)
109 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
110 return ret;
111 }