aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/xntpd/kernel/tty_clk_STREAMS.c
blob: a69a7573be4535ce0301dc69329dd7642ba33c2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* tty_clk_STREAMS.c,v 3.1 1993/07/06 01:07:34 jbj Exp
 * Timestamp STREAMS module for SunOS 4.1
 *
 * Copyright 1991, Nick Sayer
 *
 * Special thanks to Greg Onufer for his debug assists.
 *
 * Should be PUSHed directly on top of a serial I/O channel.
 * For any character in a user-designated set, adds a kernel
 * timestamp to that character.
 *
 * BUGS:
 *
 * Only so many characters can be timestamped. This number, however,
 * is adjustable.
 *
 * The null character ($00) cannot be timestamped.
 *
 * The M_DATA messages passed upstream will not be the same
 * size as when they arrive from downstream, even if no
 * timestamp character is in the message. This, however,
 * should not affect anything.
 *
 */

#include "clk.h"
#if NCLK > 0
/*
 * How big should the messages we pass upstream be?
 */
#define MESSAGE_SIZE 128

#include <string.h>
#include <sys/types.h>
#include <sys/stream.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/user.h>
#include <sys/errno.h>

#include <sys/clkdefs.h>

static struct module_info rminfo = { 0, "clk", 0, INFPSZ, 0, 0 };
static struct module_info wminfo = { 0, "clk", 0, INFPSZ, 0, 0 };
static int clkopen(), clkrput(), clkwput(), clkclose();

static struct qinit rinit = { clkrput, NULL, clkopen, clkclose, NULL,
	&rminfo, NULL };

static struct qinit winit = { clkwput, NULL, NULL, NULL, NULL,
	&wminfo, NULL };

struct streamtab clkinfo = { &rinit, &winit, NULL, NULL };

struct priv_data_type
{
  char in_use;
  char string[CLK_MAXSTRSIZE];
} priv_data[NCLK];

char first_open=1;

/*
 * God only knows why, but linking with strchr() fails
 * on my system, so here's a renamed copy.
 */

u_char *str_chr(s,c)
u_char *s;
int c;
{
  while (*s)
    if(*s++ == c)
      return (s-1);
  return NULL;
}

/*ARGSUSED*/
static int clkopen(q, dev, flag, sflag)
queue_t *q;
dev_t dev;
int flag;
int sflag;
{
  int i;

/* Damn it! We can't even have the global data struct properly
   initialized! So we have a mark to tell us to init the global
   data on the first open */

  if (first_open)
  {
    first_open=0;

    for(i=0;i<NCLK;i++)
      priv_data[i].in_use=0;
  }

  for(i=0;i<NCLK;i++)
    if(!priv_data[i].in_use)
    {
      priv_data[i].in_use++;
      ((struct priv_data_type *) (q->q_ptr))=priv_data+i;
      priv_data[i].string[0]=0;
      return (0);
    }
  u.u_error = EBUSY;
  return (OPENFAIL);
}

/*ARGSUSED*/
static int clkclose(q, flag)
queue_t *q;
int flag;
{
  ((struct priv_data_type *) (q->q_ptr))->in_use=0;

  return (0);
}

/*
 * Now the crux of the biscuit.
 *
 * If it's an M_DATA package, we take each character and pass
 * it to clkchar.
 */

void clkchar();

static int clkrput(q, mp)
queue_t *q;
mblk_t *mp;
{
  mblk_t *bp;

  switch(mp->b_datap->db_type)
  {
    case M_DATA:
      clkchar(0,q,2);
      for(bp=mp; bp!=NULL; bp=bp->b_cont)
      {
	while(bp->b_rptr < bp->b_wptr)
	  clkchar( ((u_char)*(bp->b_rptr++)) , q , 0 );
      }
      clkchar(0,q,1);
      freemsg(mp);
    break;
    default:
      putnext(q,mp);
    break;
  }

}

/*
 * If it's a matching M_IOCTL, handle it.
 */

static int clkwput(q, mp)
queue_t *q;
mblk_t *mp;
{
  struct iocblk *iocp;

  switch(mp->b_datap->db_type)
  {
    case M_IOCTL:
      iocp=(struct iocblk*) mp->b_rptr;
      if (iocp->ioc_cmd==CLK_SETSTR)
      {
        strncpy( ((struct priv_data_type *) (RD(q)->q_ptr))->string,
	  (char *) mp->b_cont->b_rptr,CLK_MAXSTRSIZE);
        /* make sure it's null terminated */
	((struct priv_data_type *) (RD(q)->q_ptr))->string[CLK_MAXSTRSIZE-1]=0;
	mp->b_datap->db_type = M_IOCACK;
	qreply(q,mp);
      }
      else
	putnext(q,mp);
    break;
    default:
      putnext(q,mp);
    break;
  }
}

/*
 * Now clkchar. It takes a character, a queue pointer and an action
 * flag and depending on the flag either:
 *
 * 0 - adds the character to the current message. If there's a
 * timestamp to be done, do that too. If the message is less than
 * 8 chars from being full, link in a new one, and set it up for
 * the next call.
 *
 * 1 - sends the whole mess to Valhala.
 *
 * 2 - set things up.
 *
 * Yeah, it's an ugly hack. Complaints may be filed with /dev/null.
 */


void clkchar(c,q,f)
	register u_char c;
	queue_t *q;
	char f;
{
  static char error;
  static mblk_t *message,*mp;
  struct timeval tv;

/* Get a timestamp ASAP! */
  uniqtime(&tv);

  switch(f)
  {
    case 1:
      if (!error)
        putnext(q,message);
    break;
    case 2:
      mp=message= (mblk_t*) allocb(MESSAGE_SIZE,BPRI_LO);
      error=(message==NULL);
      if (error)
	log(LOG_ERR,"clk: cannot allocate message - data lost");
    break;
    case 0:
      if (error) /* If we had an error, forget it. */
	return;

      *mp->b_wptr++=c; /* Put the char away first.

      /* If it's in the special string, append a struct timeval */

      if (str_chr( ((struct priv_data_type *) (q->q_ptr))->string ,
        c )!=NULL)
      {
	  int i;

	  for (i=0;i<sizeof(struct timeval);i++)
	    *mp->b_wptr++= *( ((char*)&tv) + i );
      }

      /* If we don't have space for a complete struct timeval, and a
         char, it's time for a new mp block */

      if (((mp->b_wptr-mp->b_rptr)+sizeof(struct timeval)+2)>MESSAGE_SIZE)
      {
	  mp->b_cont= (mblk_t*) allocb(MESSAGE_SIZE,BPRI_LO);
	  error=(mp->b_cont==NULL);
	  if (error)
	  {
	    log(LOG_ERR,"clk: cannot allocate message - data lost");
	    freemsg(message);
          }
          mp=mp->b_cont;
      }

    break;
  }
}

#endif