1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz> 4f08c3bdfSopenharmony_ci */ 5f08c3bdfSopenharmony_ci 6f08c3bdfSopenharmony_ci#include <errno.h> 7f08c3bdfSopenharmony_ci#include <limits.h> 8f08c3bdfSopenharmony_ci#include <stdio.h> 9f08c3bdfSopenharmony_ci#include <stdlib.h> 10f08c3bdfSopenharmony_ci#define TST_NO_DEFAULT_MAIN 11f08c3bdfSopenharmony_ci#include "tst_test.h" 12f08c3bdfSopenharmony_ci#include "old/old_device.h" 13f08c3bdfSopenharmony_ci 14f08c3bdfSopenharmony_ciextern struct tst_test *tst_test; 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_cistatic struct tst_test test = { 17f08c3bdfSopenharmony_ci}; 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_cistatic void print_help(void) 20f08c3bdfSopenharmony_ci{ 21f08c3bdfSopenharmony_ci fprintf(stderr, "\nUsage:\n"); 22f08c3bdfSopenharmony_ci fprintf(stderr, "tst_device acquire [size [filename]]\n"); 23f08c3bdfSopenharmony_ci fprintf(stderr, "tst_device release /path/to/device\n"); 24f08c3bdfSopenharmony_ci fprintf(stderr, "tst_device clear /path/to/device\n\n"); 25f08c3bdfSopenharmony_ci} 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_cistatic int acquire_device(int argc, char *argv[]) 28f08c3bdfSopenharmony_ci{ 29f08c3bdfSopenharmony_ci unsigned int size = 0; 30f08c3bdfSopenharmony_ci const char *device; 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_ci if (argc > 4) 33f08c3bdfSopenharmony_ci return 1; 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_ci if (argc >= 3) { 36f08c3bdfSopenharmony_ci size = atoi(argv[2]); 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_ci if (!size) { 39f08c3bdfSopenharmony_ci fprintf(stderr, "ERROR: Invalid device size '%s'", 40f08c3bdfSopenharmony_ci argv[2]); 41f08c3bdfSopenharmony_ci return 1; 42f08c3bdfSopenharmony_ci } 43f08c3bdfSopenharmony_ci } 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci if (argc >= 4) 46f08c3bdfSopenharmony_ci device = tst_acquire_loop_device(size, argv[3]); 47f08c3bdfSopenharmony_ci else 48f08c3bdfSopenharmony_ci device = tst_acquire_device__(size); 49f08c3bdfSopenharmony_ci 50f08c3bdfSopenharmony_ci if (!device) 51f08c3bdfSopenharmony_ci return 1; 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_ci if (tst_clear_device(device)) { 54f08c3bdfSopenharmony_ci tst_release_device(device); 55f08c3bdfSopenharmony_ci return 1; 56f08c3bdfSopenharmony_ci } 57f08c3bdfSopenharmony_ci 58f08c3bdfSopenharmony_ci printf("%s", device); 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_ci return 0; 61f08c3bdfSopenharmony_ci} 62f08c3bdfSopenharmony_ci 63f08c3bdfSopenharmony_cistatic int release_device(int argc, char *argv[]) 64f08c3bdfSopenharmony_ci{ 65f08c3bdfSopenharmony_ci if (argc != 3) 66f08c3bdfSopenharmony_ci return 1; 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci /* 69f08c3bdfSopenharmony_ci * tst_acquire_[loop_]device() was called in a different process. 70f08c3bdfSopenharmony_ci * tst_release_device() would think that no device was acquired yet 71f08c3bdfSopenharmony_ci * and do nothing. Call tst_detach_device() directly to bypass 72f08c3bdfSopenharmony_ci * the check. 73f08c3bdfSopenharmony_ci */ 74f08c3bdfSopenharmony_ci return tst_detach_device(argv[2]); 75f08c3bdfSopenharmony_ci} 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_cistatic int clear_device(int argc, char *argv[]) 78f08c3bdfSopenharmony_ci{ 79f08c3bdfSopenharmony_ci if (argc != 3) 80f08c3bdfSopenharmony_ci return 1; 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci if (tst_clear_device(argv[2])) 83f08c3bdfSopenharmony_ci return 1; 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_ci return 0; 86f08c3bdfSopenharmony_ci} 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_ciint main(int argc, char *argv[]) 89f08c3bdfSopenharmony_ci{ 90f08c3bdfSopenharmony_ci /* 91f08c3bdfSopenharmony_ci * Force messages to be printed from the new library i.e. tst_test.c 92f08c3bdfSopenharmony_ci * 93f08c3bdfSopenharmony_ci * The new library prints messages into stderr while the old one prints 94f08c3bdfSopenharmony_ci * them into stdout. When messages are printed into stderr we can 95f08c3bdfSopenharmony_ci * safely do: 96f08c3bdfSopenharmony_ci * 97f08c3bdfSopenharmony_ci * DEV=$(tst_device acquire) 98f08c3bdfSopenharmony_ci */ 99f08c3bdfSopenharmony_ci tst_test = &test; 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_ci if (argc < 2) 102f08c3bdfSopenharmony_ci goto help; 103f08c3bdfSopenharmony_ci 104f08c3bdfSopenharmony_ci if (!strcmp(argv[1], "acquire")) { 105f08c3bdfSopenharmony_ci if (acquire_device(argc, argv)) 106f08c3bdfSopenharmony_ci goto help; 107f08c3bdfSopenharmony_ci } else if (!strcmp(argv[1], "release")) { 108f08c3bdfSopenharmony_ci if (release_device(argc, argv)) 109f08c3bdfSopenharmony_ci goto help; 110f08c3bdfSopenharmony_ci } else if (!strcmp(argv[1], "clear")) { 111f08c3bdfSopenharmony_ci if (clear_device(argc, argv)) 112f08c3bdfSopenharmony_ci goto help; 113f08c3bdfSopenharmony_ci } else { 114f08c3bdfSopenharmony_ci fprintf(stderr, "ERROR: Invalid COMMAND '%s'\n", argv[1]); 115f08c3bdfSopenharmony_ci goto help; 116f08c3bdfSopenharmony_ci } 117f08c3bdfSopenharmony_ci 118f08c3bdfSopenharmony_ci return 0; 119f08c3bdfSopenharmony_cihelp: 120f08c3bdfSopenharmony_ci print_help(); 121f08c3bdfSopenharmony_ci return 1; 122f08c3bdfSopenharmony_ci} 123