linux下c程序调用reboot函数实现直接重启【转】

简介: 转自:http://www.blog.chinaunix.net/uid-20564848-id-73878.html linux下c程序调用reboot函数实现直接重启  当然你也可以直接调用system("reboot"),但是这里完全基于库函数来实现,不依赖于/sbin/reboot这个外部程序这里reboot()的其他命令,比如RB_POWER_OFF,并不能保证关闭计算机电源.

转自:http://www.blog.chinaunix.net/uid-20564848-id-73878.html

linux下c程序调用reboot函数实现直接重启

  当然你也可以直接调用system("reboot"),但是这里完全基于库函数来实现,不依赖于/sbin/reboot这个外部程序
这里reboot()的其他命令,比如RB_POWER_OFF,并不能保证关闭计算机电源.

#include <unistd.h>
#include <sys/reboot.h>
int main()
{
    sync(); // 同步磁盘数据,将缓存数据回写到硬盘,以防数据丢失[luther.gliethttp]
    return reboot(RB_AUTOBOOT);
}

luther@gliethttp:~$ vim /usr/include/sys/reboot.h
/* Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

/* This file should define RB_* macros to be used as flag
   bits in the argument to the `reboot' system call.  */

#ifndef _SYS_REBOOT_H
#define _SYS_REBOOT_H    1

#include <features.h>

/* Perform a hard reset now.  */
#define RB_AUTOBOOT    0x01234567

/* Halt the system.  */
#define RB_HALT_SYSTEM    0xcdef0123

/* Enable reboot using Ctrl-Alt-Delete keystroke.  */
#define RB_ENABLE_CAD    0x89abcdef

/* Disable reboot using Ctrl-Alt-Delete keystroke.  */
#define RB_DISABLE_CAD    0

/* Stop system and switch power off if possible.  */
#define RB_POWER_OFF    0x4321fedc

__BEGIN_DECLS

/* Reboot or halt the system.  */
extern int reboot (int __howto) __THROW;

__END_DECLS

#endif    /* _SYS_REBOOT_H */

luther@gliethttp:~$ man 2 reboot

REBOOT(2)                  Linux Programmer’s Manual                 REBOOT(2)



NAME
       reboot - reboot or enable/disable Ctrl-Alt-Del

SYNOPSIS
       /* For libc4 and libc5 the library call and the system call
          are identical, and since kernel version 2.1.30 there are
          symbolic names LINUX_REBOOT_* for the constants and a
          fourth argument to the call: */

       #include <unistd.h>
       #include <linux/reboot.h>

       int reboot(int magic, int magic2, int cmd, void *arg);

       /* Under glibc some of the constants involved have gotten
          symbolic names RB_*, and the library call is a 1-argument
          wrapper around the 3-argument system call: */

       #include <unistd.h>
       #include <sys/reboot.h>

       int reboot(int cmd);

DESCRIPTION
       The  reboot()  call  reboots the system, or enables/disables the reboot
       keystroke (abbreviated CAD, since the default  is  Ctrl-Alt-Delete;  it
       can be changed using loadkeys(1)).

       This   system   call  will  fail  (with  EINVAL)  unless  magic  equals
       LINUX_REBOOT_MAGIC1   (that   is,   0xfee1dead)   and   magic2   equals
       LINUX_REBOOT_MAGIC2  (that  is, 672274793).  However, since 2.1.17 also
       LINUX_REBOOT_MAGIC2A  (that  is,  85072278)  and  since   2.1.97   also
       LINUX_REBOOT_MAGIC2B   (that  is,  369367448)  and  since  2.5.71  also
       LINUX_REBOOT_MAGIC2C (that is, 537993216) are permitted  as  value  for
       magic2.   (The  hexadecimal  values of these constants are meaningful.)
       The cmd argument can have the following values:

       LINUX_REBOOT_CMD_RESTART
              (RB_AUTOBOOT, 0x1234567).  The message "Restarting  system."  is
              printed, and a default restart is performed immediately.  If not
              preceded by a sync(2), data will be lost.

       LINUX_REBOOT_CMD_HALT
              (RB_HALT_SYSTEM, 0xcdef0123; since 1.1.76).  The message "System
              halted." is printed, and the system is halted.  Control is given
              to the ROM monitor, if there is  one.   If  not  preceded  by  a
              sync(2), data will be lost.

       LINUX_REBOOT_CMD_POWER_OFF
              (0x4321fedc;  since  2.1.30).   The  message  "Power  down."  is
              printed, the system is stopped, and all power  is  removed  from
              the  system,  if  possible.   If not preceded by a sync(2), data
              will be lost.

       LINUX_REBOOT_CMD_RESTART2
              (0xa1b2c3d4; since 2.1.30).  The message "Restarting system with
              command  '%s'"  is  printed,  and  a  restart (using the command
              string given in arg) is performed immediately.  If not  preceded
              by a sync(2), data will be lost.

       LINUX_REBOOT_CMD_CAD_ON
              (RB_ENABLE_CAD,  0x89abcdef).   CAD is enabled.  This means that
              the CAD keystroke will immediately cause the  action  associated
              with LINUX_REBOOT_CMD_RESTART.

       LINUX_REBOOT_CMD_CAD_OFF
              (RB_DISABLE_CAD,  0).  CAD is disabled.  This means that the CAD
              keystroke will cause a SIGINT signal to be sent to init (process
              1),  whereupon  this  process  may  decide  upon a proper action
              (maybe: kill all processes, sync, reboot).

       Only the superuser may call reboot().

       The precise effect of the above actions depends  on  the  architecture.
       For the i386 architecture, the additional argument does not do anything
       at present (2.1.122), but the type of reboot can be determined by  ker‐
       nel  command-line  arguments  ("reboot=...") to be either warm or cold,
       and either hard or through the BIOS.

