Remote protocol working.
[wrappi.git] / lib / wrappi.c
1 /* wrappi
2  * Copyright (C) 2011-2012 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26
27 #include "wrappi.h"
28 #include "internal.h"
29
30 wrap_h *
31 wrap_create (void)
32 {
33   struct wrap_h *w = calloc (1, sizeof *w);
34   if (w == NULL)
35     return NULL;
36
37   w->serial = random ();
38
39   return w;
40 }
41
42 void
43 wrap_close (wrap_h *w)
44 {
45   if (w->pid)
46     waitpid (w->pid, NULL, 0);
47
48   if (w->rfp != NULL)
49     fclose (w->rfp);
50
51   if (w->wfp != NULL)
52     fclose (w->wfp);
53
54   free (w->hostname);
55   free (w);
56 }
57
58 static void
59 set_error_buf (struct wrap_internal_h *w,
60                char *buf, int errnum, const char *func)
61 {
62   free (w->error);
63   w->error = buf;
64   w->errnum = errnum;
65
66   /* 'func' is always statically allocated, since it comes from
67    * C99 __func__, so this is safe.
68    */
69   if (STRPREFIX (func, "impl_"))
70     func += 5;
71   else if (STRPREFIX (func, "wrap_"))
72     func += 5;
73   w->error_func = func;
74
75   /* XXX This will be conditional on the user setting an error handler. */
76   fprintf (stderr, "wrappi: %s: %s\n", func, buf);
77 }
78
79 void
80 wrap_int_set_error (struct wrap_internal_h *w,
81                     const char *func,
82                     const char *fs, ...)
83 {
84   va_list args;
85   char *msg;
86   int err;
87
88   va_start (args, fs);
89   err = vasprintf (&msg, fs, args);
90   va_end (args);
91
92   if (err < 0) return;
93
94   set_error_buf (w, msg, 0, func);
95 }
96
97 void
98 wrap_int_set_error_errno (struct wrap_internal_h *w,
99                           int errnum,
100                           const char *func,
101                           const char *fs, ...)
102 {
103   va_list args;
104   char *msg;
105   int err;
106
107   va_start (args, fs);
108   err = vasprintf (&msg, fs, args);
109   va_end (args);
110
111   if (err < 0) return;
112
113   set_error_buf (w, msg, errnum, func);
114 }
115
116 /* Note the gperf perfect hash in lib/internal-procs-lookup.gperf */
117 int
118 wrap_int_lookup_proc_entry (const char *name)
119 {
120   const struct proc_entry *v =
121     wrap_int_gperf_lookup_proc_entry (name, strlen (name));
122   if (v)
123     return v->proc_nr;
124   else
125     return -1;
126 }