1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 #include <sys/types.h> |
|
27 #include <sys/socket.h> |
|
28 #include <bsm/adt_event.h> |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 #include <unistd.h> |
|
32 #include <pthread.h> |
|
33 #include <ucred.h> |
|
34 |
|
35 #include <rad/adr_stream.h> |
|
36 #include "rad_object.h" |
|
37 #include "rad_modapi.h" |
|
38 #include "rad_modapi_xport.h" |
|
39 #include "rad_connection.h" |
|
40 #include "rad_xport.h" |
|
41 |
|
42 #include "api_pipe.h" |
|
43 |
|
44 static rad_moderr_t |
|
45 pipe_listen(rad_thread_t *arg) |
|
46 { |
|
47 int fdin; |
|
48 data_t *data = rad_thread_arg(arg); |
|
49 data_t *exitprop = struct_get(data, "exit"); |
|
50 rad_subject_t *subject; |
|
51 rad_moderr_t result = rm_ok; |
|
52 ucred_t *uc = ucred_get(P_MYID); |
|
53 |
|
54 adr_stream_t *stream; |
|
55 data_t *fdprop = struct_get(data, "fd"); |
|
56 if (fdprop) { |
|
57 fdin = data_to_integer(fdprop); |
|
58 stream = adr_stream_create_fd(fdin); |
|
59 } else { |
|
60 fdin = STDIN_FILENO; |
|
61 stream = adr_stream_create_fds(fdin, STDOUT_FILENO); |
|
62 } |
|
63 if (stream == NULL) { |
|
64 rad_log(RL_ERROR, "failed to allocate connection"); |
|
65 goto done; |
|
66 } |
|
67 |
|
68 if (uc == NULL || |
|
69 (subject = rad_subject_create_ucred(uc, B_FALSE, NULL)) == NULL) { |
|
70 rad_log(RL_ERROR, "failed to allocate connection"); |
|
71 adr_stream_close(stream); |
|
72 adr_stream_free(stream); |
|
73 result = rm_system; |
|
74 goto done; |
|
75 } |
|
76 |
|
77 rad_protocol_t *proto = rad_proto_find("rad"); |
|
78 if (proto == NULL) { |
|
79 rad_log(RL_ERROR, "unable to find protocol \"rad\""); |
|
80 rad_subject_unref(subject); |
|
81 adr_stream_close(stream); |
|
82 adr_stream_free(stream); |
|
83 result = rm_config; |
|
84 goto done; |
|
85 } |
|
86 |
|
87 radmod_connection_t *conn = rad_conn_create_fd(fdin, B_FALSE); |
|
88 if (conn == NULL) { |
|
89 rad_log(RL_WARN, "failed to allocate connection"); |
|
90 rad_subject_unref(subject); |
|
91 adr_stream_close(stream); |
|
92 adr_stream_free(stream); |
|
93 result = rm_system; |
|
94 goto done; |
|
95 } |
|
96 conn->rm_conn_xport = stream; |
|
97 conn->rm_conn_proto_ops = proto; |
|
98 |
|
99 if (!rad_conn_setsubject(conn, subject)) { |
|
100 rad_log(RL_WARN, "failed to set connection subject"); |
|
101 rad_conn_close(conn); |
|
102 rad_conn_free(conn); |
|
103 result = rm_system; |
|
104 goto done; |
|
105 } |
|
106 rad_thread_ack(arg, rm_ok); |
|
107 rad_proto_handle(conn); |
|
108 rad_conn_free(conn); |
|
109 |
|
110 done: |
|
111 if (exitprop != NULL) { |
|
112 assert(data_basetype(exitprop) == dt_boolean); |
|
113 if (data_to_boolean(exitprop)) { |
|
114 rad_log(result == rm_ok ? RL_DEBUG : RL_WARN, |
|
115 "exit triggered by pipe connector"); |
|
116 exit(0); |
|
117 } |
|
118 } |
|
119 return (result); |
|
120 } |
|
121 |
|
122 static boolean_t running = B_FALSE; |
|
123 |
|
124 static rad_moderr_t |
|
125 stdin_starter(data_t *data) |
|
126 { |
|
127 rad_moderr_t result; |
|
128 |
|
129 if (running) { |
|
130 /* Until we permit configuring the fd */ |
|
131 rad_log(RL_ERROR, |
|
132 "Only one stdin transport may be running at a time.\n"); |
|
133 return (rm_config); |
|
134 } |
|
135 |
|
136 result = rad_thread_create(pipe_listen, data); |
|
137 if (result == rm_ok) |
|
138 running = B_TRUE; |
|
139 return (result); |
|
140 } |
|
141 |
|
142 static rad_moderr_t |
|
143 pipe_starter(data_t *data) |
|
144 { |
|
145 return (rad_thread_create(pipe_listen, data)); |
|
146 } |
|
147 |
|
148 static rad_modinfo_t modinfo = { "xport_pipe", "stdin/pipe transport" }; |
|
149 |
|
150 int |
|
151 _rad_init(void *handle) |
|
152 { |
|
153 if (rad_module_register(handle, RAD_MODVERSION, &modinfo) == -1) |
|
154 return (-1); |
|
155 |
|
156 rad_xport_register("stdin", &t__stdin, stdin_starter); |
|
157 rad_xport_register("pipe", &t__pipe, pipe_starter); |
|
158 return (0); |
|
159 } |
|