return ret;
}
+/**
+ * Format and send an error message to the command handler.
+ *
+ * To pass an error message from the callback of an afs command to the client,
+ * this function should be called. It formats the message into a buffer which
+ * is passed as a shared memory area to the command handler from where it
+ * propagates to the client.
+ *
+ * The message will be tagged with the ERROR_LOG sideband designator so that
+ * the client writes it to its stderr stream rather than to stdout as with
+ * aca->pbout. In analogy to the default Unix semantics of stderr, the message
+ * is sent without buffering.
+ *
+ * If sending the error message fails, an error is logged on the server side,
+ * but no other action is taken.
+ *
+ * \param aca Used to obtain the fd to send the shmid to.
+ * \param fmt Usual format string.
+ */
+__printf_2_3 void afs_error(const struct afs_callback_arg *aca,
+ const char *fmt,...)
+{
+ va_list argp;
+ char *msg;
+ unsigned n;
+ int ret;
+
+ va_start(argp, fmt);
+ n = xvasprintf(&msg, fmt, argp);
+ va_end(argp);
+ ret = pass_buffer_as_shm(aca->fd, SBD_ERROR_LOG, msg, n + 1);
+ if (ret < 0)
+ PARA_ERROR_LOG("Could not send %s: %s\n", msg,
+ para_strerror(-ret));
+ free(msg);
+}
+
static int call_callback(int fd, int query_shmid)
{
void *query_shm;
/* ignore subsequent errors (but log them) */
if (current_mop && strcmp(current_mop, arg) != 0) {
int ret2;
- para_printf(&aca->pbout, "switching back to %s\n", current_mop);
+ afs_error(aca, "switching back to %s\n", current_mop);
ret2 = activate_mood_or_playlist(current_mop, &aca->pbout);
if (ret2 >= 0)
goto free_lpr;
- para_printf(&aca->pbout, "could not reactivate %s: %s\n",
- current_mop, para_strerror(-ret2));
+ afs_error(aca, "could not reactivate %s: %s\n", current_mop,
+ para_strerror(-ret2));
}
activate_mood_or_playlist(NULL, &aca->pbout);
free_lpr:
continue;
ret = t->ops->create(database_dir);
if (ret < 0) {
- para_printf(&aca->pbout, "cannot create table %s\n",
- t->name);
+ afs_error(aca, "cannot create table %s\n", t->name);
goto out;
}
para_printf(&aca->pbout, "successfully created %s table\n",
}
ret = open_afs_tables();
if (ret < 0)
- para_printf(&aca->pbout, "cannot open afs tables: %s\n",
+ afs_error(aca, "cannot open afs tables: %s\n",
para_strerror(-ret));
out:
return ret;
int send_lls_callback_request(afs_callback *f,
const struct lls_command * const cmd,
struct lls_parse_result *lpr, void *private_result_data);
+__printf_2_3 void afs_error(const struct afs_callback_arg *aca,
+ const char *fmt,...);
int string_compare(const struct osl_object *obj1, const struct osl_object *obj2);
int for_each_matching_row(struct pattern_match_data *pmd);
ret = afs_event(AUDIO_FILE_ADD, &aca->pbout, aft_row);
out:
if (ret < 0)
- para_printf(&aca->pbout, "could not add %s\n", path);
+ afs_error(aca, "could not add %s\n", path);
lls_free_parse_result(aca->lpr, cmd);
return ret;
}
ret = get_afsi_object_of_row(row, &obj);
if (ret < 0) {
- para_printf(&aca->pbout, "cannot touch %s\n", name);
+ afs_error(aca, "cannot touch %s\n", name);
return ret;
}
ret = load_afsi(&old_afsi, &obj);
if (ret < 0) {
- para_printf(&aca->pbout, "cannot touch %s\n", name);
+ afs_error(aca, "cannot touch %s\n", name);
return ret;
}
new_afsi = old_afsi;
uint32_t id = lls_uint32_val(0, r_i);
ret = img_get_name_by_id(id, NULL);
if (ret < 0) {
- para_printf(&aca->pbout, "invalid image ID: %u\n", id);
+ afs_error(aca, "invalid image ID: %u\n", id);
return ret;
}
}
uint32_t id = lls_uint32_val(0, r_y);
ret = lyr_get_name_by_id(id, NULL);
if (ret < 0) {
- para_printf(&aca->pbout, "invalid lyrics ID: %u\n", id);
+ afs_error(aca, "invalid lyrics ID: %u\n", id);
return ret;
}
}
return ret;
ret = osl(osl_del_row(audio_file_table, row));
if (ret < 0)
- para_printf(&aca->pbout, "cannot remove %s\n", name);
+ afs_error(aca, "cannot remove %s\n", name);
return ret;
}
ret = get_attribute_bitnum_by_name(p, &bitnum);
free(p);
if (ret < 0) {
- para_printf(&aca->pbout, "invalid argument: %s\n", arg);
+ afs_error(aca, "invalid argument: %s\n", arg);
goto out;
}
if (c == '+')
}
ret = osl(osl_get_object(table, row, ATTCOL_BITNUM, &bitnum_obj));
if (ret < 0) {
- para_printf(&aca->pbout, "%s: %s\n", name, para_strerror(-ret));
+ afs_error(aca, "%s: %s\n", name, para_strerror(-ret));
return ret;
}
para_printf(&aca->pbout, "%u\t%s\n", *(unsigned char*)bitnum_obj.data,
len = strlen(name);
if (len == 0 || name[len - 1] == '-' || name[len - 1] == '+') {
- para_printf(&aca->pbout,
- "invalid attribute name: %s\n", name);
+ afs_error(aca, "invalid attribute name: %s\n", name);
continue;
}
ret = get_attribute_bitnum_by_name(name, &bitnum);
}
out:
if (ret < 0)
- para_printf(&aca->pbout, "error while adding %s\n",
+ afs_error(aca, "error while adding %s\n",
lls_input(i, aca->lpr));
lls_free_parse_result(aca->lpr, cmd);
return ret;
ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
out:
if (ret < 0)
- para_printf(&aca->pbout, "cannot rename %s to %s\n", old, new);
+ afs_error(aca, "cannot rename %s to %s\n", old, new);
else
ret = afs_event(ATTRIBUTE_RENAME, &aca->pbout, NULL);
lls_free_parse_result(aca->lpr, cmd);
ret = get_attribute_bitnum_by_name(name, &red.bitnum);
if (ret < 0) {
- para_printf(&aca->pbout, "cannot remove %s\n", name);
+ afs_error(aca, "cannot remove %s\n", name);
return ret;
}
para_printf(&aca->pbout, "removing attribute %s\n", name);
ret = osl(osl_del_row(table, row));
if (ret < 0) {
- para_printf(&aca->pbout, "cannot remove %s\n", name);
+ afs_error(aca, "cannot remove %s\n", name);
return ret;
}
return afs_event(ATTRIBUTE_REMOVE, &aca->pbout, &red);
}
ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
if (ret < 0) {
- para_printf(&aca->pbout, "cannot list %s\n", name);
+ afs_error(aca, "cannot list %s\n", name);
return ret;
}
id = read_u32(obj.data);
int ret = osl(osl_del_row(table, row));
if (ret < 0) {
- para_printf(&aca->pbout, "cannot remove %s\n", name);
+ afs_error(aca, "cannot remove %s\n", name);
return ret;
}
return 1;
ret = afs_event(BLOB_ADD, NULL, table);
out:
if (ret < 0)
- para_printf(&aca->pbout, "cannot add %s\n", name);
+ afs_error(aca, "cannot add %s\n", name);
else
para_printf(&aca->pbout, "added %s as id %u\n", name, id);
return ret;
ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
if (ret < 0) {
- para_printf(&aca->pbout, "cannot find source blob %s\n", src);
+ afs_error(aca, "cannot find source blob %s\n", src);
goto out;
}
obj.data = (char *)dest;
obj.size = strlen(dest) + 1;
ret = osl(osl_update_object(table, row, BLOBCOL_NAME, &obj));
if (ret < 0) {
- para_printf(&aca->pbout, "cannot rename blob %s to %s\n",
- src, dest);
+ afs_error(aca, "cannot rename blob %s to %s\n", src, dest);
goto out;
}
ret = afs_event(BLOB_RENAME, NULL, table);
static int check_mood(struct osl_row *mood_row, void *data)
{
- struct para_buffer *pb = data;
+ struct afs_callback_arg *aca = data;
char *mood_name, *errmsg;
struct osl_object mood_def;
struct mood_instance *m;
int ret = mood_get_name_and_def_by_row(mood_row, &mood_name, &mood_def);
if (ret < 0) {
- para_printf(pb, "cannot read mood\n");
+ afs_error(aca, "cannot read mood\n");
return ret;
}
if (!*mood_name) /* ignore dummy row */
ret = mp_init(mood_def.data, mood_def.size, &m->parser_context,
&errmsg);
if (ret < 0) {
- para_printf(pb, "%s: %s\n", mood_name, errmsg);
+ afs_error(aca, "%s: %s\n%s\n", mood_name, errmsg,
+ para_strerror(-ret));
free(errmsg);
- para_printf(pb, "%s\n", para_strerror(-ret));
} else
destroy_mood(m);
ret = 1; /* don't fail the loop on invalid mood definitions */
/**
* Check all moods for syntax errors.
*
- * \param aca Only ->pbout is used for diagnostics.
+ * \param aca Output goes to ->pbout, errors to ->fd on the error band.
*
* \return Negative on fatal errors. Inconsistent mood definitions are not
* considered an error.
int mood_check_callback(struct afs_callback_arg *aca)
{
para_printf(&aca->pbout, "checking moods...\n");
- return osl(osl_rbtree_loop(moods_table, BLOBCOL_ID, &aca->pbout,
- check_mood));
+ return osl(osl_rbtree_loop(moods_table, BLOBCOL_ID, aca, check_mood));
}
/*
static int check_playlist_path(char *path, void *data)
{
- struct para_buffer *pb = data;
+ struct afs_callback_arg *aca = data;
struct osl_row *aft_row;
int ret = aft_get_row_of_path(path, &aft_row);
if (ret < 0)
- para_printf(pb, "%s: %s\n", path, para_strerror(-ret));
+ afs_error(aca, "%s: %s\n", path, para_strerror(-ret));
return 1; /* do not fail the loop on bad paths */
}
static int check_playlist(struct osl_row *row, void *data)
{
- struct para_buffer *pb = data;
+ struct afs_callback_arg *aca = data;
+ struct para_buffer *pb = &aca->pbout;
struct osl_object playlist_def;
char *playlist_name;
int ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
if (ret < 0) { /* log error, but continue */
- para_printf(pb, "failed to get playlist data: %s\n",
+ afs_error(aca, "failed to get playlist data: %s\n",
para_strerror(-ret));
return 1;
}
if (*playlist_name) { /* skip dummy row */
para_printf(pb, "checking playlist %s...\n", playlist_name);
for_each_line(FELF_READ_ONLY, playlist_def.data,
- playlist_def.size, check_playlist_path, pb);
+ playlist_def.size, check_playlist_path, aca);
}
osl_close_disk_object(&playlist_def);
return 1;
int playlist_check_callback(struct afs_callback_arg *aca)
{
para_printf(&aca->pbout, "checking playlists...\n");
- return osl(osl_rbtree_loop(playlists_table, BLOBCOL_ID, &aca->pbout,
+ return osl(osl_rbtree_loop(playlists_table, BLOBCOL_ID, aca,
check_playlist));
}