summaryrefslogtreecommitdiff
path: root/src/factory-reset.c
blob: a0de7928fd137cf16b79ee2e8f8782274a32d48c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd.
 *
 * Contact: MyoungJune Park <mj2004.park@samsung.com>
 *   Created by Wonil Choi <wonil22.choi@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <linux/reboot.h>
#include <linux/limits.h>
#include <sys/reboot.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <time.h>
#include <getopt.h>
#include <dbus/dbus.h>
#include <glib.h>
#include <dbus/dbus-glib-lowlevel.h>

#define RESET_FLAG_FILE "/opt/.factoryreset"
static const char RESET_BACKUP_FILE[2][40] = {
	"/usr/system/RestoreDir/opt.tar.gz",
	"/usr/system/RestoreDir/opt.zip"
};

static const char *RUN_SCRIPT_FILE = "/usr/bin/run-factory-reset.sh";
static const char *LOG_FILE = "/opt/.factoryreset.log";
static const char *LOG_OLD = "/opt/.factoryreset.log.1";

static char *reset_flag;

void usage()
{
	printf("Options: \n");
	printf("  -b, --dbus      Start with dbus signal message\n");
	printf("\n");

	exit(0);
}

//extern int   csc_svc_config_set_pre_config_for_factory_reset( void );
/*==================================================================================
* CSC API Code (12/10/11 applied)
  - Include header : #include "csc-dispatch.h"
  - API : int   csc_svc_config_set_pre_config_for_factory_reset( void );
  - Return value : 1 (Success), 0 (Failure, 미리 저장해놓은 거래선 코드가 없을 경우 발생)
====================================================================================*/
int __system(const char *argv[])
{
	int status;
	pid_t cpid;

	cpid = fork();

	if (cpid == -1) {
		perror("fork");
		return -1;
	}

	if (cpid == 0) {
		execvp(argv[0], (char *const *)argv);
		_exit(-1);
	} else {
		if (waitpid(cpid, &status, 0) == -1) {
			perror("waitpid failed");
			return -1;
		}
		if (WIFSIGNALED(status)) {
			perror("exit by signal");
			return -1;
		}
		if (!WIFEXITED(status)) {
			perror("exit abnormally");
			return -1;
		}
		if (WIFEXITED(status) && WEXITSTATUS(status)) {
			perror("child return error");
			return -1;
		}
	}
	return 0;
}


#define RESET_DBUS_SERVICE	"org.tizen.factoryreset"
#define RESET_DBUS_INTERFACE	RESET_DBUS_SERVICE".start"

static const char granted_caller[6][16] = {
	"setting",
	"cscmgr",
	"wms",
	"bt", /* when the host device is changed */
	"omadmagent",
	"testmode"
};

static const char list_reset_flags[2][16] = {
	"ftrrstcp",
	"withoutcp"
};


static GMainLoop* loop;
static DBusConnection *bus_conn = NULL;
static const char *dbus_obj_path = "/org/tizen/factoryreset";

static void unregistered_func (DBusConnection *connection, void *user_data)
{
	perror("factory-reset dbus unregistered\n");
}

static DBusHandlerResult message_func (DBusConnection *connection,
		DBusMessage    *message,
		void           *user_data)
{
	/* store the name of factory reset caller. */
	char caller_name[NAME_MAX];
	FILE *fp = NULL;
	int i = 0;

	if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL) {
		perror("Not a dbus-signal\n");
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
	}

	if (strncmp(dbus_message_get_interface(message), RESET_DBUS_INTERFACE,
				strlen(RESET_DBUS_INTERFACE)) != 0) {
		printf("Not correct interface: \"%s\"\n",
				dbus_message_get_interface (message));
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
	}

	strncpy(caller_name, dbus_message_get_member(message),
			sizeof(caller_name));
	caller_name[NAME_MAX - 1] = '\0';

	while (strncmp(granted_caller[i], caller_name,
			strlen(granted_caller[i]))) {
		if ((++i) >= sizeof(granted_caller) / sizeof(granted_caller[0])) {
			printf("Not a granted caller, %s\n", caller_name);
			/* XXX Warning: Comparing granted caller list cannot
			 * grant good caller or not. Security is and should be
			 * protected by the SMACK rule. This code is only for
			 * preventing that a new unknown caller use factory
			 * reset without confirmation of the factory reset
			 * developer.
			 */
			return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
		}
	}
	fp = fopen(LOG_FILE, "a");
	if (fp != NULL) {
		fprintf(fp, "Requested by %s, %s\n", caller_name,
				dbus_message_get_sender(message));
		fclose(fp);
	}

	/* member name of dbus signal message may contains reset flags */
	for (i = 0; i < sizeof(list_reset_flags) / sizeof(list_reset_flags[0]);
			i++) {
		if (strstr(caller_name, list_reset_flags[i])) {
			snprintf(caller_name, NAME_MAX, "--%s",
					list_reset_flags[i]);
			reset_flag = strdup(caller_name);
			break;
		}
	}

	g_main_loop_quit(loop);
	return DBUS_HANDLER_RESULT_HANDLED;
}