RETURN VALUE
       For the values of cmd that stop or restart  the  system,  a  successful
       call  to  reboot()  does not return.  For the other cmd values, zero is
       returned on success.  In all cases, -1  is  returned  on  failure,  and
       errno is set appropriately.

ERRORS
       EFAULT Problem      with      getting      userspace     data     under
              LINUX_REBOOT_CMD_RESTART2.

       EINVAL Bad magic numbers or cmd.

       EPERM  The calling process has insufficient privilege to call reboot();
              the CAP_SYS_BOOT capability is required.

CONFORMING TO
       reboot() is Linux-specific, and should not be used in programs intended
       to be portable.

SEE ALSO
       sync(2),   bootparam(7),   capabilities(7),   ctrlaltdel(8),   halt(8),
       reboot(8)

COLOPHON
       This  page  is  part of release 3.01 of the Linux man-pages project.  A
       description of the project, and information about reporting  bugs,  can
       be found at http://www.kernel.org/doc/man-pages/.



Linux                             2008-02-11                         REBOOT(2)

 

vim /usr/include/sys/reboot.h
--------------------------------------------------
#define RB_AUTOBOOT          0x01234567
#define RB_HALT_SYSTEM       0xcdef0123
#define RB_ENABLE_CAD        0x89abcdef
#define RB_DISABLE_CAD       0
#define RB_POWER_OFF         0x4321fedc 




1 sys_reboot() -->
2     kernel_restart() -->
3        kernel_restart_prepare()
4        machine_restart()




linux-2.6.21.7/include/linux/reboot.h
--------------------------------------------
#define    LINUX_REBOOT_CMD_RESTART     0x01234567
#define    LINUX_REBOOT_CMD_HALT        0xCDEF0123
#define    LINUX_REBOOT_CMD_CAD_ON      0x89ABCDEF
#define    LINUX_REBOOT_CMD_CAD_OFF     0x00000000
#define    LINUX_REBOOT_CMD_POWER_OFF   0x4321FEDC
#define    LINUX_REBOOT_CMD_RESTART2    0xA1B2C3D4
#define    LINUX_REBOOT_CMD_SW_SUSPEND  0xD000FCE2
#define    LINUX_REBOOT_CMD_KEXEC       0x45584543

 

【作者】 张昺华
【新浪微博】 张昺华--sky
【twitter】 @sky2030_
【facebook】 张昺华 zhangbinghua
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
目录
相关文章
|
1月前
|
安全 Linux iOS开发
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
137 6
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
|
安全 Linux Shell
Linux上执行内存中的脚本和程序
【9月更文挑战第3天】在 Linux 系统中,可以通过多种方式执行内存中的脚本和程序:一是使用 `eval` 命令直接执行内存中的脚本内容;二是利用管道将脚本内容传递给 `bash` 解释器执行;三是将编译好的程序复制到 `/dev/shm` 并执行。这些方法虽便捷,但也需谨慎操作以避免安全风险。
402 7
|
网络协议 Linux
Linux查看端口监听情况,以及Linux查看某个端口对应的进程号和程序
Linux查看端口监听情况,以及Linux查看某个端口对应的进程号和程序
1303 2
|
Linux Python
linux上根据运行程序的进程号,查看程序所在的绝对路径。linux查看进程启动的时间
linux上根据运行程序的进程号,查看程序所在的绝对路径。linux查看进程启动的时间
449 2
|
运维 Java Linux
【运维基础知识】Linux服务器下手写启停Java程序脚本start.sh stop.sh及详细说明
### 启动Java程序脚本 `start.sh` 此脚本用于启动一个Java程序,设置JVM字符集为GBK,最大堆内存为3000M,并将程序的日志输出到`output.log`文件中,同时在后台运行。 ### 停止Java程序脚本 `stop.sh` 此脚本用于停止指定名称的服务(如`QuoteServer`),通过查找并终止该服务的Java进程,输出操作结果以确认是否成功。
818 1
|
消息中间件 分布式计算 Java
Linux环境下 java程序提交spark任务到Yarn报错
Linux环境下 java程序提交spark任务到Yarn报错
193 5
|
NoSQL Linux C语言
嵌入式GDB调试Linux C程序或交叉编译(开发板)
【8月更文挑战第24天】本文档介绍了如何在嵌入式环境下使用GDB调试Linux C程序及进行交叉编译。调试步骤包括:编译程序时加入`-g`选项以生成调试信息;启动GDB并加载程序;设置断点;运行程序至断点;单步执行代码;查看变量值;继续执行或退出GDB。对于交叉编译,需安装对应架构的交叉编译工具链,配置编译环境,使用工具链编译程序,并将程序传输到开发板进行调试。过程中可能遇到工具链不匹配等问题,需针对性解决。
809 3
|
网络协议 Linux Shell
在Linux中,如何通过一个端口找到程序?
在Linux中,如何通过一个端口找到程序?
|
Linux API
在Linux中,程序产生了库日志虽然删除了,但磁盘空间未更新是什么原因?
在Linux中,程序产生了库日志虽然删除了,但磁盘空间未更新是什么原因?