aboutsummaryrefslogtreecommitdiff
path: root/sbin
diff options
context:
space:
mode:
authorKenneth D. Merry <ken@FreeBSD.org>2016-12-01 22:20:27 +0000
committerKenneth D. Merry <ken@FreeBSD.org>2016-12-01 22:20:27 +0000
commit28db0a5e740c9929ce560572efee40099428a246 (patch)
tree5df4ff924590f9ee9567d1882590971b28d450ee /sbin
parent710542df20ed68759eb8370402305eef8f68ffe5 (diff)
downloadsrc-28db0a5e740c9929ce560572efee40099428a246.tar.gz
src-28db0a5e740c9929ce560572efee40099428a246.zip
Add SCSI REPORT TIMESTAMP and SET TIMESTAMP support.
This adds support to camcontrol(8) and libcam(3) for getting and setting the time on SCSI protocol drives. This is more commonly found on tape drives, but is a SPC (SCSI Primary Commands) command, and may be found on any device that speaks SCSI. The new camcontrol timestamp subcommand allows getting the current device time or setting the time to the current system time or any arbitrary time. sbin/camcontrol/Makefile: Add timestamp.c. sbin/camcontrol/camcontrol.8: Document the new timestamp subcommand. sbin/camcontrol/camcontrol.c: Add the timestamp subcommand to camcontrol. sbin/camcontrol/camcontrol.h: Add the timestamp() function prototype. sbin/camcontrol/timestamp.c: Timestamp setting and reporting functionality. sys/cam/scsi/scsi_all.c: Add two new CCB building functions, scsi_set_timestamp() and scsi_report_timestamp(). Also, add a new helper function, scsi_create_timestamp(). sys/cam/scsi/scsi_all.h: Add CDB and parameter data for the the set and report timestamp commands. Add function declarations for the new CCB building and helper functions. Submitted by: Sam Klopsch Sponsored by: Spectra Logic MFC After: 2 weeks
Notes
Notes: svn path=/head/; revision=309374
Diffstat (limited to 'sbin')
-rw-r--r--sbin/camcontrol/Makefile2
-rw-r--r--sbin/camcontrol/camcontrol.867
-rw-r--r--sbin/camcontrol/camcontrol.c23
-rw-r--r--sbin/camcontrol/camcontrol.h3
-rw-r--r--sbin/camcontrol/timestamp.c494
5 files changed, 586 insertions, 3 deletions
diff --git a/sbin/camcontrol/Makefile b/sbin/camcontrol/Makefile
index dc9320ac3170..a1ad69309c79 100644
--- a/sbin/camcontrol/Makefile
+++ b/sbin/camcontrol/Makefile
@@ -4,7 +4,7 @@ PACKAGE=runtime
PROG= camcontrol
SRCS= camcontrol.c util.c
.if !defined(RELEASE_CRUNCH)
-SRCS+= attrib.c epc.c fwdownload.c modeedit.c persist.c progress.c zone.c
+SRCS+= attrib.c epc.c fwdownload.c modeedit.c persist.c progress.c timestamp.c zone.c
.else
CFLAGS+= -DMINIMALISTIC
.endif
diff --git a/sbin/camcontrol/camcontrol.8 b/sbin/camcontrol/camcontrol.8
index 5997e1f2c318..ee44d55772c6 100644
--- a/sbin/camcontrol/camcontrol.8
+++ b/sbin/camcontrol/camcontrol.8
@@ -27,7 +27,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd May 16, 2016
+.Dd November 30, 2016
.Dt CAMCONTROL 8
.Os
.Sh NAME
@@ -343,6 +343,11 @@
.Op Fl S Ar power_src
.Op Fl T Ar timer
.Nm
+.Ic timestamp
+.Op device id
+.Op generic args
+.Ao Fl r Oo Ns Fl f Ar format | Fl m | Fl U Oc | Fl s Ao Fl f Ar format Fl T Ar time | Fl U Ac Ac
+.Nm
.Ic help
.Sh DESCRIPTION
The
@@ -2417,6 +2422,54 @@ supports, and a number of parameters about each condition, including
whether it is enabled and what the timer value is.
.El
.El
+.It Ic timestamp
+Issue REPORT TIMESTAMP or SET TIMESTAMP
+.Tn SCSI
+commands. Either the
+.Fl r
+option or the
+.Fl s
+option must be specified.
+.Bl -tag -width 6n
+.It Fl r
+Report the device's timestamp.
+If no more arguments are specified, the timestamp will be reported using
+the national representation of the date and time, followed by the time
+zone.
+.Bl -tag -width 9n
+.It Fl f Ar format
+Specify the strftime format string, as documented in strftime(3), to be used
+to format the reported timestamp.
+.It Fl m
+Report the timestamp as milliseconds since the epoch.
+.It Fl U
+Report the timestamp using the national representation of the date and
+time, but override the system time zone and use UTC instead.
+.El
+.El
+.Bl -tag -width 6n
+.It Fl s
+Set the device's timestamp. Either the
+.Fl f
+and
+.Fl T
+options or the
+.Fl U
+option must be specified.
+.Bl -tag -width 9n
+.It Fl f Ar format
+Specify the strptime format string, as documented in strptime(3).
+The time must also be specified with the
+.Fl T
+option.
+.It Fl T
+Provide the time in the format specified with the
+.Fl f
+option.
+.It Fl U
+Set the timestamp to the host system's time in UTC.
+.El
+.El
.It Ic help
Print out verbose usage information.
.El
@@ -2730,6 +2783,18 @@ camcontrol epc ada0 -c list
Display the ATA Power Conditions log (Log Address 0x08) for
drive
.Pa ada0 .
+.Pp
+.Bd -literal -offset indent
+camcontrol timestamp sa0 -s -f "%A %c" \e
+ -T "Wednesday Wed Oct 26 21:43:57 2016"
+.Ed
+.Pp
+Set the timestamp of drive
+.Pa sa0
+using a
+.Xr strptime 3
+format string followed by a time string
+that was created using this format string.
.Sh SEE ALSO
.Xr cam 3 ,
.Xr cam_cdbparse 3 ,
diff --git a/sbin/camcontrol/camcontrol.c b/sbin/camcontrol/camcontrol.c
index 3a443a9c1f3e..8fdb45ca9cf6 100644
--- a/sbin/camcontrol/camcontrol.c
+++ b/sbin/camcontrol/camcontrol.c
@@ -103,7 +103,8 @@ typedef enum {
CAM_CMD_OPCODES = 0x00000024,
CAM_CMD_REPROBE = 0x00000025,
CAM_CMD_ZONE = 0x00000026,
- CAM_CMD_EPC = 0x00000027
+ CAM_CMD_EPC = 0x00000027,
+ CAM_CMD_TIMESTAMP = 0x00000028
} cam_cmdmask;
typedef enum {
@@ -234,6 +235,7 @@ static struct camcontrol_opts option_table[] = {
{"opcodes", CAM_CMD_OPCODES, CAM_ARG_NONE, "No:s:T"},
{"zone", CAM_CMD_ZONE, CAM_ARG_NONE, "ac:l:No:P:"},
{"epc", CAM_CMD_EPC, CAM_ARG_NONE, "c:dDeHp:Pr:sS:T:"},
+ {"timestamp", CAM_CMD_TIMESTAMP, CAM_ARG_NONE, "f:mrsUT:"},
#endif /* MINIMALISTIC */
{"help", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
{"-?", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
@@ -8922,6 +8924,9 @@ usage(int printlong)
" camcontrol epc [dev_id][generic_args]<-c cmd> [-d] [-D] [-e]\n"
" [-H] [-p power_cond] [-P] [-r rst_src] [-s]\n"
" [-S power_src] [-T timer]\n"
+" camcontrol timestamp [dev_id][generic_args] <-r [-f format|-m|-U]>|\n"
+" <-s <-f format -T time | -U >>\n"
+" \n"
#endif /* MINIMALISTIC */
" camcontrol help\n");
if (!printlong)
@@ -8966,6 +8971,7 @@ usage(int printlong)
"opcodes send the SCSI REPORT SUPPORTED OPCODES command\n"
"zone manage Zoned Block (Shingled) devices\n"
"epc send ATA Extended Power Conditions commands\n"
+"timestamp report or set the device's timestamp\n"
"help this message\n"
"Device Identifiers:\n"
"bus:target specify the bus and target, lun defaults to 0\n"
@@ -9157,6 +9163,17 @@ usage(int printlong)
"-s save mode (timer, state, restore)\n"
"-S power_src set power source: battery, nonbattery (source)\n"
"-T timer set timer, seconds, .1 sec resolution (timer)\n"
+"timestamp arguments:\n"
+"-r report the timestamp of the device\n"
+"-f format report the timestamp of the device with the given\n"
+" strftime(3) format string\n"
+"-m report the timestamp of the device as milliseconds since\n"
+" January 1st, 1970\n"
+"-U report the time with UTC instead of the local time zone\n"
+"-s set the timestamp of the device\n"
+"-f format the format of the time string passed into strptime(3)\n"
+"-T time the time value passed into strptime(3)\n"
+"-U set the timestamp of the device to UTC time\n"
);
#endif /* MINIMALISTIC */
}
@@ -9520,6 +9537,10 @@ main(int argc, char **argv)
error = epc(cam_dev, argc, argv, combinedopt,
retry_count, timeout, arglist & CAM_ARG_VERBOSE);
break;
+ case CAM_CMD_TIMESTAMP:
+ error = timestamp(cam_dev, argc, argv, combinedopt,
+ retry_count, timeout, arglist & CAM_ARG_VERBOSE);
+ break;
#endif /* MINIMALISTIC */
case CAM_CMD_USAGE:
usage(1);
diff --git a/sbin/camcontrol/camcontrol.h b/sbin/camcontrol/camcontrol.h
index 76b7f9b43f5d..a9a661e728a5 100644
--- a/sbin/camcontrol/camcontrol.h
+++ b/sbin/camcontrol/camcontrol.h
@@ -81,6 +81,9 @@ int zone(struct cam_device *device, int argc, char **argv, char *combinedopt,
int retry_count, int timeout, int verbosemode);
int epc(struct cam_device *device, int argc, char **argv, char *combinedopt,
int retry_count, int timeout, int verbosemode);
+int timestamp(struct cam_device *device, int argc, char **argv,
+ char *combinedopt, int retry_count, int timeout,
+ int verbosemode);
void mode_sense(struct cam_device *device, int mode_page, int page_control,
int dbd, int retry_count, int timeout, u_int8_t *data,
int datalen);
diff --git a/sbin/camcontrol/timestamp.c b/sbin/camcontrol/timestamp.c
new file mode 100644
index 000000000000..f452affd1818
--- /dev/null
+++ b/sbin/camcontrol/timestamp.c
@@ -0,0 +1,494 @@
+/*-
+ * Copyright (c) 2016 Spectra Logic Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions, and the following disclaimer,
+ * without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ * substantially similar to the "NO WARRANTY" disclaimer below
+ * ("Disclaimer") and any redistribution must be conditioned upon
+ * including a substantially similar Disclaimer requirement for further
+ * binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * Authors: Ken Merry (Spectra Logic Corporation)
+ * Reid Linnemann (Spectra Logic Corporation)
+ * Samuel Klopsch (Spectra Logic Corporation)
+ */
+/*
+ * SCSI tape drive timestamp support
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <err.h>
+#include <time.h>
+#include <locale.h>
+
+#include <cam/cam.h>
+#include <cam/cam_debug.h>
+#include <cam/cam_ccb.h>
+#include <cam/scsi/scsi_all.h>
+#include <cam/scsi/scsi_message.h>
+#include <camlib.h>
+#include "camcontrol.h"
+
+#define TIMESTAMP_REPORT 0
+#define TIMESTAMP_SET 1
+#define MIL "milliseconds"
+#define UTC "utc"
+
+static int set_restore_flags(struct cam_device *device, uint8_t *flags,
+ int set_flag, int retry_count, int timeout);
+static int report_timestamp(struct cam_device *device, uint64_t *ts,
+ int retry_count, int timeout);
+static int set_timestamp(struct cam_device *device, char *format_string,
+ char *timestamp_string,
+ int retry_count, int timeout);
+
+static int
+set_restore_flags(struct cam_device *device, uint8_t *flags, int set_flag,
+ int retry_count, int timeout)
+{
+ unsigned long blk_desc_length, hdr_and_blk_length;
+ int error = 0;
+ struct scsi_control_ext_page *control_page = NULL;
+ struct scsi_mode_header_10 *mode_hdr = NULL;
+ struct scsi_mode_sense_10 *cdb = NULL;
+ union ccb *ccb = NULL;
+ unsigned long mode_buf_size = sizeof(struct scsi_mode_header_10) +
+ sizeof(struct scsi_mode_blk_desc) +
+ sizeof(struct scsi_control_ext_page);
+ uint8_t mode_buf[mode_buf_size];
+
+ ccb = cam_getccb(device);
+ if (ccb == NULL) {
+ warnx("%s: error allocating CCB", __func__);
+ error = 1;
+ goto bailout;
+ }
+ /*
+ * Get the control extension subpage, we'll send it back modified to
+ * enable SCSI control over the tape drive's timestamp
+ */
+ scsi_mode_sense_len(&ccb->csio,
+ /*retries*/ retry_count,
+ /*cbfcnp*/ NULL,
+ /*tag_action*/ MSG_SIMPLE_Q_TAG,
+ /*dbd*/ 0,
+ /*page_control*/ SMS_PAGE_CTRL_CURRENT,
+ /*page*/ SCEP_PAGE_CODE,
+ /*param_buf*/ &mode_buf[0],
+ /*param_len*/ mode_buf_size,
+ /*minimum_cmd_size*/ 10,
+ /*sense_len*/ SSD_FULL_SIZE,
+ /*timeout*/ timeout ? timeout : 5000);
+ /*
+ * scsi_mode_sense_len does not have a subpage argument at the moment,
+ * so we have to manually set the subpage code before calling
+ * cam_send_ccb().
+ */
+ cdb = (struct scsi_mode_sense_10 *)ccb->csio.cdb_io.cdb_bytes;
+ cdb->subpage = SCEP_SUBPAGE_CODE;
+
+ ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
+ if (retry_count > 0)
+ ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
+
+ error = cam_send_ccb(device, ccb);
+ if (error != 0) {
+ warn("error sending Mode Sense");
+ goto bailout;
+ }
+
+ if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
+ cam_error_print(device, ccb, CAM_ESF_ALL,
+ CAM_EPF_ALL, stderr);
+ error = 1;
+ goto bailout;
+ }
+
+ mode_hdr = (struct scsi_mode_header_10 *)&mode_buf[0];
+ blk_desc_length = scsi_2btoul(mode_hdr->blk_desc_len);
+ hdr_and_blk_length = sizeof(struct scsi_mode_header_10)+blk_desc_length;
+ /*
+ * Create the control page at the correct point in the mode_buf, it
+ * starts after the header and the blk description.
+ */
+ control_page = (struct scsi_control_ext_page *)&mode_buf
+ [hdr_and_blk_length];
+ if (set_flag != 0) {
+ *flags = control_page->flags;
+ /*
+ * Set the SCSIP flag to enable SCSI to change the
+ * tape drive's timestamp.
+ */
+ control_page->flags |= SCEP_SCSIP;
+ } else {
+ control_page->flags = *flags;
+ }
+
+ scsi_mode_select_len(&ccb->csio,
+ /*retries*/ retry_count,
+ /*cbfcnp*/ NULL,
+ /*tag_action*/ MSG_SIMPLE_Q_TAG,
+ /*scsi_page_fmt*/ 1,
+ /*save_pages*/ 0,
+ /*param_buf*/ &mode_buf[0],
+ /*param_len*/ mode_buf_size,
+ /*minimum_cmd_size*/ 10,
+ /*sense_len*/ SSD_FULL_SIZE,
+ /*timeout*/ timeout ? timeout : 5000);
+
+ ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
+ if (retry_count > 0)
+ ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
+
+ error = cam_send_ccb(device, ccb);
+ if (error != 0) {
+ warn("error sending Mode Select");
+ goto bailout;
+ }
+
+ if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
+ cam_error_print(device, ccb, CAM_ESF_ALL,
+ CAM_EPF_ALL, stderr);
+ error = 1;
+ goto bailout;
+ }
+
+bailout:
+ if (ccb != NULL)
+ cam_freeccb(ccb);
+
+ return error;
+}
+
+static int
+report_timestamp(struct cam_device *device, uint64_t *ts,
+ int retry_count, int timeout)
+{
+ int error = 0;
+ struct scsi_report_timestamp_data *report_buf = malloc(
+ sizeof(struct scsi_report_timestamp_data));
+ uint8_t temp_timestamp[8];
+ uint32_t report_buf_size = sizeof(
+ struct scsi_report_timestamp_data);
+ union ccb *ccb = NULL;
+
+ ccb = cam_getccb(device);
+ if (ccb == NULL) {
+ warnx("%s: error allocating CCB", __func__);
+ error = 1;
+ goto bailout;
+ }
+
+ scsi_report_timestamp(&ccb->csio,
+ /*retries*/ retry_count,
+ /*cbfcnp*/ NULL,
+ /*tag_action*/ MSG_SIMPLE_Q_TAG,
+ /*pdf*/ 0,
+ /*buf*/ report_buf,
+ /*buf_len*/ report_buf_size,
+ /*sense_len*/ SSD_FULL_SIZE,
+ /*timeout*/ timeout ? timeout : 5000);
+
+ ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
+ if (retry_count > 0)
+ ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
+
+ error = cam_send_ccb(device, ccb);
+ if (error != 0) {
+ warn("error sending Report Timestamp");
+ goto bailout;
+ }
+ if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
+ cam_error_print(device, ccb, CAM_ESF_ALL,
+ CAM_EPF_ALL, stderr);
+ error = 1;
+ goto bailout;
+ }
+
+ bzero(temp_timestamp, sizeof(temp_timestamp));
+ memcpy(&temp_timestamp[2], &report_buf->timestamp, 6);
+
+ *ts = scsi_8btou64(temp_timestamp);
+
+bailout:
+ if (ccb != NULL)
+ cam_freeccb(ccb);
+
+ return error;
+}
+
+static int
+set_timestamp(struct cam_device *device, char *format_string,
+ char *timestamp_string, int retry_count,
+ int timeout)
+{
+ struct scsi_set_timestamp_parameters ts_p;
+ time_t time_value;
+ struct tm time_struct;
+ uint8_t flags = 0;
+ int error = 0;
+ uint64_t ts = 0;
+ union ccb *ccb = NULL;
+ int do_restore_flags = 0;
+
+ error = set_restore_flags(device, &flags, /*set_flag*/ 1, retry_count,
+ timeout);
+ if (error != 0)
+ goto bailout;
+
+ do_restore_flags = 1;
+
+ ccb = cam_getccb(device);
+ if (ccb == NULL) {
+ warnx("%s: error allocating CCB", __func__);
+ error = 1;
+ goto bailout;
+ }
+
+ if (strcmp(format_string, UTC) == 0) {
+ time(&time_value);
+ ts = (uint64_t) time_value;
+ } else {
+ bzero(&time_struct, sizeof(struct tm));
+ strptime(timestamp_string, format_string, &time_struct);
+ time_value = mktime(&time_struct);
+ ts = (uint64_t) time_value;
+ }
+ /* Convert time from seconds to milliseconds */
+ ts *= 1000;
+ scsi_create_timestamp(ts_p.timestamp, ts);
+
+ scsi_set_timestamp(&ccb->csio,
+ /*retries*/ retry_count,
+ /*cbfcnp*/ NULL,
+ /*tag_action*/ MSG_SIMPLE_Q_TAG,
+ /*buf*/ &ts_p,
+ /*buf_len*/ sizeof(ts_p),
+ /*sense_len*/ SSD_FULL_SIZE,
+ /*timeout*/ timeout ? timeout : 5000);
+
+ ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
+ if (retry_count > 0)
+ ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
+
+ error = cam_send_ccb(device, ccb);
+ if (error != 0) {
+ warn("error sending Set Timestamp");
+ goto bailout;
+ }
+
+ if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
+ cam_error_print(device, ccb, CAM_ESF_ALL,
+ CAM_EPF_ALL, stderr);
+ error = 1;
+ goto bailout;
+ }
+
+ printf("Timestamp set to %ju\n", (uintmax_t)ts);
+
+bailout:
+ if (do_restore_flags != 0)
+ error = set_restore_flags(device, &flags, /*set_flag*/ 0,
+ retry_count, timeout);
+ if (ccb != NULL)
+ cam_freeccb(ccb);
+
+ return error;
+}
+
+int
+timestamp(struct cam_device *device, int argc, char **argv, char *combinedopt,
+ int retry_count, int timeout, int verbosemode __unused)
+{
+ int c;
+ uint64_t ts;
+ char *format_string = NULL;
+ char *timestamp_string = NULL;
+ int action = -1;
+ int error = 0;
+ int single_arg = 0;
+ int do_utc = 0;
+
+ while ((c = getopt(argc, argv, combinedopt)) != -1) {
+ switch (c) {
+ case 'r': {
+ if (action != -1) {
+ warnx("Use only one -r or only one -s");
+ error =1;
+ goto bailout;
+ }
+ action = TIMESTAMP_REPORT;
+ break;
+ }
+ case 's': {
+ if (action != -1) {
+ warnx("Use only one -r or only one -s");
+ error = 1;
+ goto bailout;
+ }
+ action = TIMESTAMP_SET;
+ break;
+ }
+ case 'f': {
+ single_arg++;
+ format_string = strdup(optarg);
+ if (format_string == NULL) {
+ warn("Error allocating memory for format "
+ "argument");
+ error = 1;
+ goto bailout;
+ }
+ break;
+ }
+ case 'm': {
+ single_arg++;
+ format_string = strdup(MIL);
+ if (format_string == NULL) {
+ warn("Error allocating memory");
+ error = 1;
+ goto bailout;
+ }
+ break;
+ }
+ case 'U': {
+ do_utc = 1;
+ break;
+ }
+ case 'T':
+ timestamp_string = strdup(optarg);
+ if (timestamp_string == NULL) {
+ warn("Error allocating memory for format "
+ "argument");
+ error = 1;
+ goto bailout;
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (action == -1) {
+ warnx("Must specify an action, either -r or -s");
+ error = 1;
+ goto bailout;
+ }
+
+ if (single_arg > 1) {
+ warnx("Select only one: %s",
+ (action == TIMESTAMP_REPORT) ?
+ "-f format or -m for the -r flag" :
+ "-f format -T time or -U for the -s flag");
+ error = 1;
+ goto bailout;
+ }
+
+ if (action == TIMESTAMP_SET) {
+ if ((format_string == NULL)
+ && (do_utc == 0)) {
+ warnx("Must specify either -f format or -U for "
+ "setting the timestamp");
+ error = 1;
+ } else if ((format_string != NULL)
+ && (do_utc != 0)) {
+ warnx("Must specify only one of -f or -U to set "
+ "the timestamp");
+ error = 1;
+ } else if ((format_string != NULL)
+ && (strcmp(format_string, MIL) == 0)) {
+ warnx("-m is not allowed for setting the "
+ "timestamp");
+ error = 1;
+ } else if ((do_utc == 0)
+ && (timestamp_string == NULL)) {
+ warnx("Must specify the time (-T) to set as the "
+ "timestamp");
+ error = 1;
+ }
+ if (error != 0)
+ goto bailout;
+ } else if (action == TIMESTAMP_REPORT) {
+ if (format_string == NULL) {
+ format_string = strdup("%c %Z");
+ if (format_string == NULL) {
+ warn("Error allocating memory for format "
+ "string");
+ error = 1;
+ goto bailout;
+ }
+ }
+ }
+
+ if (action == TIMESTAMP_REPORT) {
+ error = report_timestamp(device, &ts, retry_count,
+ timeout);
+ if (error != 0) {
+ goto bailout;
+ } else if (strcmp(format_string, MIL) == 0) {
+ printf("Timestamp in milliseconds: %ju\n",
+ (uintmax_t)ts);
+ } else {
+ char temp_timestamp_string[100];
+ time_t time_var = ts / 1000;
+ const struct tm *restrict cur_time;
+
+ setlocale(LC_TIME, "");
+ if (do_utc != 0)
+ cur_time = gmtime(&time_var);
+ else
+ cur_time = localtime(&time_var);
+
+ strftime(temp_timestamp_string,
+ sizeof(temp_timestamp_string), format_string,
+ cur_time);
+ printf("Formatted timestamp: %s\n",
+ temp_timestamp_string);
+ }
+ } else if (action == TIMESTAMP_SET) {
+ if (do_utc != 0) {
+ format_string = strdup(UTC);
+ if (format_string == NULL) {
+ warn("Error allocating memory for format "
+ "string");
+ error = 1;
+ goto bailout;
+ }
+ }
+
+ error = set_timestamp(device, format_string, timestamp_string,
+ retry_count, timeout);
+ }
+
+bailout:
+ free(format_string);
+ free(timestamp_string);
+
+ return (error);
+}