13899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross/* 23899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross * Copyright (C) 2010 The Android Open Source Project 33899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross * 43899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross * Licensed under the Apache License, Version 2.0 (the "License"); 53899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross * you may not use this file except in compliance with the License. 63899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross * You may obtain a copy of the License at 73899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross * 83899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross * http://www.apache.org/licenses/LICENSE-2.0 93899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross * 103899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross * Unless required by applicable law or agreed to in writing, software 113899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross * distributed under the License is distributed on an "AS IS" BASIS, 123899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross * See the License for the specific language governing permissions and 143899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross * limitations under the License. 153899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross */ 163899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross 173899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross#ifndef _INIT_UTIL_H_ 183899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross#define _INIT_UTIL_H_ 193899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross 20f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross#include <sys/stat.h> 21f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross#include <sys/types.h> 22f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 23f682b4786a4093efb23bf80d69bf80eb274b145bElliott Hughes#include <string> 24f667a3247a7e814355feedbc08c6bbc92a9409b5Nick Kralevich#include <functional> 25f682b4786a4093efb23bf80d69bf80eb274b145bElliott Hughes 2644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) 2744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross 28a016c42b4f7122625a417929afa0e9379ffa4887Andreas Gampe#define COLDBOOT_DONE "/dev/.coldboot_done" 29f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross 303899e9fc01cf608f19f716749c54cc5c4d17e766Colin Crossint mtd_name_to_number(const char *name); 313899e9fc01cf608f19f716749c54cc5c4d17e766Colin Crossint create_socket(const char *name, int type, mode_t perm, 328348d279c7ce1a2453965ba7f05a7b818d58886cStephen Smalley uid_t uid, gid_t gid, const char *socketcon); 33f682b4786a4093efb23bf80d69bf80eb274b145bElliott Hughes 34f682b4786a4093efb23bf80d69bf80eb274b145bElliott Hughesbool read_file(const char* path, std::string* content); 35f682b4786a4093efb23bf80d69bf80eb274b145bElliott Hughesint write_file(const char* path, const char* content); 36f682b4786a4093efb23bf80d69bf80eb274b145bElliott Hughes 37da40c00137f75543a69972f1be506e2d14a41845Elliott Hughestime_t gettime(); 38da40c00137f75543a69972f1be506e2d14a41845Elliott Hughesuint64_t gettime_ns(); 39da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes 40da40c00137f75543a69972f1be506e2d14a41845Elliott Hughesclass Timer { 41da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes public: 42da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes Timer() : t0(gettime_ns()) { 43da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes } 44da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes 45da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes double duration() { 46da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes return static_cast<double>(gettime_ns() - t0) / 1000000000.0; 47da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes } 48da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes 49da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes private: 50da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes uint64_t t0; 51da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes}; 52da40c00137f75543a69972f1be506e2d14a41845Elliott Hughes 533899e9fc01cf608f19f716749c54cc5c4d17e766Colin Crossunsigned int decode_uid(const char *s); 543899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross 55b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Crossint mkdir_recursive(const char *pathname, mode_t mode); 56b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Crossvoid sanitize(char *p); 5779f338465213885900cea5a39f3aeeea083bbe51Chris Friesvoid make_link_init(const char *oldpath, const char *newpath); 58b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Crossvoid remove_link(const char *oldpath, const char *newpath); 59cd0f173e2790ee068fd2a20bcfc6c20468e97e51Colin Crossint wait_for_file(const char *filename, int timeout); 60f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Crossvoid open_devnull_stdio(void); 61e5ce30fed81d1918a259be092dcd8bfffc3c2649Elliott Hughesvoid import_kernel_cmdline(bool in_qemu, 62e5ce30fed81d1918a259be092dcd8bfffc3c2649Elliott Hughes std::function<void(const std::string&, const std::string&, bool)>); 63e096e36e50b4b66638ebc4d3c09c2ee35f538dfaStephen Smalleyint make_dir(const char *path, mode_t mode); 64e096e36e50b4b66638ebc4d3c09c2ee35f538dfaStephen Smalleyint restorecon(const char *pathname); 65ae76f6dbcf3dfd6eda223911f91f4a1316433771Nick Kralevichint restorecon_recursive(const char *pathname); 66db5f5d43679546ecde2b85151c0a8b8ccd605b07Andres Moralesstd::string bytes_to_hex(const uint8_t *bytes, size_t bytes_len); 67f13b1b31399aa501514eb9beeef303d1ae2e0e14Lee Campbellbool is_dir(const char* pathname); 68b7349902a945903f9e36a569051f5131beb0bc24Tom Cherrybool expand_props(const std::string& src, std::string* dst); 693899e9fc01cf608f19f716749c54cc5c4d17e766Colin Cross#endif 70