Path: blob/main/crypto/krb5/src/util/et/com_err.c
104187 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright 1997 by Massachusetts Institute of Technology3*4* Copyright 1987, 1988 by MIT Student Information Processing Board5*6* Permission to use, copy, modify, and distribute this software7* and its documentation for any purpose and without fee is8* hereby granted, provided that the above copyright notice9* appear in all copies and that both that copyright notice and10* this permission notice appear in supporting documentation,11* and that the names of M.I.T. and the M.I.T. S.I.P.B. not be12* used in advertising or publicity pertaining to distribution13* of the software without specific, written prior permission.14* Furthermore if you modify this software you must label15* your software as modified software and not distribute it in such a16* fashion that it might be confused with the original M.I.T. software.17* M.I.T. and the M.I.T. S.I.P.B. make no representations about18* the suitability of this software for any purpose. It is19* provided "as is" without express or implied warranty.20*/2122#include <stdio.h>23#include <string.h>24#include <stdlib.h>2526#include "com_err.h"27#include "error_table.h"2829#if defined(_WIN32)30#include <io.h>31#endif3233static /*@null@*/ et_old_error_hook_func com_err_hook = 0;34k5_mutex_t com_err_hook_lock = K5_MUTEX_PARTIAL_INITIALIZER;3536#if defined(_WIN32)37BOOL isGuiApp(void) {38DWORD mypid;39HANDLE myprocess;40mypid = GetCurrentProcessId();41myprocess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, mypid);42return GetGuiResources(myprocess, 1) > 0;43}44#endif4546static void default_com_err_proc (const char *whoami, errcode_t code,47const char *fmt, va_list ap)48{49#if defined(_WIN32)5051char errbuf[1024] = "";5253if (whoami) {54errbuf[sizeof(errbuf) - 1] = '\0';55strncat (errbuf, whoami, sizeof(errbuf) - 1 - strlen(errbuf));56strncat (errbuf, ": ", sizeof(errbuf) - 1 - strlen(errbuf));57}58if (code) {59errbuf[sizeof(errbuf) - 1] = '\0';60strncat (errbuf, error_message(code), sizeof(errbuf) - 1 - strlen(errbuf));61strncat (errbuf, " ", sizeof(errbuf) - 1 - strlen(errbuf));62}63if (fmt)64/* ITS4: ignore vsprintf */65vsprintf (errbuf + strlen (errbuf), fmt, ap);66errbuf[sizeof(errbuf) - 1] = '\0';6768if (_isatty(_fileno(stderr)) || !isGuiApp()) {69fputs(errbuf, stderr);70fputc('\r', stderr);71fputc('\n', stderr);72fflush(stderr);73} else74MessageBox ((HWND)NULL, errbuf, "Kerberos", MB_ICONEXCLAMATION);7576#else /* !_WIN32 */7778if (whoami) {79fputs(whoami, stderr);80fputs(": ", stderr);81}82if (code) {83fputs(error_message(code), stderr);84fputs(" ", stderr);85}86if (fmt) {87vfprintf(stderr, fmt, ap);88}89/* should do this only on a tty in raw mode */90putc('\r', stderr);91putc('\n', stderr);92fflush(stderr);9394#endif95}9697void KRB5_CALLCONV com_err_va(const char *whoami,98errcode_t code,99const char *fmt,100va_list ap)101{102int err;103et_old_error_hook_func p;104105err = com_err_finish_init();106if (err)107goto best_try;108k5_mutex_lock(&com_err_hook_lock);109p = com_err_hook ? com_err_hook : default_com_err_proc;110(*p)(whoami, code, fmt, ap);111k5_mutex_unlock(&com_err_hook_lock);112return;113114best_try:115/* Yikes. Our library initialization failed or we couldn't lock116the lock we want. We could be in trouble. Gosh, we should117probably print an error message. Oh, wait. That's what we're118trying to do. In fact, if we're losing on initialization here,119there's a good chance it has to do with failed initialization120of the caller. */121if (!com_err_hook)122default_com_err_proc(whoami, code, fmt, ap);123else124(com_err_hook)(whoami, code, fmt, ap);125assert(err == 0);126abort();127}128129130void KRB5_CALLCONV_C com_err(const char *whoami,131errcode_t code,132const char *fmt, ...)133{134va_list ap;135136va_start(ap, fmt);137com_err_va(whoami, code, fmt, ap);138va_end(ap);139}140141/* Make a separate function because the assert invocations below142use the macro expansion on some platforms, which may be insanely143long and incomprehensible. */144static void com_err_lock_hook_handle(void)145{146k5_mutex_lock(&com_err_hook_lock);147}148149et_old_error_hook_func set_com_err_hook (et_old_error_hook_func new_proc)150{151et_old_error_hook_func x;152153/* Broken initialization? What can we do? */154if (com_err_finish_init() != 0)155abort();156com_err_lock_hook_handle();157x = com_err_hook;158com_err_hook = new_proc;159k5_mutex_unlock(&com_err_hook_lock);160return x;161}162163et_old_error_hook_func reset_com_err_hook (void)164{165et_old_error_hook_func x;166167/* Broken initialization? What can we do? */168if (com_err_finish_init() != 0)169abort();170com_err_lock_hook_handle();171x = com_err_hook;172com_err_hook = NULL;173k5_mutex_unlock(&com_err_hook_lock);174return x;175}176177178