blob: a9ab47ebec359c47ab5b6c02878ab5d124835320 [file] [log] [blame]
Szymon Starzyckib6c5f282013-07-24 17:08:04 -07001/*
2 * Copyright (c) 2009-2013, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google, Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
Elliott Hughes0badbd62014-12-29 12:24:25 -080031#include <errno.h>
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070032#include <sys/types.h>
33#include <sys/stat.h>
34#include <unistd.h>
35#include <stdio.h>
36#include <sys/ioctl.h>
37#include <linux/fs.h>
Szymon Starzycki2a656c32013-09-05 14:26:28 -070038#include <stdlib.h>
Szymon Starzyckibc849f12013-09-13 15:37:08 -070039#include <cutils/properties.h>
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070040
41#include "utils.h"
42#include "debug.h"
43
44#ifndef BLKDISCARD
45#define BLKDISCARD _IO(0x12,119)
46#endif
47
48#ifndef BLKSECDISCARD
49#define BLKSECDISCARD _IO(0x12,125)
50#endif
51
Szymon Starzycki2a656c32013-09-05 14:26:28 -070052#define READ_BUF_SIZE (16*1024)
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070053
54int get_stream_size(FILE *stream) {
55 int size;
56 fseek(stream, 0, SEEK_END);
57 size = ftell(stream);
58 fseek(stream, 0, SEEK_SET);
59 return size;
60}
61
62uint64_t get_block_device_size(int fd)
63{
64 uint64_t size = 0;
65 int ret;
66
67 ret = ioctl(fd, BLKGETSIZE64, &size);
68
69 if (ret)
70 return 0;
71
72 return size;
73}
74
75uint64_t get_file_size(int fd)
76{
77 struct stat buf;
78 int ret;
79 int64_t computed_size;
80
81 ret = fstat(fd, &buf);
82 if (ret)
83 return 0;
84
85 if (S_ISREG(buf.st_mode))
86 computed_size = buf.st_size;
87 else if (S_ISBLK(buf.st_mode))
88 computed_size = get_block_device_size(fd);
89 else
90 computed_size = 0;
91
92 return computed_size;
93}
94
95uint64_t get_file_size64(int fd)
96{
97 struct stat64 buf;
98 int ret;
99 uint64_t computed_size;
100
101 ret = fstat64(fd, &buf);
102 if (ret)
103 return 0;
104
105 if (S_ISREG(buf.st_mode))
106 computed_size = buf.st_size;
107 else if (S_ISBLK(buf.st_mode))
108 computed_size = get_block_device_size(fd);
109 else
110 computed_size = 0;
111
112 return computed_size;
113}
114
115
116char *strip(char *str)
117{
118 int n;
119
120 n = strspn(str, " \t");
121 str += n;
122 n = strcspn(str, " \t");
123 str[n] = '\0';
124
125 return str;
126}
127
128int wipe_block_device(int fd, int64_t len)
129{
130 uint64_t range[2];
131 int ret;
132
133 range[0] = 0;
134 range[1] = len;
135 ret = ioctl(fd, BLKSECDISCARD, &range);
136 if (ret < 0) {
137 range[0] = 0;
138 range[1] = len;
139 ret = ioctl(fd, BLKDISCARD, &range);
140 if (ret < 0) {
141 D(WARN, "Discard failed\n");
142 return 1;
143 } else {
144 D(WARN, "Wipe via secure discard failed, used discard instead\n");
145 return 0;
146 }
147 }
148
149 return 0;
150}
151
Szymon Starzycki2a656c32013-09-05 14:26:28 -0700152int create_temp_file() {
153 char tempname[] = "/dev/fastboot_data_XXXXXX";
154 int fd;
155
156 fd = mkstemp(tempname);
157 if (fd < 0)
158 return -1;
159
160 unlink(tempname);
161
162 return fd;
163}
164
165ssize_t bulk_write(int bulk_in, const char *buf, size_t length)
166{
167 size_t count = 0;
168 ssize_t ret;
169
170 do {
171 ret = TEMP_FAILURE_RETRY(write(bulk_in, buf + count, length - count));
172 if (ret < 0) {
Elliott Hughes4f241252014-01-14 16:17:08 -0800173 D(WARN, "[ bulk_write failed fd=%d length=%zu errno=%d %s ]",
Szymon Starzycki2a656c32013-09-05 14:26:28 -0700174 bulk_in, length, errno, strerror(errno));
175 return -1;
176 } else {
177 count += ret;
178 }
179 } while (count < length);
180
181 D(VERBOSE, "[ bulk_write done fd=%d ]", bulk_in);
182 return count;
183}
184
185ssize_t bulk_read(int bulk_out, char *buf, size_t length)
186{
187 ssize_t ret;
188 size_t n = 0;
189
190 while (n < length) {
191 size_t to_read = (length - n > READ_BUF_SIZE) ? READ_BUF_SIZE : length - n;
192 ret = TEMP_FAILURE_RETRY(read(bulk_out, buf + n, to_read));
193 if (ret < 0) {
Elliott Hughes4f241252014-01-14 16:17:08 -0800194 D(WARN, "[ bulk_read failed fd=%d length=%zu errno=%d %s ]",
Szymon Starzycki2a656c32013-09-05 14:26:28 -0700195 bulk_out, length, errno, strerror(errno));
196 return ret;
197 }
198 n += ret;
199 if (ret < (ssize_t)to_read) {
Elliott Hughes4f241252014-01-14 16:17:08 -0800200 D(VERBOSE, "bulk_read short read, ret=%zd to_read=%zu n=%zu length=%zu",
Szymon Starzycki2a656c32013-09-05 14:26:28 -0700201 ret, to_read, n, length);
202 break;
203 }
204 }
205
206 return n;
207}
208
Szymon Starzyckibc849f12013-09-13 15:37:08 -0700209#define NAP_TIME 200 // 200 ms between polls
210static int wait_for_property(const char *name, const char *desired_value, int maxwait)
211{
212 char value[PROPERTY_VALUE_MAX] = {'\0'};
213 int maxnaps = (maxwait * 1000) / NAP_TIME;
214
215 if (maxnaps < 1) {
216 maxnaps = 1;
217 }
218
219 while (maxnaps-- > 0) {
220 usleep(NAP_TIME * 1000);
221 if (property_get(name, value, NULL)) {
222 if (desired_value == NULL || strcmp(value, desired_value) == 0) {
223 return 0;
224 }
225 }
226 }
227 return -1; /* failure */
228}
229
230int service_start(const char *service_name)
231{
232 int result = 0;
233 char property_value[PROPERTY_VALUE_MAX];
234
235 property_get(service_name, property_value, "");
236 if (strcmp("running", property_value) != 0) {
Szymon Starzycki27ea99f2013-09-18 16:12:43 -0700237 D(INFO, "Starting %s", service_name);
Szymon Starzyckibc849f12013-09-13 15:37:08 -0700238 property_set("ctl.start", service_name);
239 if (wait_for_property(service_name, "running", 5))
240 result = -1;
241 }
242
243 return result;
244}
245
246int service_stop(const char *service_name)
247{
248 int result = 0;
249
250 D(INFO, "Stopping MDNSD");
251 property_set("ctl.stop", service_name);
252 if (wait_for_property(service_name, "stopped", 5))
253 result = -1;
254
255 return result;
256}
257
Szymon Starzycki27ea99f2013-09-18 16:12:43 -0700258int ssh_server_start()
259{
260 return service_start("sshd");
261}