static DBusHandlerResult filter_func (DBusConnection  *connection,
		DBusMessage *message, void *user_data)
{
	if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL,
				"Disconnected")) {
		/* Exit cleanly. */
		printf("Disconnected");
	}
	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static DBusObjectPathVTable dbus_vtable = {
	unregistered_func,
	message_func,
	NULL,
};

static int process_dbus(void)
{
	int ret;
	DBusError error;

	loop = g_main_loop_new (NULL, FALSE);

	dbus_error_init (&error);
	bus_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
	if (!bus_conn) {
		perror("failed to get dbus system bus");
		dbus_error_free(&error);
		return -1;
	}
	/* Add message filter to handle Disconnected. */
	dbus_connection_add_filter(bus_conn, (DBusHandleMessageFunction) filter_func,
			NULL, NULL);

	ret = dbus_bus_request_name(bus_conn, RESET_DBUS_SERVICE, 0, &error);

	/* Should be ret == -1, if error is set */
	if (ret == -1 && dbus_error_is_set(&error)) {
		dbus_error_free (&error);
		return -1;
	}

	if (!dbus_connection_register_object_path(bus_conn, dbus_obj_path,
				&dbus_vtable, NULL)) {
		return -1;
	}

	dbus_connection_setup_with_g_main(bus_conn, NULL);
	g_main_loop_run(loop);
	return 0;
}


int main(int argc, char **argv)
{
	FILE *fp;
	int ret, i = 0, c;
	const char *reset_cmd[] = {RUN_SCRIPT_FILE, NULL, NULL};
	time_t t = time(NULL);
	struct tm tm2 = *localtime(&t);

	static struct option long_options[] = {
		{"dbus", no_argument, NULL, 'b'},
		{0, 0, 0, 0}
	};

	ret = setuid(0);
	if (ret < 0) {
		perror("wrong permission");
		return -1;
	}

	/* If there is no factory-reset backup file, don't try to reset. */
	while (!(fp = fopen(RESET_BACKUP_FILE[i++], "r"))) {
		if (i >= sizeof(RESET_BACKUP_FILE) /
				sizeof(RESET_BACKUP_FILE[0])) {
			perror("backup file is not exist");
			return -1;
		}
	}
	if (fp != NULL) fclose(fp);

	if (rename(LOG_FILE, LOG_OLD) < 0)
		perror("can't rename the old log file");

	while (1) {

		int option_index = -1;
		c = getopt_long(argc, argv, "b", long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
		case 'b':

			if (process_dbus() < 0) {
				perror("dbus has some errors.");
				return -1;
			}
			break;

		default:
			usage();
			break;
		}
	}

	if (optind < argc)
		usage();

#if 0
	fp = fopen(RESET_FLAG_FILE, "w");
	if (fp == NULL) {
		perror("reset flg file open error");
		return -1;
	}
	fclose(fp);
#endif

	// Factory Reset Run
	reset_cmd[1] = reset_flag;
	__system(reset_cmd);

	// open log file (add local time)
	fp = fopen(LOG_FILE, "a");
	if (fp == NULL) {
		perror("reset log file open error");
		return -2;
	}

	// write finish log file
	fprintf(fp, "End Factory Reset\n%d-%02d-%02d %02d:%02d:%02d\n",
			tm2.tm_year + 1900, tm2.tm_mon + 1, tm2.tm_mday,
			tm2.tm_hour, tm2.tm_min, tm2.tm_sec);
	fclose(fp);

	// remove reset flag file, sync and reboot
#if 0
	unlink(RESET_FLAG_FILE);
#endif
	sync();

#ifdef DEBUG_BINARY
	reboot(LINUX_REBOOT_CMD_RESTART);
#else
	const char *reboot_cmd[] = {"/usr/sbin/reboot", "debug0x4f4c", NULL};
	__system(reboot_cmd);
#endif

	return 0;
}