Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/makemandb apropos(1): improve error handling in edg...



details:   https://anonhg.NetBSD.org/src/rev/3896f764d98d
branches:  trunk
changeset: 366222:3896f764d98d
user:      gutteridge <gutteridge%NetBSD.org@localhost>
date:      Thu May 19 04:08:03 2022 +0000

description:
apropos(1): improve error handling in edge cases

Patch from RVP on NetBSD-Users, with an additional comment tweak by me.
Summary from RVP:

1. Ignore SIGPIPE so that we're not killed in the middle of some
   DB operation by a botched $PAGER:

$ env PAGER=/non-existent apropos -p ...

2. Return proper exit status in case of write errors:

$ apropos ... >/dev/full || echo fail

diffstat:

 usr.sbin/makemandb/apropos-utils.c |  30 +++++++++++++++---------------
 usr.sbin/makemandb/apropos.c       |  21 ++++++++++++++-------
 2 files changed, 29 insertions(+), 22 deletions(-)

diffs (172 lines):

diff -r 21cecadadb4a -r 3896f764d98d usr.sbin/makemandb/apropos-utils.c
--- a/usr.sbin/makemandb/apropos-utils.c        Thu May 19 03:58:13 2022 +0000
+++ b/usr.sbin/makemandb/apropos-utils.c        Thu May 19 04:08:03 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: apropos-utils.c,v 1.48 2021/11/27 22:30:25 rillig Exp $        */
+/*     $NetBSD: apropos-utils.c,v 1.49 2022/05/19 04:08:03 gutteridge Exp $    */
 /*-
  * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay%gmail.com@localhost>
  * All rights reserved.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: apropos-utils.c,v 1.48 2021/11/27 22:30:25 rillig Exp $");
+__RCSID("$NetBSD: apropos-utils.c,v 1.49 2022/05/19 04:08:03 gutteridge Exp $");
 
 #include <sys/queue.h>
 #include <sys/stat.h>
@@ -665,7 +665,7 @@
  * Execute the full text search query and return the number of results
  * obtained.
  */
-static unsigned int
+static int
 execute_search_query(sqlite3 *db, char *query, query_args *args)
 {
        sqlite3_stmt *stmt;
@@ -699,8 +699,8 @@
                return -1;
        }
 
-       unsigned int nresults = 0;
-       while (sqlite3_step(stmt) == SQLITE_ROW) {
+       int nresults = rc = 0;
+       while (rc == 0 && sqlite3_step(stmt) == SQLITE_ROW) {
                nresults++;
                callback_args.section = get_stmt_col_text(stmt, 0);
                name_temp = get_stmt_col_text(stmt, 1);
@@ -725,11 +725,11 @@
                }
                callback_args.name = name;
                callback_args.other_data = args->callback_data;
-               (args->callback)(&callback_args);
+               rc = (args->callback)(&callback_args);
                free(name);
        }
        sqlite3_finalize(stmt);
-       return nresults;
+       return (rc < 0) ? rc : nresults;
 }
 
 
@@ -752,9 +752,9 @@
                return -1;
        }
 
-       execute_search_query(db, query, args);
+       int rc = execute_search_query(db, query, args);
        sqlite3_free(query);
-       return *(args->errmsg) == NULL ? 0 : -1;
+       return (rc < 0 || *(args->errmsg) != NULL) ? -1 : 0;
 }
 
 static char *
@@ -845,10 +845,10 @@
        callback_args->snippet = qsnippet;
        callback_args->snippet_length = length;
        callback_args->other_data = orig_data->data;
-       (*callback)(callback_args);
+       int rc = (*callback)(callback_args);
        free(qsnippet);
        free(qname_description);
-       return 0;
+       return rc;
 }
 
 /*
@@ -968,12 +968,12 @@
        callback_args->snippet = psnippet;
        callback_args->snippet_length = psnippet_length;
        callback_args->other_data = orig_data->data;
-       (orig_data->callback)(callback_args);
+       int rc = (orig_data->callback)(callback_args);
        free(ul_section);
        free(ul_name);
        free(ul_name_desc);
        free(psnippet);
-       return 0;
+       return rc;
 }
 
 struct term_args {
@@ -1013,11 +1013,11 @@
        callback_args->name = ul_name;
        callback_args->name_desc = ul_name_desc;
        callback_args->other_data = orig_data->data;
-       (orig_data->callback)(callback_args);
+       int rc = (orig_data->callback)(callback_args);
        free(ul_section);
        free(ul_name);
        free(ul_name_desc);
-       return 0;
+       return rc;
 }
 
 /*
diff -r 21cecadadb4a -r 3896f764d98d usr.sbin/makemandb/apropos.c
--- a/usr.sbin/makemandb/apropos.c      Thu May 19 03:58:13 2022 +0000
+++ b/usr.sbin/makemandb/apropos.c      Thu May 19 04:08:03 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: apropos.c,v 1.25 2022/05/17 00:21:22 gutteridge Exp $  */
+/*     $NetBSD: apropos.c,v 1.26 2022/05/19 04:08:03 gutteridge Exp $  */
 /*-
  * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay%gmail.com@localhost>
  * All rights reserved.
@@ -31,9 +31,10 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: apropos.c,v 1.25 2022/05/17 00:21:22 gutteridge Exp $");
+__RCSID("$NetBSD: apropos.c,v 1.26 2022/05/19 04:08:03 gutteridge Exp $");
 
 #include <err.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -223,6 +224,10 @@
                const char *pager = getenv("PAGER");
                if (pager == NULL)
                        pager = _PATH_PAGER;
+
+               /* Don't get killed by a broken pipe */
+               signal(SIGPIPE, SIG_IGN);
+
                /* Open a pipe to the pager */
                if ((cbdata.out = popen(pager, "w")) == NULL) {
                        close_db(db);
@@ -270,10 +275,12 @@
        if (pc == -1)
                err(EXIT_FAILURE, "pclose error");
 
-       if (rc < 0) {
-               /* Something wrong with the database. Exit */
+       /* 
+        * Something wrong with the database, writing output, or a non-existent
+        * pager.
+        */
+       if (rc < 0)
                exit(EXIT_FAILURE);
-       }
 
        if (cbdata.count == 0) {
                warnx("No relevant results obtained.\n"
@@ -286,7 +293,7 @@
 /*
  * query_callback --
  *  Callback function for run_query.
- *  It simply outputs the results from do_query. If the user specified the -p
+ *  It simply outputs the results from run_query. If the user specified the -p
  *  option, then the output is sent to a pager, otherwise stdout is the default
  *  output stream.
  */
@@ -308,7 +315,7 @@
                    fprintf(out, "<tr><td colspan=2>%s</td></tr>\n", qargs->snippet);
        }
 
-       return 0;
+       return fflush(out);
 }
 
 #include "stopwords.c"



Home | Main Index | Thread Index | Old Index