|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 Intel Corporation |
| 3 | + * SPDX-License-Identifier: BSD-3-Clause |
| 4 | + */ |
| 5 | + |
| 6 | +/* |
| 7 | + * Copyright (C) 2018 Intel Corporation |
| 8 | + * |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +#include <openssl/sha.h> |
| 23 | +#include <errno.h> |
| 24 | +#include <stdio.h> |
| 25 | +#include <string.h> |
| 26 | +#include <stdlib.h> |
| 27 | +#include "property.h" |
| 28 | +#include "log_sys.h" |
| 29 | +#include "fsutils.h" |
| 30 | + |
| 31 | +#define MACHINE_ID "/etc/machine-id" |
| 32 | +#define OS_VERSION "/usr/lib/os-release" |
| 33 | +#define OS_VERSION_KEY "VERSION_ID=" |
| 34 | +#define DEVICE_ID_UNKNOWN "UnknownId" |
| 35 | +#define LOG_UUID "uuid.txt" |
| 36 | +#define LOG_BUILDID "buildid.txt" |
| 37 | + |
| 38 | +static void get_device_id(struct sender_t *sender) |
| 39 | +{ |
| 40 | + int ret; |
| 41 | + char *loguuid; |
| 42 | + |
| 43 | + |
| 44 | + ret = asprintf(&loguuid, "%s/%s", sender->outdir, LOG_UUID); |
| 45 | + if (ret < 0) { |
| 46 | + LOGE("compute string failed, out of memory\n"); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + ret = file_read_string(MACHINE_ID, guuid, VERSION_SIZE); |
| 51 | + if (ret <= 0) |
| 52 | + LOGE("Could not get mmc id: %d (%s)\n", |
| 53 | + ret, strerror(-ret)); |
| 54 | + else |
| 55 | + goto write; |
| 56 | + |
| 57 | + LOGE("Could not find DeviceId, set it to '%s'\n", |
| 58 | + DEVICE_ID_UNKNOWN); |
| 59 | + strncpy(guuid, DEVICE_ID_UNKNOWN, strlen(DEVICE_ID_UNKNOWN)); |
| 60 | + |
| 61 | +write: |
| 62 | + overwrite_file(loguuid, guuid); |
| 63 | + free(loguuid); |
| 64 | +} |
| 65 | + |
| 66 | +static int get_buildversion(struct sender_t *sender) |
| 67 | +{ |
| 68 | + int ret; |
| 69 | + char lastbuild[VERSION_SIZE]; |
| 70 | + char *logbuildid; |
| 71 | + char *currentbuild = gbuildversion; |
| 72 | + |
| 73 | + ret = file_read_key_value(OS_VERSION, OS_VERSION_KEY, gbuildversion); |
| 74 | + if (ret <= 0) { |
| 75 | + LOGE("failed to get version from %s, error (%s)\n", |
| 76 | + OS_VERSION, strerror(-ret)); |
| 77 | + return ret; |
| 78 | + } |
| 79 | + |
| 80 | + ret = asprintf(&logbuildid, "%s/%s", sender->outdir, LOG_BUILDID); |
| 81 | + if (ret < 0) { |
| 82 | + LOGE("compute string failed, out of memory\n"); |
| 83 | + return ret; |
| 84 | + } |
| 85 | + |
| 86 | + ret = file_read_string(logbuildid, lastbuild, VERSION_SIZE); |
| 87 | + if (ret == -ENOENT || |
| 88 | + (ret > 0 && strcmp(currentbuild, lastbuild))) { |
| 89 | + /* build changed or file not found, overwrite it */ |
| 90 | + ret = overwrite_file(logbuildid, gbuildversion); |
| 91 | + if (ret) { |
| 92 | + LOGE("create (%s) failed, error (%s)\n", logbuildid, |
| 93 | + strerror(-ret)); |
| 94 | + goto free; |
| 95 | + } |
| 96 | + |
| 97 | + sender->sw_updated = 1; |
| 98 | + ret = 0; |
| 99 | + } else if (ret <= 0) { |
| 100 | + LOGE("Cannot read %s, error (%s)\n", |
| 101 | + logbuildid, strerror(errno)); |
| 102 | + } else { |
| 103 | + /* buildid is the same */ |
| 104 | + sender->sw_updated = 0; |
| 105 | + ret = 0; |
| 106 | + } |
| 107 | +free: |
| 108 | + free(logbuildid); |
| 109 | + return ret; |
| 110 | +} |
| 111 | + |
| 112 | +int swupdated(struct sender_t *sender) |
| 113 | +{ |
| 114 | + return sender->sw_updated; |
| 115 | +} |
| 116 | + |
| 117 | +int init_properties(struct sender_t *sender) |
| 118 | +{ |
| 119 | + int ret; |
| 120 | + |
| 121 | + ret = get_buildversion(sender); |
| 122 | + if (ret) { |
| 123 | + LOGE("init properties failed\n"); |
| 124 | + return ret; |
| 125 | + } |
| 126 | + get_device_id(sender); |
| 127 | + return 0; |
| 128 | +} |
0 commit comments