settimeofday函数执行没有权限
我写了一个应用程序其中用到了settimeofday函数,但是执行的时候说权限不够,怎么解决这个问题
settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。成功则返回0,失败返回-1,错误代码存于errno。
#include <stdio.h> #include <stdlib.h> #include<unistd.h> #include <errno.h> #include <time.h> #include<sys/time.h> #include <string.h> int set_time(unsigned char *settime) { struct timeval new_tv; int ret; time_t timep,timeq; struct tm *p; time(&timep); p = localtime(&timep); p->tm_year = settime[0]+2000-1900; p->tm_mon = settime[1]-1; p->tm_mday = settime[2]; p->tm_hour = settime[3]; p->tm_min = settime[4]; p->tm_sec = settime[5]; printf("%d %d %d %d %d %d \n",p->tm_year,p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec); timeq = mktime(p); printf("timeq %ld\n",timeq); new_tv.tv_sec = (long)timeq; new_tv.tv_usec = 0; ret = settimeofday(&new_tv,NULL); if(ret == -1) { printf("settimeofday error.%s\n",strerror(errno)); return ret; } else system("hwclock -w"); return 0; } int main() { unsigned char timebuf[] = {13,07,05,17,25,55}; set_time(timebuf); return 0; }