#include <syslog.h> void syslog ( int priority, const char* message, ... ); // 可變參數
#include <syslog.h> #define LOG_EMERG 0 /* 系統不可用 */ #define LOG_ALERT 1 /* 報警,須要當即採起動做 */ #define LOG_CRIT 2 /* 很是嚴重的狀況 */ #define LOG_ERR 3 /* 錯誤 */ #define LOG_WARNING 4 /* 警告 */ #define LOG_NOTICE 5 /* 通知 */ #define LOG_INFO 7 /* 信息 */ #define LOG_DEBUG 8 /* 調試 */
#include <syslog.h> void openlog ( const char* ident, int logopt, int facility );
#define LOG_PID 0x01 /* 在日誌消息中包含程序PID */ #define LOG_CONS 0x02 /* 若是消息不能記錄到日誌文件,則打印至終端 */ #define LOG_ODELAY 0x04 /* 延遲打開日誌功能直到第一次調用syslog */ #define LOG_NDELAY 0x08 /* 不延遲打開日誌功能 */
#include <syslog.h> int setlogmask( int maskpri );
setlogmask(LOG_ERR); //僅僅記錄ERR級別的日誌消息 setlogmask(LOG_UPTO(LOG_ERR)); //記錄ERR以及以前的全部日誌的消息[0,3]
#include <syslog.h> void closelog();
#include <sys/types.h> #include <unistd.h> uid_t getuid(); //獲取實際用戶ID uid_t geteuid(); //獲取有效用戶ID gid_t getgid(); // 獲取實際組ID gid_t getegid(); // 獲取有效組ID int setuid( uid_t uid ); //設置實際用戶ID int seteuid( uid_t uid ); //設置有效用戶ID int setgid( gid_t gid ); // 設置實際組ID int setegid( gid_t gid ); // 設置有效組ID
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { uid_t uid = getuid(); uid_t euid = geteuid(); printf("userid is %d,effective userid is %d\n",uid,euid); return 0; }
@ubuntu:~$ sudo chown root:root test_uid // 修改目標文件的全部者爲root @ubuntu:~$ sudo chmod +s test_uid // 設置目標文件的set-user-id 標誌 注意這段很重要 @ubuntu:~$ ./test_uid // 運行程序 userid is 1000,effective userid is 0從測試程序的輸出來看,進程的UID是啓動程序的用戶的ID,而EUID則是root帳戶(文件的全部者) 的ID。
#include <unistd.h> pid_t getpgid( pid_t pid );
#include <unistd.h> int setpgid( pid_t pid, pid_t pgid );
#include <unistd.h> pid_t setsid( void );
#include <unistd.h> pid_t getsid ( pid_t pid );
#include <sys/resource.h> int getrlimit( int resource, struct rlimit *rlim ); int setrlimit( int resource, const struct rlimit *rlim ); rlimit結構體的定義以下: struct rlimit { rlim_t rlim_cur; //軟限制 rlim_t rlim_max; //硬限制 };
#include <unistd.h> char* getcwd( char* buf, size_t size ); int chdir( const char* path );
#include <unistd.h> int chroot( const char* path );
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> bool daemonize() { pid_t pid = fork(); if (pid < 0) { return false; } else if (pid > 0) { exit(0); } umask(0); //設置新的會話,設置本進程爲進程組的首領 pid_t sid = setsid(); if (sid < 0) return false; //切換工做目錄 if ((chdir("/")) < 0) return false; printf("2. pid: %ld, parent id: %ld\n", (long)getpid(), (long)getppid()); //關閉標準輸入,標準輸出,標準錯誤輸出 close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); //重定向標準輸入,標準輸出,標準錯誤輸出到/dev/null open("/dev/null", O_RDONLY); open("/dev/null", O_RDWR); open("/dev/null", O_RDWR); return true; } int main(int argc, char **argv) { printf("1. pid: %ld, parent id: %ld\n", (long)getpid(), (long)getppid());; daemonize(); return 0; }
#include <unistd.h> int daemon(int nochdir, int noclose);nochdir參數用於指定是否改變工做目錄,若是是0,則工做目錄將設置爲」/「, 不然繼續使用當前工做目錄。
noclose參數爲0時,標準輸入,標準輸出,標準錯誤輸出都被重定向到/dev/null,不然依然使用原來的設備。linux
調用成功時返回0,失敗時返回-1,並設置errno。shell