15c51f124SMoriah Waterland /*
25c51f124SMoriah Waterland * CDDL HEADER START
35c51f124SMoriah Waterland *
45c51f124SMoriah Waterland * The contents of this file are subject to the terms of the
55c51f124SMoriah Waterland * Common Development and Distribution License (the "License").
65c51f124SMoriah Waterland * You may not use this file except in compliance with the License.
75c51f124SMoriah Waterland *
85c51f124SMoriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95c51f124SMoriah Waterland * or http://www.opensolaris.org/os/licensing.
105c51f124SMoriah Waterland * See the License for the specific language governing permissions
115c51f124SMoriah Waterland * and limitations under the License.
125c51f124SMoriah Waterland *
135c51f124SMoriah Waterland * When distributing Covered Code, include this CDDL HEADER in each
145c51f124SMoriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155c51f124SMoriah Waterland * If applicable, add the following below this CDDL HEADER, with the
165c51f124SMoriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying
175c51f124SMoriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner]
185c51f124SMoriah Waterland *
195c51f124SMoriah Waterland * CDDL HEADER END
205c51f124SMoriah Waterland */
215c51f124SMoriah Waterland
225c51f124SMoriah Waterland /*
2332991bedSPeter Tribble * Copyright (c) 2017 Peter Tribble.
2432991bedSPeter Tribble */
2532991bedSPeter Tribble
2632991bedSPeter Tribble /*
275c51f124SMoriah Waterland * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
285c51f124SMoriah Waterland * Use is subject to license terms.
295c51f124SMoriah Waterland */
305c51f124SMoriah Waterland
315c51f124SMoriah Waterland
325c51f124SMoriah Waterland /* unix system includes */
335c51f124SMoriah Waterland
345c51f124SMoriah Waterland #include <stdio.h>
355c51f124SMoriah Waterland #include <stdarg.h>
365c51f124SMoriah Waterland #include <stdlib.h>
375c51f124SMoriah Waterland #include <string.h>
385c51f124SMoriah Waterland #include <sys/types.h>
395c51f124SMoriah Waterland #include <unistd.h>
405c51f124SMoriah Waterland #include <locale.h>
415c51f124SMoriah Waterland #include <sys/param.h>
425c51f124SMoriah Waterland
435c51f124SMoriah Waterland #include <pkglib.h>
445c51f124SMoriah Waterland #include "pkgadm.h"
455c51f124SMoriah Waterland #include "pkgadm_msgs.h"
4662224350SCasper H.S. Dik #include "libadm.h"
475c51f124SMoriah Waterland
485c51f124SMoriah Waterland /* Local Function Prototypes */
495c51f124SMoriah Waterland
505c51f124SMoriah Waterland static void print_version();
515c51f124SMoriah Waterland int get_dbstatus(int argc, char **argv);
5262224350SCasper H.S. Dik int sync_server(int argc, char **argv);
535c51f124SMoriah Waterland
545c51f124SMoriah Waterland /* holds subcommands and their definitions */
555c51f124SMoriah Waterland struct cmd {
565c51f124SMoriah Waterland char *c_name;
575c51f124SMoriah Waterland int (*c_func)(int, char **);
585c51f124SMoriah Waterland };
595c51f124SMoriah Waterland
605c51f124SMoriah Waterland struct cmd cmds[] = {
615c51f124SMoriah Waterland { "dbstatus", get_dbstatus},
625c51f124SMoriah Waterland { "lock", admin_lock},
6362224350SCasper H.S. Dik { "sync", sync_server},
645c51f124SMoriah Waterland /* last one must be all NULLs */
655c51f124SMoriah Waterland { NULL, NULL }
665c51f124SMoriah Waterland };
675c51f124SMoriah Waterland
685c51f124SMoriah Waterland /*
695c51f124SMoriah Waterland * Function: main
705c51f124SMoriah Waterland *
715c51f124SMoriah Waterland * Return: 0 - subprocessing successful
725c51f124SMoriah Waterland * scripts and reboot
735c51f124SMoriah Waterland * [other] - subprocessing-specific failure
745c51f124SMoriah Waterland */
755c51f124SMoriah Waterland int
main(int argc,char ** argv)765c51f124SMoriah Waterland main(int argc, char **argv)
775c51f124SMoriah Waterland {
78*2e10def1SPeter Tribble int cur_cmd;
795c51f124SMoriah Waterland int newargc;
805c51f124SMoriah Waterland char **newargv;
815c51f124SMoriah Waterland int i;
825c51f124SMoriah Waterland
835c51f124SMoriah Waterland /* Should be defined by cc -D */
845c51f124SMoriah Waterland #if !defined(TEXT_DOMAIN)
855c51f124SMoriah Waterland #define TEXT_DOMAIN "SYS_TEST"
865c51f124SMoriah Waterland #endif
875c51f124SMoriah Waterland
885c51f124SMoriah Waterland /* set the default text domain for messaging */
895c51f124SMoriah Waterland (void) setlocale(LC_ALL, "");
905c51f124SMoriah Waterland (void) textdomain(TEXT_DOMAIN);
915c51f124SMoriah Waterland
925c51f124SMoriah Waterland if (getenv("PKGADM_VERBOSE")) {
935c51f124SMoriah Waterland set_verbose(B_TRUE);
945c51f124SMoriah Waterland }
955c51f124SMoriah Waterland
965c51f124SMoriah Waterland /* Superficial check of the arguments. */
975c51f124SMoriah Waterland if (argc <= 1) {
985c51f124SMoriah Waterland log_msg(LOG_MSG_INFO, MSG_USAGE);
995c51f124SMoriah Waterland return (1);
1005c51f124SMoriah Waterland }
1015c51f124SMoriah Waterland
1025c51f124SMoriah Waterland /* first, process any arguments that can appear before the subcommand */
1035c51f124SMoriah Waterland while ((i = getopt(argc, argv, "vV?")) != EOF) {
1045c51f124SMoriah Waterland switch (i) {
1055c51f124SMoriah Waterland case 'v': /* verbose mode enabled */
1065c51f124SMoriah Waterland set_verbose(B_TRUE);
1075c51f124SMoriah Waterland break;
1085c51f124SMoriah Waterland case 'V':
1095c51f124SMoriah Waterland print_version();
1105c51f124SMoriah Waterland return (0);
1115c51f124SMoriah Waterland case '?':
1125c51f124SMoriah Waterland log_msg(LOG_MSG_INFO, MSG_USAGE);
1135c51f124SMoriah Waterland return (0);
1145c51f124SMoriah Waterland }
1155c51f124SMoriah Waterland }
1165c51f124SMoriah Waterland
1175c51f124SMoriah Waterland /* OK, hand it off to the subcommand processors */
1185c51f124SMoriah Waterland for (cur_cmd = 0; cmds[cur_cmd].c_name != NULL; cur_cmd++) {
1195c51f124SMoriah Waterland if (ci_streq(argv[optind], cmds[cur_cmd].c_name)) {
1205c51f124SMoriah Waterland /* make subcommand the first option */
1215c51f124SMoriah Waterland newargc = argc - optind;
1225c51f124SMoriah Waterland newargv = argv + optind;
1235c51f124SMoriah Waterland opterr = optind = 1; optopt = 0;
1245c51f124SMoriah Waterland return (cmds[cur_cmd].c_func(newargc, newargv));
1255c51f124SMoriah Waterland }
1265c51f124SMoriah Waterland }
1275c51f124SMoriah Waterland
1285c51f124SMoriah Waterland /* bad subcommand */
1295c51f124SMoriah Waterland log_msg(LOG_MSG_ERR, MSG_BAD_SUB, argv[optind]);
1305c51f124SMoriah Waterland log_msg(LOG_MSG_INFO, MSG_USAGE);
1315c51f124SMoriah Waterland return (1);
1325c51f124SMoriah Waterland }
1335c51f124SMoriah Waterland
1345c51f124SMoriah Waterland /*
1355c51f124SMoriah Waterland * Name: set_verbose
1365c51f124SMoriah Waterland * Description: Turns on verbose output
1375c51f124SMoriah Waterland * Scope: public
1385c51f124SMoriah Waterland * Arguments: verbose = B_TRUE indicates verbose mode
1395c51f124SMoriah Waterland * Returns: none
1405c51f124SMoriah Waterland */
1415c51f124SMoriah Waterland void
set_verbose(boolean_t setting)1425c51f124SMoriah Waterland set_verbose(boolean_t setting)
1435c51f124SMoriah Waterland {
1445c51f124SMoriah Waterland log_set_verbose(setting);
1455c51f124SMoriah Waterland }
1465c51f124SMoriah Waterland
1475c51f124SMoriah Waterland /*
1485c51f124SMoriah Waterland * Name: get_verbose
1495c51f124SMoriah Waterland * Description: Returns whether or not to output verbose messages
1505c51f124SMoriah Waterland * Scope: public
1515c51f124SMoriah Waterland * Arguments: none
1525c51f124SMoriah Waterland * Returns: B_TRUE - verbose messages should be output
1535c51f124SMoriah Waterland */
1545c51f124SMoriah Waterland boolean_t
get_verbose()1555c51f124SMoriah Waterland get_verbose()
1565c51f124SMoriah Waterland {
1575c51f124SMoriah Waterland return (log_get_verbose());
1585c51f124SMoriah Waterland }
1595c51f124SMoriah Waterland
1605c51f124SMoriah Waterland /*
1615c51f124SMoriah Waterland * Name: print_Version
1625c51f124SMoriah Waterland * Desc: Prints Version of packaging tools
1635c51f124SMoriah Waterland * Arguments: none
1645c51f124SMoriah Waterland * Returns: none
1655c51f124SMoriah Waterland */
1665c51f124SMoriah Waterland static void
print_version()1675c51f124SMoriah Waterland print_version()
1685c51f124SMoriah Waterland {
1695c51f124SMoriah Waterland /* ignore any and all arguments, print version only */
1705c51f124SMoriah Waterland (void) fprintf(stdout, "%s\n", SUNW_PKGVERS);
1715c51f124SMoriah Waterland }
1725c51f124SMoriah Waterland
1735c51f124SMoriah Waterland /*
1745c51f124SMoriah Waterland * usage
1755c51f124SMoriah Waterland *
1765c51f124SMoriah Waterland * Outputs the usage string.
1775c51f124SMoriah Waterland *
1785c51f124SMoriah Waterland * Return:1
1795c51f124SMoriah Waterland * Side effects: none
1805c51f124SMoriah Waterland */
1815c51f124SMoriah Waterland static int
usage()1825c51f124SMoriah Waterland usage()
1835c51f124SMoriah Waterland {
1845c51f124SMoriah Waterland log_msg(LOG_MSG_INFO, MSG_USAGE);
1855c51f124SMoriah Waterland return (1);
1865c51f124SMoriah Waterland }
1875c51f124SMoriah Waterland
1885c51f124SMoriah Waterland /*
1895c51f124SMoriah Waterland * get_dbstatus
1905c51f124SMoriah Waterland *
1915c51f124SMoriah Waterland * Return 'text' as the db status.
1925c51f124SMoriah Waterland * Use the command line to determine if there is an alternate root.
1935c51f124SMoriah Waterland *
1945c51f124SMoriah Waterland * Return: 0 on success, nonzero on failure
1955c51f124SMoriah Waterland * Side effects: none
1965c51f124SMoriah Waterland */
1975c51f124SMoriah Waterland int
get_dbstatus(int argc,char ** argv)1985c51f124SMoriah Waterland get_dbstatus(int argc, char **argv)
1995c51f124SMoriah Waterland {
2005c51f124SMoriah Waterland /* Either accept 1 argument or 3 arguments where the second is -R */
2015c51f124SMoriah Waterland if (argc != 1 && (argc != 3 || strcmp(argv[1], "-R")))
2025c51f124SMoriah Waterland return (usage());
2035c51f124SMoriah Waterland
2045c51f124SMoriah Waterland (void) printf("%s\n", PKGADM_DBSTATUS_TEXT);
2055c51f124SMoriah Waterland
2065c51f124SMoriah Waterland return (0);
2075c51f124SMoriah Waterland }
20862224350SCasper H.S. Dik
20962224350SCasper H.S. Dik /*
21062224350SCasper H.S. Dik * sync
21162224350SCasper H.S. Dik *
21262224350SCasper H.S. Dik * Use the command line to determine if there is an alternate root.
21362224350SCasper H.S. Dik *
21462224350SCasper H.S. Dik * Return: 0 on success, nonzero on failure
21562224350SCasper H.S. Dik * Flush the pkgserv's log.
21662224350SCasper H.S. Dik */
21762224350SCasper H.S. Dik int
sync_server(int argc,char ** argv)21862224350SCasper H.S. Dik sync_server(int argc, char **argv)
21962224350SCasper H.S. Dik {
22062224350SCasper H.S. Dik int c;
22162224350SCasper H.S. Dik char *root = NULL;
222af122237SJan Kryl char *dryrundir = NULL;
22362224350SCasper H.S. Dik boolean_t quit = B_FALSE;
22462224350SCasper H.S. Dik
225af122237SJan Kryl /*
226af122237SJan Kryl * Options:
227af122237SJan Kryl * -q: Tell pkgserv daemon to quit.
228af122237SJan Kryl * -R: Alternate root specification.
229af122237SJan Kryl * -D: Dryrun directory specification.
230af122237SJan Kryl *
231af122237SJan Kryl * -R and -D help pkgadm to locate IPC files used for communication
232af122237SJan Kryl * with pkgserv daemon. They should not be used together, though
233af122237SJan Kryl * nothing prevents you from doing so. If you use both at once
234af122237SJan Kryl * then IPC files will be searched in $ROOTDIR/$DRYRUNDIR directory.
235af122237SJan Kryl * So if you want to terminate dryrun pkgserv process, you should
236af122237SJan Kryl * always use only -D option.
237af122237SJan Kryl */
238af122237SJan Kryl while ((c = getopt(argc, argv, "D:R:q")) != EOF) {
23962224350SCasper H.S. Dik switch (c) {
240af122237SJan Kryl case 'D':
241af122237SJan Kryl dryrundir = optarg;
242af122237SJan Kryl break;
24362224350SCasper H.S. Dik case 'R':
24462224350SCasper H.S. Dik root = optarg;
24562224350SCasper H.S. Dik break;
24662224350SCasper H.S. Dik case 'q':
24762224350SCasper H.S. Dik quit = B_TRUE;
24862224350SCasper H.S. Dik break;
24962224350SCasper H.S. Dik default:
25062224350SCasper H.S. Dik return (usage());
25162224350SCasper H.S. Dik }
25262224350SCasper H.S. Dik }
25362224350SCasper H.S. Dik
254af122237SJan Kryl if (!pkgsync_needed(root, dryrundir, quit))
25562224350SCasper H.S. Dik return (0);
25662224350SCasper H.S. Dik
25762224350SCasper H.S. Dik set_PKGpaths(root);
258af122237SJan Kryl set_cfdir(dryrundir);
25962224350SCasper H.S. Dik
26062224350SCasper H.S. Dik if (pkgWlock(1) == 1) {
26162224350SCasper H.S. Dik /* Flush the log file */
262af122237SJan Kryl (void) pkgsync(root, dryrundir, quit);
26362224350SCasper H.S. Dik (void) relslock();
26462224350SCasper H.S. Dik return (0);
26562224350SCasper H.S. Dik }
26662224350SCasper H.S. Dik
26762224350SCasper H.S. Dik return (1);
26862224350SCasper H.S. Dik }
269