未验证 提交 988c5dc8 编写于 作者: O openharmony_ci 提交者: Gitee

!1317 L1 linux 版本 init watchdog 会导致系统重启 暂时回退修改

Merge pull request !1317 from cheng_jinsong/init0927_1
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -40,34 +40,28 @@ ...@@ -40,34 +40,28 @@
#define PRETIMEOUT_GAP 5 #define PRETIMEOUT_GAP 5
#define PRETIMEOUT_DIV 2 #define PRETIMEOUT_DIV 2
#define WATCHDOG_DEV "/dev/watchdog" static void WaitAtStartup(const char *source)
static int WaitForWatchDogDevice(void)
{ {
unsigned int count = 0; unsigned int count = 0;
struct stat sourceInfo; struct stat sourceInfo;
const unsigned int waitTime = 500000; const unsigned int waitTime = 500000;
while ((stat(WATCHDOG_DEV, &sourceInfo) < 0) && (errno == ENOENT) && (count < WAIT_MAX_COUNT)) { do {
usleep(waitTime); usleep(waitTime);
count++; count++;
} } while ((stat(source, &sourceInfo) < 0) && (errno == ENOENT) && (count < WAIT_MAX_COUNT));
if (count == WAIT_MAX_COUNT) { if (count == WAIT_MAX_COUNT) {
INIT_LOGE("Wait for watchdog device failed after %u seconds", (WAIT_MAX_COUNT * waitTime) / CONVERSION_BASE); INIT_LOGE("wait for file:%s failed after %u seconds.", source, (WAIT_MAX_COUNT * waitTime) / CONVERSION_BASE);
return 0;
} }
return 1; return;
} }
int main(int argc, const char *argv[]) int main(int argc, const char *argv[])
{ {
if (WaitForWatchDogDevice() == 0) { WaitAtStartup("/dev/watchdog");
return -1; int fd = open("/dev/watchdog", O_RDWR);
} if (fd == -1) {
int fd = open(WATCHDOG_DEV, O_RDWR | O_CLOEXEC); INIT_LOGE("Can't open /dev/watchdog.");
if (fd < 0) { return 1;
INIT_LOGE("Open watchdog device failed, err = %d", errno);
return -1;
} }
int interval = 0; int interval = 0;
...@@ -82,10 +76,11 @@ int main(int argc, const char *argv[]) ...@@ -82,10 +76,11 @@ int main(int argc, const char *argv[])
} }
gap = (gap > 0) ? gap : DEFAULT_GAP; gap = (gap > 0) ? gap : DEFAULT_GAP;
INIT_LOGI("Watchdog started (interval %d, margin %d), fd = %d\n", interval, gap, fd);
#ifdef OHOS_LITE_WATCHDOG #ifdef OHOS_LITE_WATCHDOG
#ifndef LINUX_WATCHDOG #ifndef LINUX_WATCHDOG
if (setpriority(PRIO_PROCESS, 0, 14) != 0) { // 14 is process priority if (setpriority(PRIO_PROCESS, 0, 14) != 0) { // 14 is process priority
INIT_LOGE("setpriority failed, err=%d", errno); INIT_LOGE("setpriority failed err=%d\n", errno);
} }
#endif #endif
#endif #endif
...@@ -98,16 +93,12 @@ int main(int argc, const char *argv[]) ...@@ -98,16 +93,12 @@ int main(int argc, const char *argv[])
#endif #endif
int ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeoutSet); int ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeoutSet);
if (ret < 0) { if (ret) {
INIT_LOGE("ioctl failed with command WDIOC_SETTIMEOUT, err = %d", errno); INIT_LOGE("Failed to set timeout to %d\n", timeoutSet);
close(fd);
return -1;
} }
ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeoutGet); ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeoutGet);
if (ret < 0) { if (ret) {
INIT_LOGE("ioctl failed with command WDIOC_GETTIMEOUT, err = %d", errno); INIT_LOGE("Failed to get timeout\n");
close(fd);
return -1;
} }
if (timeoutGet > 0) { if (timeoutGet > 0) {
...@@ -115,19 +106,15 @@ int main(int argc, const char *argv[]) ...@@ -115,19 +106,15 @@ int main(int argc, const char *argv[])
} }
#ifdef WDIOC_SETPRETIMEOUT #ifdef WDIOC_SETPRETIMEOUT
preTimeout = timeoutGet - PRETIMEOUT_GAP; // ensure pre timeout smaller then timeout preTimeout = timeoutGet - PRETIMEOUT_GAP; // ensure pretimeout smaller then timeout
if (preTimeout > 0) { if (preTimeout > 0) {
ret = ioctl(fd, WDIOC_SETPRETIMEOUT, &preTimeout); ret = ioctl(fd, WDIOC_SETPRETIMEOUT, &preTimeout);
if (ret < 0) { if (ret) {
INIT_LOGE("ioctl failed with command WDIOC_SETPRETIMEOUT, err = %d", errno); INIT_LOGE("Failed to set pretimeout to %d\n", preTimeout);
close(fd);
return -1;
} }
ret = ioctl(fd, WDIOC_GETPRETIMEOUT, &preTimeoutGet); ret = ioctl(fd, WDIOC_GETPRETIMEOUT, &preTimeoutGet);
if (ret < 0) { if (ret) {
INIT_LOGE("ioctl failed with command WDIOC_GETPRETIMEOUT, err = %d", errno); INIT_LOGE("Failed to get pretimeout\n");
close(fd);
return -1;
} }
} }
...@@ -136,14 +123,8 @@ int main(int argc, const char *argv[]) ...@@ -136,14 +123,8 @@ int main(int argc, const char *argv[])
} }
#endif #endif
INIT_LOGI("watchdog started (interval %d, margin %d)", interval, gap);
while (1) { while (1) {
ret = ioctl(fd, WDIOC_KEEPALIVE); ioctl(fd, WDIOC_KEEPALIVE);
if (ret < 0) {
// Fed watchdog failed, we don't need to quit the process.
// Wait for kernel to trigger panic.
INIT_LOGE("ioctl failed with command WDIOC_KEEPALIVE, err = %d", errno);
}
sleep(interval); sleep(interval);
} }
close(fd); close(fd);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册