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