Make setthetime() static per the prototype.
[dragonfly.git] / lib / libcaps / sysport.c
1 /*
2  * Copyright (c) 2003 Galen Sampson <galen_sampson@yahoo.com>
3  * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $DragonFly: src/lib/libcaps/sysport.c,v 1.3 2003/12/07 04:21:52 dillon Exp $
28  */
29 #include "defs.h"
30
31 #include "sendsys.h"
32 #include <sys/syscall.h>
33 #include <sys/sysproto.h>
34 #include <sys/sysunion.h>
35
36 /* XXX Temporary */
37 #include <unistd.h>
38
39 #ifdef DEBUG
40 #include <stdio.h>
41 #endif
42
43 static int sysport_putport(lwkt_port_t port, lwkt_msg_t msg);
44 static void sysport_loop(void *dummy);
45
46 struct thread sys_td;
47 lwkt_port_t sysport;
48
49 void
50 sysport_init(void)
51 {
52     lwkt_init_thread(&sys_td, libcaps_alloc_stack(THREAD_STACK),
53                     TDF_SYSTHREAD, mycpu);
54     sysport = &sys_td.td_msgport;
55     sysport->mp_putport = sysport_putport;
56     sysport->mp_flags = MSGPORTF_WAITING;       /* XXX temporary */
57     cpu_set_thread_handler(&sys_td, lwkt_exit, sysport_loop, NULL);
58 }
59
60 /************************************************************************
61  *                             PORT FUNCTIONS                           *
62  ************************************************************************/
63
64 /*
65  * XXX We might need to separte this function in the way *lwkt_putport*
66  * is separated in the case of multiple cpus. Secifically we will need
67  * a lwkt_sysputport_remote().
68  */
69 static
70 int
71 sysport_putport(lwkt_port_t port, lwkt_msg_t msg)
72 {
73     int error = 0;
74     thread_t td = port->mp_td;
75
76     /**
77      * XXX sendsys() will only allow asynchronous messages from uid 0. This
78      * is a kernel issue. See note below.
79      */
80     if(msg->ms_flags & MSGF_ASYNC)
81     {
82         /**
83          * The message is not done.
84          */
85         msg->ms_flags &= ~MSGF_DONE;
86         error = sendsys(NULL, msg, msg->ms_msgsize);
87         if(error == EASYNC)
88         {
89             TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
90             msg->ms_flags |= MSGF_QUEUED;
91             /**
92              * Shouldn't need this check, we are always waiting
93              */
94             if(port->mp_flags & MSGPORTF_WAITING)
95             {
96                 lwkt_schedule(td);
97             }
98         }
99 #ifdef DEBUG
100         printf("async return error %d\n", error);
101 #endif
102     }
103
104     /**
105      * XXX this is a temporary hack until the kernel changes to implement
106      * the desired asynchronous goals.
107      *
108      * The current asynchronous messaging systemcall interface that sendsys
109      * uses has some potential security issues and is limited to use by the
110      * superuser only.  Synchronous messages are allowed by anyone.  Sendsys
111      * returns EPERM in the case where you are not the superuser but tried to
112      * send an asynchonous message.
113      *
114      * If you are not the super user then the system call will be made again,
115      * but without MSGF_ASYNC set.
116      */
117     if(error != EASYNC && error == EPERM)
118     {
119         msg->ms_flags &= ~MSGF_ASYNC;
120 #ifdef DEBUG
121         printf("Warning, only super user can send asynchonous system messages\n");
122 #endif
123     }
124
125     /**
126      * The message is synchronous.  Send it sychronously.
127      */
128     if((msg->ms_flags & MSGF_ASYNC) == 0)
129     {
130         error = sendsys(NULL, msg, msg->ms_msgsize);
131         msg->ms_flags |= MSGF_DONE;
132     }
133
134     return(error);
135 }
136
137 void *
138 lwkt_syswaitport(lwkt_msg_t msg)
139 {
140     lwkt_msg_t rmsg;
141
142     /**
143      * Block awaiting a return from the kernel.
144      */
145     for(rmsg = (lwkt_msg_t)sendsys(NULL, NULL, -1); rmsg != msg; )
146     {
147         usleep(1000000 / 10);
148         rmsg = (lwkt_msg_t)sendsys(NULL, NULL, -1);
149 #ifdef DEBUG
150         printf("    rmsg %p\n", rmsg);
151 #endif
152     }
153
154     return msg;
155 }
156
157 /************************************************************************
158  *                            THREAD FUNCTIONS                          *
159  ************************************************************************/
160 /* 
161  * XXX Temporary function that provides a mechanism to return an asynchronous
162  * message completed by the kernel to be returned to the port it originated
163  * from.
164  */
165 static 
166 void
167 sysport_loop(void *dummy)
168 {
169     lwkt_msg_t msg;
170
171     for(;;)
172     {
173         msg = lwkt_waitport(&curthread->td_msgport, NULL);
174
175         msg = lwkt_syswaitport(msg);
176
177         /**
178          * The message was asynchronous
179          */
180         if(msg->ms_flags & MSGF_ASYNC)
181         {
182             lwkt_replymsg(msg, msg->ms_error);
183         }
184         else
185         {
186             msg->ms_flags |= MSGF_DONE;
187         }
188     }
189 }