1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 1990-1997 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/socket.h>
31*0Sstevel@tonic-gate #include <sys/errno.h>
32*0Sstevel@tonic-gate #include <sys/stat.h>
33*0Sstevel@tonic-gate #include <fcntl.h>
34*0Sstevel@tonic-gate #include <sys/syslog.h>
35*0Sstevel@tonic-gate #include <sys/strlog.h>
36*0Sstevel@tonic-gate #include <sys/stropts.h>
37*0Sstevel@tonic-gate #include <stdio.h>
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate extern int errno;
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate #define N_AGAIN 11
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate int
send(s,msg,len,flags)44*0Sstevel@tonic-gate send(s, msg, len, flags)
45*0Sstevel@tonic-gate int s;
46*0Sstevel@tonic-gate char *msg;
47*0Sstevel@tonic-gate int len, flags;
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate int a;
50*0Sstevel@tonic-gate if ((a = _send(s, msg, len, flags)) == -1) {
51*0Sstevel@tonic-gate if (errno == N_AGAIN)
52*0Sstevel@tonic-gate errno = EWOULDBLOCK;
53*0Sstevel@tonic-gate else
54*0Sstevel@tonic-gate maperror();
55*0Sstevel@tonic-gate }
56*0Sstevel@tonic-gate return (a);
57*0Sstevel@tonic-gate }
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate /* Added to convert socket "/dev/log" to stream "/dev/conslog" */
61*0Sstevel@tonic-gate #define logname "/dev/conslog"
62*0Sstevel@tonic-gate #define MAXLINE 1024
63*0Sstevel@tonic-gate #define SVR4_ENOTSOCK 95 /* Socket operation on non-socket */
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate int
sendto(s,msg,len,flags,to,tolen)67*0Sstevel@tonic-gate sendto(s, msg, len, flags, to, tolen)
68*0Sstevel@tonic-gate int s;
69*0Sstevel@tonic-gate char *msg;
70*0Sstevel@tonic-gate int len, flags;
71*0Sstevel@tonic-gate struct sockaddr *to;
72*0Sstevel@tonic-gate int tolen;
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate int a;
75*0Sstevel@tonic-gate static int LogDev = -1;
76*0Sstevel@tonic-gate /* check for logfile */
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate if ((a = _sendto(s, msg, len, flags, to, tolen)) == -1) {
79*0Sstevel@tonic-gate if (errno == SVR4_ENOTSOCK &&
80*0Sstevel@tonic-gate strcmp(to->sa_data, "/dev/log") == 0) {
81*0Sstevel@tonic-gate char *msg_p;
82*0Sstevel@tonic-gate struct log_ctl hdr;
83*0Sstevel@tonic-gate struct strbuf dat;
84*0Sstevel@tonic-gate struct strbuf ctl;
85*0Sstevel@tonic-gate struct stat sbuf;
86*0Sstevel@tonic-gate if (LogDev == -1) {
87*0Sstevel@tonic-gate int tfd;
88*0Sstevel@tonic-gate /* close socket /dev/log */
89*0Sstevel@tonic-gate close(s);
90*0Sstevel@tonic-gate /* open stream /dev/conslog */
91*0Sstevel@tonic-gate tfd = open(logname, O_WRONLY);
92*0Sstevel@tonic-gate if (tfd == -1)
93*0Sstevel@tonic-gate return (-1);
94*0Sstevel@tonic-gate /* insure stream has same fd as closed socket */
95*0Sstevel@tonic-gate if (tfd != s) {
96*0Sstevel@tonic-gate if (dup2(tfd, s) < 0) {
97*0Sstevel@tonic-gate close(tfd);
98*0Sstevel@tonic-gate return (-1);
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate close(tfd);
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate if (fcntl(s, F_SETFD, FD_CLOEXEC) == -1)
103*0Sstevel@tonic-gate return (-1);
104*0Sstevel@tonic-gate if (fstat(s, &sbuf) != -1)
105*0Sstevel@tonic-gate LogDev = sbuf.st_rdev;
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate } else if (fstat(s, &sbuf) == -1 ||
108*0Sstevel@tonic-gate LogDev != sbuf.st_rdev)
109*0Sstevel@tonic-gate return (-1);
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gate /* build the header */
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate /* parse <pri> from msg */
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate hdr.mid = 1; /* 0 for kernal */
116*0Sstevel@tonic-gate /* sid, ltime, ttime, seq_no not used */
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate hdr.pri = strtol(msg + 1, &msg_p, 10);
119*0Sstevel@tonic-gate if (msg + 1 == msg_p) {
120*0Sstevel@tonic-gate hdr.pri = (LOG_USER|LOG_INFO);
121*0Sstevel@tonic-gate } else {
122*0Sstevel@tonic-gate len -= msg_p - msg;
123*0Sstevel@tonic-gate msg = msg_p + 1;
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate hdr.flags = SL_CONSOLE;
126*0Sstevel@tonic-gate hdr.level = 0;
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate ctl.maxlen = sizeof (struct log_ctl);
129*0Sstevel@tonic-gate ctl.len = sizeof (struct log_ctl);
130*0Sstevel@tonic-gate ctl.buf = (caddr_t)&hdr;
131*0Sstevel@tonic-gate dat.maxlen = MAXLINE;
132*0Sstevel@tonic-gate dat.len = len;
133*0Sstevel@tonic-gate if (dat.len > MAXLINE) {
134*0Sstevel@tonic-gate dat.len = MAXLINE;
135*0Sstevel@tonic-gate msg[MAXLINE - 1] = '\0';
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate dat.buf = msg;
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate /* output the message to the local logger */
140*0Sstevel@tonic-gate if (_putmsg(s, &ctl, &dat, 0) == 0)
141*0Sstevel@tonic-gate return (0);
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate if (errno == N_AGAIN)
144*0Sstevel@tonic-gate errno = EWOULDBLOCK;
145*0Sstevel@tonic-gate else
146*0Sstevel@tonic-gate maperror();
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate return (a);
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate int
sendmsg(s,msg,flags)153*0Sstevel@tonic-gate sendmsg(s, msg, flags)
154*0Sstevel@tonic-gate int s;
155*0Sstevel@tonic-gate struct msghdr *msg;
156*0Sstevel@tonic-gate int flags;
157*0Sstevel@tonic-gate {
158*0Sstevel@tonic-gate int a;
159*0Sstevel@tonic-gate if ((a = _sendmsg(s, msg, flags)) == -1) {
160*0Sstevel@tonic-gate if (errno == N_AGAIN)
161*0Sstevel@tonic-gate errno = EWOULDBLOCK;
162*0Sstevel@tonic-gate else
163*0Sstevel@tonic-gate maperror();
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate return (a);
166*0Sstevel@tonic-gate }
167