1/* 2 * Copyright (c) 1999-2017 The strace developers. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#ifdef HAVE_CONFIG_H 29# include "config.h" 30#endif 31 32#include <errno.h> 33#include <stdarg.h> 34#include <string.h> 35#include <stdio.h> 36#include <stdlib.h> 37 38#include "error_prints.h" 39 40#ifndef HAVE_PROGRAM_INVOCATION_NAME 41extern char *program_invocation_name; 42#endif 43 44static void 45verror_msg(int err_no, const char *fmt, va_list p) 46{ 47 char *msg; 48 49 fflush(NULL); 50 51 /* We want to print entire message with single fprintf to ensure 52 * message integrity if stderr is shared with other programs. 53 * Thus we use vasprintf + single fprintf. 54 */ 55 msg = NULL; 56 if (vasprintf(&msg, fmt, p) >= 0) { 57 if (err_no) 58 fprintf(stderr, "%s: %s: %s\n", 59 program_invocation_name, msg, strerror(err_no)); 60 else 61 fprintf(stderr, "%s: %s\n", 62 program_invocation_name, msg); 63 free(msg); 64 } else { 65 /* malloc in vasprintf failed, try it without malloc */ 66 fprintf(stderr, "%s: ", program_invocation_name); 67 vfprintf(stderr, fmt, p); 68 if (err_no) 69 fprintf(stderr, ": %s\n", strerror(err_no)); 70 else 71 putc('\n', stderr); 72 } 73 /* We don't switch stderr to buffered, thus fprintf(stderr) 74 * always flushes its output and this is not necessary: */ 75 /* fflush(stderr); */ 76} 77 78void 79error_msg(const char *fmt, ...) 80{ 81 va_list p; 82 va_start(p, fmt); 83 verror_msg(0, fmt, p); 84 va_end(p); 85} 86 87void 88error_msg_and_die(const char *fmt, ...) 89{ 90 va_list p; 91 va_start(p, fmt); 92 verror_msg(0, fmt, p); 93 die(); 94} 95 96void 97error_msg_and_help(const char *fmt, ...) 98{ 99 if (fmt != NULL) { 100 va_list p; 101 va_start(p, fmt); 102 verror_msg(0, fmt, p); 103 } 104 fprintf(stderr, "Try '%s -h' for more information.\n", 105 program_invocation_name); 106 die(); 107} 108 109void 110perror_msg(const char *fmt, ...) 111{ 112 va_list p; 113 va_start(p, fmt); 114 verror_msg(errno, fmt, p); 115 va_end(p); 116} 117 118void 119perror_msg_and_die(const char *fmt, ...) 120{ 121 va_list p; 122 va_start(p, fmt); 123 verror_msg(errno, fmt, p); 124 die(); 125} 126