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