From 47a91ddcc2c366e54c2517b1831106997e1bcaff Mon Sep 17 00:00:00 2001 From: Sebastian Sura Date: Mon, 25 Mar 2024 15:38:58 +0100 Subject: [PATCH] mac: fix accessing global variables in callback --- core/src/stored/mac.cc | 73 ++++++++++++++++++++++----------------- core/src/stored/record.cc | 6 +--- core/src/stored/record.h | 22 ++++-------- 3 files changed, 50 insertions(+), 51 deletions(-) diff --git a/core/src/stored/mac.cc b/core/src/stored/mac.cc index bda1ef17f0c..4270301dc01 100644 --- a/core/src/stored/mac.cc +++ b/core/src/stored/mac.cc @@ -65,9 +65,14 @@ static char ReplicateData[] = "replicate data %d\n"; static char end_replicate[] = "end replicate\n"; -/* get information from first original SOS label for our job */ -static bool found_first_sos_label = false; - +/* last callback information of our job */ +struct cb_data { + bool found_first_sos_label{false}; + uint32_t last_VolSessionId{0}; + uint32_t last_VolSessionTime{0}; + int32_t last_FileIndex{0}; + int32_t last_Stream{0}; +}; /** * Get response from Storage daemon to a command we sent. @@ -109,7 +114,9 @@ static bool response(JobControlRecord* jcr, * Returns: true if OK * false if error */ -static bool CloneRecordInternally(DeviceControlRecord* dcr, DeviceRecord* rec) +static bool CloneRecordInternally(DeviceControlRecord* dcr, + DeviceRecord* rec, + cb_data* data) { bool retval = false; bool translated_record = false; @@ -127,9 +134,9 @@ static bool CloneRecordInternally(DeviceControlRecord* dcr, DeviceRecord* rec) if (rec->FileIndex < 0) { if (rec->FileIndex == SOS_LABEL) { - if (!found_first_sos_label) { + if (!data->found_first_sos_label) { Dmsg0(200, "Found first SOS_LABEL and adopting job info\n"); - found_first_sos_label = true; + data->found_first_sos_label = true; Session_Label sos_label; @@ -177,13 +184,13 @@ static bool CloneRecordInternally(DeviceControlRecord* dcr, DeviceRecord* rec) * JobFiles, which we then use as the output FileIndex. */ if (rec->FileIndex >= 0) { // If something changed, increment FileIndex - if (rec->VolSessionId != rec->last_VolSessionId - || rec->VolSessionTime != rec->last_VolSessionTime - || rec->FileIndex != rec->last_FileIndex) { + if (rec->VolSessionId != data->last_VolSessionId + || rec->VolSessionTime != data->last_VolSessionTime + || rec->FileIndex != data->last_FileIndex) { jcr->JobFiles++; - rec->last_VolSessionId = rec->VolSessionId; - rec->last_VolSessionTime = rec->VolSessionTime; - rec->last_FileIndex = rec->FileIndex; + data->last_VolSessionId = rec->VolSessionId; + data->last_VolSessionTime = rec->VolSessionTime; + data->last_FileIndex = rec->FileIndex; } rec->FileIndex = jcr->JobFiles; /* set sequential output FileIndex */ } @@ -256,8 +263,8 @@ static bool CloneRecordInternally(DeviceControlRecord* dcr, DeviceRecord* rec) /* Restore packet -- the read record function uses this information * to check if the job changed, so we need to restore it to how it was; * otherwise the record will get zeroed on job change. */ - rec->VolSessionId = rec->last_VolSessionId; - rec->VolSessionTime = rec->last_VolSessionTime; + rec->VolSessionId = data->last_VolSessionId; + rec->VolSessionTime = data->last_VolSessionTime; if (translated_record) { FreeRecord(jcr->sd_impl->dcr->after_rec); @@ -275,7 +282,9 @@ static bool CloneRecordInternally(DeviceControlRecord* dcr, DeviceRecord* rec) * Returns: true if OK * false if error */ -static bool CloneRecordToRemoteSd(DeviceControlRecord* dcr, DeviceRecord* rec) +static bool CloneRecordToRemoteSd(DeviceControlRecord* dcr, + DeviceRecord* rec, + cb_data* data) { POOLMEM* msgsave; JobControlRecord* jcr = dcr->jcr; @@ -287,13 +296,13 @@ static bool CloneRecordToRemoteSd(DeviceControlRecord* dcr, DeviceRecord* rec) if (rec->FileIndex < 0) { return true; } // See if this is the first record being processed. - if (rec->last_FileIndex == 0) { + if (data->last_FileIndex == 0) { /* Initialize the last counters so we can compare * things in the next run through here. */ - rec->last_VolSessionId = rec->VolSessionId; - rec->last_VolSessionTime = rec->VolSessionTime; - rec->last_FileIndex = rec->FileIndex; - rec->last_Stream = rec->Stream; + data->last_VolSessionId = rec->VolSessionId; + data->last_VolSessionTime = rec->VolSessionTime; + data->last_FileIndex = rec->FileIndex; + data->last_Stream = rec->Stream; jcr->JobFiles = 1; // Need to send both a new header only. @@ -301,19 +310,19 @@ static bool CloneRecordToRemoteSd(DeviceControlRecord* dcr, DeviceRecord* rec) send_header = true; } else { // See if we are changing file or stream type. - if (rec->VolSessionId != rec->last_VolSessionId - || rec->VolSessionTime != rec->last_VolSessionTime - || rec->FileIndex != rec->last_FileIndex - || rec->Stream != rec->last_Stream) { + if (rec->VolSessionId != data->last_VolSessionId + || rec->VolSessionTime != data->last_VolSessionTime + || rec->FileIndex != data->last_FileIndex + || rec->Stream != data->last_Stream) { /* See if we are changing the FileIndex e.g. * start processing the next file in the backup stream. */ - if (rec->FileIndex != rec->last_FileIndex) { jcr->JobFiles++; } + if (rec->FileIndex != data->last_FileIndex) { jcr->JobFiles++; } // Keep track of the new state. - rec->last_VolSessionId = rec->VolSessionId; - rec->last_VolSessionTime = rec->VolSessionTime; - rec->last_FileIndex = rec->FileIndex; - rec->last_Stream = rec->Stream; + data->last_VolSessionId = rec->VolSessionId; + data->last_VolSessionTime = rec->VolSessionTime; + data->last_FileIndex = rec->FileIndex; + data->last_Stream = rec->Stream; // Need to send both a EOD and a new header. send_eod = true; @@ -545,9 +554,10 @@ bool DoMacRun(JobControlRecord* jcr) now = (utime_t)time(NULL); UpdateJobStatistics(jcr, now); + cb_data data{}; // Read all data and send it to remote SD. ok = ReadRecords(jcr->sd_impl->read_dcr, CloneRecordToRemoteSd, - MountNextReadVolume); + MountNextReadVolume, &data); /* Send the last EOD to close the last data transfer and a next EOD to * signal the remote we are done. */ @@ -641,9 +651,10 @@ bool DoMacRun(JobControlRecord* jcr) SetStartVolPosition(jcr->sd_impl->dcr); jcr->JobFiles = 0; + cb_data data{}; // Read all data and make a local clone of it. ok = ReadRecords(jcr->sd_impl->read_dcr, CloneRecordInternally, - MountNextReadVolume); + MountNextReadVolume, &data); } bail_out: diff --git a/core/src/stored/record.cc b/core/src/stored/record.cc index 7560e4dee5d..25b17d9f806 100644 --- a/core/src/stored/record.cc +++ b/core/src/stored/record.cc @@ -2,7 +2,7 @@ BAREOSĀ® - Backup Archiving REcovery Open Sourced Copyright (C) 2001-2012 Free Software Foundation Europe e.V. - Copyright (C) 2016-2023 Bareos GmbH & Co. KG + Copyright (C) 2016-2024 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -519,10 +519,6 @@ void DumpRecord(const char* tag, const DeviceRecord* rec) Dmsg2(100, "%-14s %p\n", "bsr", rec->bsr); Dmsg2(100, "%-14s %p\n", "data", rec->data); Dmsg2(100, "%-14s %d\n", "match_stat", rec->match_stat); - Dmsg2(100, "%-14s %u\n", "last_VolSessionId", rec->last_VolSessionId); - Dmsg2(100, "%-14s %u\n", "last_VolSessionTime", rec->last_VolSessionTime); - Dmsg2(100, "%-14s %d\n", "last_FileIndex", rec->last_FileIndex); - Dmsg2(100, "%-14s %d\n", "last_Stream", rec->last_Stream); Dmsg2(100, "%-14s %s\n", "own_mempool", rec->own_mempool ? "true" : "false"); } diff --git a/core/src/stored/record.h b/core/src/stored/record.h index 7fc0d9b8786..3fa4ced941f 100644 --- a/core/src/stored/record.h +++ b/core/src/stored/record.h @@ -3,7 +3,7 @@ Copyright (C) 2000-2012 Free Software Foundation Europe e.V. Copyright (C) 2011-2012 Planets Communications B.V. - Copyright (C) 2013-2021 Bareos GmbH & Co. KG + Copyright (C) 2013-2024 Bareos GmbH & Co. KG This program is Free Software; you can redistribute it and/or modify it under the terms of version three of the GNU Affero General Public @@ -121,12 +121,8 @@ struct DeviceRecord { char state_bits[REC_STATE_BYTES]{}; /**< State bits */ rec_state state{st_none}; /**< State of WriteRecordToBlock */ BootStrapRecord* bsr{nullptr}; /**< Pointer to bsr that matched */ - POOLMEM* data{nullptr}; /**< Record data. This MUST be a memory pool item */ - int32_t match_stat{0}; /**< BootStrapRecord match status */ - uint32_t last_VolSessionId{0}; /**< Used in sequencing FI for Vbackup */ - uint32_t last_VolSessionTime{0}; - int32_t last_FileIndex{0}; - int32_t last_Stream{0}; /**< Used in SD-SD replication */ + POOLMEM* data{nullptr}; /**< Record data. This MUST be a memory pool item */ + int32_t match_stat{0}; /**< BootStrapRecord match status */ bool own_mempool{false}; /**< Do we own the POOLMEM pointed to in data ? */ }; @@ -150,17 +146,13 @@ struct DeviceRecord { * ser_volume_label() and UnserVolumeLabel() and is slightly different. */ struct Volume_Label { - /* - * The first items in this structure are saved + /* The first items in this structure are saved * in the Device buffer, but are not actually written - * to the tape. - */ + * to the tape. */ int32_t LabelType{}; /**< This is written in header only */ uint32_t LabelSize{}; /**< length of serialized label */ - /* - * The items below this line are stored on - * the tape - */ + /* The items below this line are stored on + * the tape */ char Id[32]{}; /**< Bareos Immortal ... */ uint32_t VerNum{}; /**< Label version number */