2007-12-02 19:11:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Vreixo Formoso
|
|
|
|
*
|
|
|
|
* This file is part of the libisofs project; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation. See COPYING file for details.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-12-17 20:12:51 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2007-12-02 19:11:44 +00:00
|
|
|
|
|
|
|
#include "libisofs.h"
|
|
|
|
#include "messages.h"
|
2008-01-19 01:48:12 +00:00
|
|
|
#include "error.h"
|
|
|
|
|
|
|
|
int iso_message_id = LIBISO_MSGS_ORIGIN_IMAGE_BASE;
|
2007-12-02 19:11:44 +00:00
|
|
|
|
2008-01-22 20:12:27 +00:00
|
|
|
/**
|
|
|
|
* Threshold for aborting.
|
|
|
|
*/
|
2008-01-22 20:36:24 +00:00
|
|
|
int abort_threshold = LIBISO_MSGS_SEV_HINT;
|
2008-01-22 20:12:27 +00:00
|
|
|
|
2007-12-17 20:12:51 +00:00
|
|
|
#define MAX_MSG_LEN 4096
|
|
|
|
|
2008-01-19 01:48:12 +00:00
|
|
|
struct libiso_msgs *libiso_msgr = NULL;
|
|
|
|
|
|
|
|
int iso_init()
|
|
|
|
{
|
|
|
|
if (libiso_msgr == NULL) {
|
|
|
|
if (libiso_msgs_new(&libiso_msgr, 0) <= 0)
|
2008-01-20 21:28:27 +00:00
|
|
|
return ISO_FATAL_ERROR;
|
2008-01-19 01:48:12 +00:00
|
|
|
}
|
|
|
|
libiso_msgs_set_severities(libiso_msgr, LIBISO_MSGS_SEV_NEVER,
|
|
|
|
LIBISO_MSGS_SEV_FATAL, "libisofs: ", 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void iso_finish()
|
|
|
|
{
|
|
|
|
libiso_msgs_destroy(&libiso_msgr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void iso_msg_debug(int imgid, const char *fmt, ...)
|
2007-12-02 19:11:44 +00:00
|
|
|
{
|
2007-12-17 20:12:51 +00:00
|
|
|
char msg[MAX_MSG_LEN];
|
|
|
|
va_list ap;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-17 20:12:51 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(msg, MAX_MSG_LEN, fmt, ap);
|
|
|
|
va_end(ap);
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2008-01-19 01:48:12 +00:00
|
|
|
libiso_msgs_submit(libiso_msgr, imgid, 0x00000002, LIBISO_MSGS_SEV_DEBUG,
|
2007-12-17 20:12:51 +00:00
|
|
|
LIBISO_MSGS_PRIO_ZERO, msg, 0, 0);
|
2007-12-02 19:11:44 +00:00
|
|
|
}
|
|
|
|
|
2008-01-22 20:12:27 +00:00
|
|
|
const char *iso_error_to_msg(int errcode)
|
2007-12-02 19:11:44 +00:00
|
|
|
{
|
2008-01-22 20:12:27 +00:00
|
|
|
/* TODO not implemented yet!!! */
|
|
|
|
return "TODO";
|
2007-12-02 19:11:44 +00:00
|
|
|
}
|
|
|
|
|
2008-01-22 20:36:24 +00:00
|
|
|
int iso_msg_submit(int imgid, int errcode, int causedby, const char *fmt, ...)
|
2007-12-02 19:11:44 +00:00
|
|
|
{
|
2007-12-28 21:10:17 +00:00
|
|
|
char msg[MAX_MSG_LEN];
|
2007-12-17 20:12:51 +00:00
|
|
|
va_list ap;
|
2008-01-22 20:12:27 +00:00
|
|
|
|
|
|
|
/* when called with ISO_CANCELED, we don't need to submit any message */
|
|
|
|
if (errcode == ISO_CANCELED && fmt == NULL) {
|
|
|
|
return ISO_CANCELED;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2008-01-22 20:12:27 +00:00
|
|
|
if (fmt) {
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(msg, MAX_MSG_LEN, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
} else {
|
|
|
|
strncpy(msg, iso_error_to_msg(errcode), MAX_MSG_LEN);
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2008-01-22 20:12:27 +00:00
|
|
|
libiso_msgs_submit(libiso_msgr, imgid, errcode, ISO_ERR_SEV(errcode),
|
|
|
|
ISO_ERR_PRIO(errcode), msg, 0, 0);
|
2008-01-22 20:36:24 +00:00
|
|
|
if (causedby != 0) {
|
|
|
|
iso_msg_debug(imgid, " > Caused by: %s", iso_error_to_msg(causedby));
|
|
|
|
if (ISO_ERR_SEV(causedby) == LIBISO_MSGS_SEV_FATAL) {
|
|
|
|
return ISO_CANCELED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-22 20:12:27 +00:00
|
|
|
if (ISO_ERR_SEV(errcode) >= abort_threshold) {
|
|
|
|
return ISO_CANCELED;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-12-02 19:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Control queueing and stderr printing of messages from libisofs.
|
|
|
|
* Severity may be one of "NEVER", "FATAL", "SORRY", "WARNING", "HINT",
|
|
|
|
* "NOTE", "UPDATE", "DEBUG", "ALL".
|
|
|
|
*
|
|
|
|
* @param queue_severity Gives the minimum limit for messages to be queued.
|
|
|
|
* Default: "NEVER". If you queue messages then you
|
|
|
|
* must consume them by iso_msgs_obtain().
|
|
|
|
* @param print_severity Does the same for messages to be printed directly
|
|
|
|
* to stderr.
|
|
|
|
* @param print_id A text prefix to be printed before the message.
|
|
|
|
* @return >0 for success, <=0 for error
|
|
|
|
*/
|
2008-01-19 01:48:12 +00:00
|
|
|
int iso_set_msgs_severities(char *queue_severity, char *print_severity,
|
|
|
|
char *print_id)
|
2007-12-02 19:11:44 +00:00
|
|
|
{
|
2007-12-28 21:10:17 +00:00
|
|
|
int ret, queue_sevno, print_sevno;
|
|
|
|
|
|
|
|
ret = libiso_msgs__text_to_sev(queue_severity, &queue_sevno, 0);
|
|
|
|
if (ret <= 0)
|
|
|
|
return 0;
|
|
|
|
ret = libiso_msgs__text_to_sev(print_severity, &print_sevno, 0);
|
|
|
|
if (ret <= 0)
|
|
|
|
return 0;
|
2008-01-19 01:48:12 +00:00
|
|
|
ret = libiso_msgs_set_severities(libiso_msgr, queue_sevno, print_sevno,
|
2007-12-28 21:10:17 +00:00
|
|
|
print_id, 0);
|
|
|
|
if (ret <= 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
2007-12-02 19:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain the oldest pending libisofs message from the queue which has at
|
|
|
|
* least the given minimum_severity. This message and any older message of
|
|
|
|
* lower severity will get discarded from the queue and is then lost forever.
|
|
|
|
*
|
|
|
|
* Severity may be one of "NEVER", "FATAL", "SORRY", "WARNING", "HINT",
|
|
|
|
* "NOTE", "UPDATE", "DEBUG", "ALL". To call with minimum_severity "NEVER"
|
|
|
|
* will discard the whole queue.
|
|
|
|
*
|
|
|
|
* @param error_code Will become a unique error code as listed in messages.h
|
|
|
|
* @param msg_text Must provide at least ISO_MSGS_MESSAGE_LEN bytes.
|
|
|
|
* @param os_errno Will become the eventual errno related to the message
|
|
|
|
* @param severity Will become the severity related to the message and
|
|
|
|
* should provide at least 80 bytes.
|
|
|
|
* @return 1 if a matching item was found, 0 if not, <0 for severe errors
|
|
|
|
*/
|
2008-01-19 01:48:12 +00:00
|
|
|
int iso_obtain_msgs(char *minimum_severity, int *error_code,
|
|
|
|
char msg_text[], int *os_errno, char severity[])
|
2007-12-02 19:11:44 +00:00
|
|
|
{
|
2007-12-28 21:10:17 +00:00
|
|
|
int ret, minimum_sevno, sevno, priority;
|
|
|
|
char *textpt, *sev_name;
|
|
|
|
struct libiso_msgs_item *item= NULL;
|
|
|
|
|
|
|
|
ret = libiso_msgs__text_to_sev(minimum_severity, &minimum_sevno, 0);
|
|
|
|
if (ret <= 0)
|
|
|
|
return 0;
|
2008-01-19 01:48:12 +00:00
|
|
|
ret = libiso_msgs_obtain(libiso_msgr, &item, minimum_sevno,
|
2007-12-28 21:10:17 +00:00
|
|
|
LIBISO_MSGS_PRIO_ZERO, 0);
|
|
|
|
if (ret <= 0)
|
|
|
|
goto ex;
|
|
|
|
ret = libiso_msgs_item_get_msg(item, error_code, &textpt, os_errno, 0);
|
|
|
|
if (ret <= 0)
|
|
|
|
goto ex;
|
|
|
|
strncpy(msg_text, textpt, ISO_MSGS_MESSAGE_LEN-1);
|
|
|
|
if (strlen(textpt) >= ISO_MSGS_MESSAGE_LEN)
|
|
|
|
msg_text[ISO_MSGS_MESSAGE_LEN-1] = 0;
|
|
|
|
|
|
|
|
severity[0]= 0;
|
|
|
|
ret = libiso_msgs_item_get_rank(item, &sevno, &priority, 0);
|
|
|
|
if (ret <= 0)
|
|
|
|
goto ex;
|
|
|
|
ret = libiso_msgs__sev_to_text(sevno, &sev_name, 0);
|
|
|
|
if (ret <= 0)
|
|
|
|
goto ex;
|
|
|
|
strcpy(severity, sev_name);
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
ex: ;
|
2008-01-19 01:48:12 +00:00
|
|
|
libiso_msgs_destroy_item(libiso_msgr, &item, 0);
|
2007-12-28 21:10:17 +00:00
|
|
|
return ret;
|
2007-12-02 19:11:44 +00:00
|
|
|
}
|
|
|
|
|
2008-01-19 01:48:12 +00:00
|
|
|
/**
|
|
|
|
* Return the messenger object handle used by libisofs. This handle
|
|
|
|
* may be used by related libraries to their own compatible
|
|
|
|
* messenger objects and thus to direct their messages to the libisofs
|
|
|
|
* message queue. See also: libburn, API function burn_set_messenger().
|
|
|
|
*
|
|
|
|
* @return the handle. Do only use with compatible
|
|
|
|
*/
|
|
|
|
void *iso_get_messenger()
|
2007-12-02 19:11:44 +00:00
|
|
|
{
|
2008-01-19 01:48:12 +00:00
|
|
|
return libiso_msgr;
|
2007-12-02 19:11:44 +00:00
|
|
|
}
|