8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libbc / libc / sys / common / send.c
blob371935f87afb3fd79759d2c11bf30ec776f82f85
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 1990-1997 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #ident "%Z%%M% %I% %E% SMI"
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>
39 extern int errno;
41 #define N_AGAIN 11
43 int
44 send(s, msg, len, flags)
45 int s;
46 char *msg;
47 int len, flags;
49 int a;
50 if ((a = _send(s, msg, len, flags)) == -1) {
51 if (errno == N_AGAIN)
52 errno = EWOULDBLOCK;
53 else
54 maperror();
56 return (a);
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 */
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;
74 int a;
75 static int LogDev = -1;
76 /* check for logfile */
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);
100 close(tfd);
102 if (fcntl(s, F_SETFD, FD_CLOEXEC) == -1)
103 return (-1);
104 if (fstat(s, &sbuf) != -1)
105 LogDev = sbuf.st_rdev;
107 } else if (fstat(s, &sbuf) == -1 ||
108 LogDev != sbuf.st_rdev)
109 return (-1);
111 /* build the header */
113 /* parse <pri> from msg */
115 hdr.mid = 1; /* 0 for kernal */
116 /* sid, ltime, ttime, seq_no not used */
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;
125 hdr.flags = SL_CONSOLE;
126 hdr.level = 0;
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';
137 dat.buf = msg;
139 /* output the message to the local logger */
140 if (_putmsg(s, &ctl, &dat, 0) == 0)
141 return (0);
143 if (errno == N_AGAIN)
144 errno = EWOULDBLOCK;
145 else
146 maperror();
148 return (a);
153 sendmsg(s, msg, flags)
154 int s;
155 struct msghdr *msg;
156 int flags;
158 int a;
159 if ((a = _sendmsg(s, msg, flags)) == -1) {
160 if (errno == N_AGAIN)
161 errno = EWOULDBLOCK;
162 else
163 maperror();
165 return (a);