Add API: set-wrappid-path.
[wrappi.git] / lib / internal.h
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 #ifndef WRAPPI_INTERNAL_H_
20 #define WRAPPI_INTERNAL_H_
21
22 #include <string.h>
23 #include <errno.h>
24 #include <rpc/types.h>
25 #include <rpc/xdr.h>
26
27 #define STREQ(a,b) (strcmp((a),(b)) == 0)
28 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
29 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
30 #define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
31 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
32 #define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
33 #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
34 #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
35 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
36
37 struct wrap_internal_h {
38   char *error;          /* Last error on this handle, NULL is none. */
39   int errnum;           /* errno, or 0 if there was no errno set. */
40   const char *error_func; /* Function where the error occurred, if known. */
41 };
42
43 struct wrap_h {
44   /* The "internal" part of the handle must always appear at the
45    * beginning of the handle structure.  For the remote case, this
46    * contains a cut-down handle which is what non-local entry points
47    * are permitted to touch.  For the local case, the full handle is
48    * cast to (struct wrap_internal_h *).
49    */
50   struct wrap_internal_h internal;
51
52   /* Fields that follow the 'internal' structure only exist
53    * on the local side of a connection.
54    */
55
56   uint32_t serial;              /* Serial number used in protocol. */
57
58   FILE *rfp, *wfp;              /* Connection to remote. */
59   pid_t pid;                    /* Child process if using remote, eg ssh */
60
61   /* Connection URL.  If scheme == LOCAL, it means we're using the
62    * local code.
63    */
64   wrap_scheme_enum scheme;
65   char *hostname;
66   char *wrappid_path;
67 };
68
69 /* Declare an error, setting the error field in the handle. */
70 #define set_error(fs...) \
71   wrap_int_set_error ((struct wrap_internal_h *)w, __func__, fs)
72 #define set_error_errno(fs...) \
73   wrap_int_set_error_errno ((struct wrap_internal_h *)w, (errno), __func__, fs)
74
75 extern void wrap_int_connect_ssh (wrap_h *w);
76 extern int wrap_int_lookup_proc_entry (const char *name);
77 extern void wrap_int_make_request (wrap_h *w, int proc_nr, const void *args, void *ret);
78 extern void wrap_int_make_request_xdr (wrap_h *w, int proc_nr, const void *args, void *ret);
79 extern void wrap_int_set_error (struct wrap_internal_h *w, const char *func, const char *fs, ...);
80 extern void wrap_int_set_error_errno (struct wrap_internal_h *w, int errnum, const char *func, const char *fs, ...);
81
82 /* See lib/internal-procs.c for the static contents of this table.
83  * The table is indexed by wrap_int_<name>_num.  These are defined in
84  * lib/internal-procs.h.
85  */
86 struct proc_table {
87   const char *name;             /* The name of this entry point. */
88
89   /* Size of the args and ret structs. */
90   size_t args_struct_size;
91   size_t ret_struct_size;
92
93   /* Call this procedure. */
94   void (*call) (wrap_h *w, const void *args, void *ret);
95
96   /* XDR encode/decode functions for serializing the args and ret. */
97   xdrproc_t args_xdrproc;
98   xdrproc_t ret_xdrproc;
99 };
100
101 extern const struct proc_table wrap_int_proc_table[];
102
103 #include "internal-procs.h"
104
105 /* Defined and used by lib/internal-procs-lookup.gperf */
106 struct proc_entry { char *name; int proc_nr; };
107 extern const struct proc_entry *wrap_int_gperf_lookup_proc_entry (register const char *str, register unsigned int len);
108
109 #endif /* WRAPPI_INTERNAL_H_ */