2 This software is available to you under a choice of one of two
3 licenses. You may choose to be licensed under the terms of the GNU
4 General Public License (GPL) Version 2, available at
5 <http://www.fsf.org/copyleft/gpl.html>, or the OpenIB.org BSD
6 license, available in the LICENSE.TXT file accompanying this
7 software. These details are also available at
8 <http://openib.org/license.html>.
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 Copyright (c) 2004 Topspin Communications. All rights reserved.
20 Copyright (c) 2006 Mellanox Technologies Ltd. All rights reserved.
30 #endif /* HAVE_CONFIG_H */
32 #include <sys/types.h>
44 * SDP specific includes
48 extern char *program_invocation_short_name;
56 /* --------------------------------------------------------------------- */
57 /* library static and global variables */
58 /* --------------------------------------------------------------------- */
59 int __sdp_min_level = 9;
60 static __sdp_log_type_t __sdp_log_type = SDP_LOG_FILE;
61 static FILE *__sdp_log_file = NULL;
70 char extra_format[512];
74 if ( level < __sdp_min_level ) {
78 va_start( ap, format );
79 switch ( __sdp_log_type ) {
81 sprintf( extra_format, "%s[%d] libsdp %s ",
82 program_invocation_short_name, getpid( ), format );
83 vsyslog( LOG_USER | LOG_NOTICE, extra_format, ap );
88 ctime_r(&timeval, timestr, sizeof timestr);
90 ctime_r(&timeval, timestr);
92 timestr[strlen(timestr)-1] = '\0';
93 sprintf( extra_format, "%s %s[%d] libsdp %s ",
94 timestr, program_invocation_short_name,
96 if ( __sdp_log_file == NULL ) {
97 vfprintf( stderr, extra_format, ap );
98 #if 0 /* might slow everything too much? */
99 ( void )fflush( stderr );
102 vfprintf( __sdp_log_file, extra_format, ap );
103 #if 0 /* might slow everything too much? */
104 ( void )fflush( __sdp_log_file );
116 return ( __sdp_min_level );
120 __sdp_log_set_min_level(
123 __sdp_min_level = level;
127 __sdp_log_set_log_type(
128 __sdp_log_type_t type )
130 if ( __sdp_log_file != NULL ) {
131 fclose( __sdp_log_file );
132 __sdp_log_file = NULL;
135 __sdp_log_type = type;
139 __sdp_log_set_log_stderr(
142 __sdp_log_set_log_type( SDP_LOG_FILE );
143 /* NULL means stderr */
149 __sdp_log_set_log_syslog(
152 __sdp_log_set_log_type( SDP_LOG_SYSLOG );
158 __sdp_log_set_log_file(
163 struct stat lstat_res;
166 char *p, tfilename[PATH_MAX + 1];
168 /* Strip off any paths from the filename */
169 p = strrchr( filename, '/' );
172 base on the active user ID we either use /var/log for root or
173 append the uid to the name
179 snprintf( tfilename, sizeof(tfilename), "/var/log/%s", filename );
181 char tdir[PATH_MAX + 1];
183 for regular user, allow log file to be placed in a user
184 requested path. If no path is requested the log file is
188 snprintf(tdir, sizeof(tdir), "%s.%d", filename, uid );
190 snprintf(tdir, sizeof(tdir ), "/tmp/%s.%d", filename, uid );
192 if (mkdir(tdir, 0700)) {
195 if (errno != EEXIST) {
196 __sdp_log( 9, "Couldn't create directory '%s' for logging (%m)\n", tdir );
200 if (lstat(tdir, &stat)) {
201 __sdp_log(9, "Couldn't lstat directory %s\n", tdir);
205 if (!S_ISDIR(stat.st_mode) || stat.st_uid != uid ||
206 (stat.st_mode & ~(S_IFMT | S_IRWXU))) {
207 __sdp_log( 9, "Cowardly refusing to log into directory:'%s'. "
208 "Make sure it is not: (1) link, (2) other uid, (3) bad permissions."
209 "thus is a security issue.\n", tdir );
214 snprintf(tfilename, sizeof(tfilename), "%s/log", tdir);
215 printf("dir: %s file: %s\n", tdir, tfilename);
218 /* double check the file is not a link */
219 status = lstat(tfilename, &lstat_res);
220 if ( (status == 0) && S_ISLNK(lstat_res.st_mode) ) {
221 __sdp_log( 9, "Cowardly refusing to log into:'%s'. "
222 "It is a link - thus is a security issue.\n", tfilename );
226 f = fopen( tfilename, "a" );
228 __sdp_log( 9, "Couldn't open '%s' for logging (%m)\n", tfilename );
232 __sdp_log_set_log_type( SDP_LOG_FILE );