2018-12-06 06:11:19 sjdsx1990 阅读数 29
Evernote Export

Problem

linux中添加用户:

sudo groupadd tomcat
sudo mkdir /opt/tomcat
sudo useradd -s /bin/nologin -g tomcat -d /opt/tomcat tomcat`--没有erroe||warning,安排上了~~

出现警告:useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.

出现
同样问题的兄台?
解决方法:???

Analysis

之前参考的文章有毒,useradd -d命令本身就创建目录,所以没必要mkdir.自己挖坑自己挑啊····

If the directory didn’t exist , the Linux useradd process creates the directory and copies the skel files : .kshrc , .bashrc , .bash_profile and .bash_logout files into the user directory.

solution

思路很简单,推倒重来:删掉该文件夹删掉该用户

SEL linux:Security-Enhanced Linux

SELinux user mapping

RHlinux官方文档讲解SELlinux,相当清晰
与mandatory access controlmechanism相对应的discretionary access controls

  • 中间出现小问题,删掉account和directory后添加新用户
    提示:useradd: group 'tomcat' does not exist
    groupadd tomcat后重新执行
    提示:Creating mailbox file: File exists
    rm -rf /var/spool/mail/tomcat即可
    继续执行statement:useradd -s /bin/nologin -g tomcat -d /opt/tomcat tomcat--没有erroe||warning,安排上了~~

总结

linux添加tomcat用户时只需要两个语句
groupadd tomcat
useradd -s /bin/nologin -g tomcat -d /opt/tomcat
addition:groupadd
-g, --gid GROUP
The group name or number of the user?s initial login group. The group name must exist. A group number must refer to
an already existing group.
If not specified, the behavior of useradd will depend on the USERGROUPS_ENAB variable in /etc/login.defs. If this
variable is set to yes (or -U/--user-group is specified on the command line), a group will be created for the user,
with the same name as her loginname. If the variable is set to no (or -N/--no-user-group is specified on the command
line), useradd will set the primary group of the new user to the value specified by the GROUP variable in
/etc/default/useradd, or 100 by default.

2011-02-24 18:14:34 envy2008 阅读数 75

 

 

 

把上个版本一对一的聊天室,修改了一下,使其能进行一(server)对多(client)的聊天。

直接上源码:

 

 

/*
 provide the function to log.
 input: environment varibles, (1) LEVEL=DEBUG||WARNING||ERROE (2)LOGPATH(the log file saving path)
 output: different level log file
*/
#ifndef  _LOG_H_
#define  _LOG_H_

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>


//define the debug level
#define  DEBUG 5
#define  WARNNING 4
#define  ERROR 3
#define  WORKKING 1
//if return 0,means ok, !=0, means not ok.
int mylog(char * optional_msg,char * msg, int level);	

#endif

 

    #include "log.h"

int mylog(char * optional_msg,char * msg, int level)
{
	char * pc_level=getenv("LEVEL");
	char * pc_path =getenv("LOGPATH");
	
	//configure debug level
	if(pc_level==NULL)	
	{	
		pc_level="WORKKING";
	}
	//printf("current level is  %s\n",pc_level);
		
	//configure path	
	char path[256];
	if(pc_path==NULL)
	{		
		getcwd(path, sizeof(path));
		//printf("current path is %s\n",path);
		pc_path=path;	
				
	}
	
	//printf("configured path is %s\n",pc_path);	
	
	int int_src=0;
	char *pc_preLog;
	if(strcmp(pc_level,"DEBUG")==0)
	{	
		int_src=5;
	  	pc_preLog="[DEBUG]  ";
	}
	if(strcmp(pc_level,"WARNNING")==0)
	{
		int_src=4;
		pc_preLog="[WARNNING]  ";
	}
	if(strcmp(pc_level,"ERROR")==0)
	{
		int_src=3;
		pc_preLog="[ERROR]  ";
	}
	if(strcmp(pc_level,"WORKKING")==0)
	{
		int_src=1;
		pc_preLog="[WORKKING]  ";
	}
	
	//create a file to write log.	
	char target_path[400];
	strcpy(target_path,pc_path);
	strcat(target_path,"/log.txt");
	//printf("target path is %s\n",target_path);
	
	
	if(int_src>=level)
	{
		int fileid=open(target_path,O_WRONLY|O_APPEND|O_CREAT,0);
		if(fileid<0)
		{
			printf("create log file failed!\n");
			exit(0);	
		}
		write(fileid,optional_msg,strlen(optional_msg));
		write(fileid,pc_preLog,strlen(pc_preLog));
		write(fileid,msg,strlen(msg));
		//set time
		time_t timep;
        time (&timep);
        write(fileid,"   ----",strlen("   ----"));
        write(fileid,ctime(&timep),strlen(ctime(&timep)));
        
		write(fileid,"  \n",strlen("  \n"));
		close(fileid);
    }
	return 0;
}  



 

 

#ifndef _LIST_H_
#define   _LIST_H_
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct List 
{
	int *p_buffer;
	int *p_begin;
	int *p_end;
	int *p_cursor;
		
};
// you must add struct, otherwise it will prompt "expected ‘)’ before ‘*’ token"
void init(struct List * p_list);
int get_buffer_length (struct List * p_list);
int put_into_buffer(struct List* p_list,int p_number);
void delete_from_buffer(struct List* p_list, int p_number);
#endif
 

 

#include "List.h"


void init(struct List * p_list)
{
	int *p_temp=malloc(10*sizeof(10));
	p_list->p_buffer=p_temp;
	p_list->p_begin=p_temp;
	p_list->p_end=p_list->p_begin+9;
	p_list->p_cursor=p_list->p_begin;
	
}
int get_buffer_length (struct List * p_list)
{
     return p_list->p_cursor-p_list->p_begin;	
}

// return 0:OK,1:false
int put_into_buffer(struct List * p_list,int p_number)
{
	
	printf("before put  ");
	int m=0;
	for(;m<10;m++)
	{
		int shuzi=*(p_list->p_begin+m);
		printf("%d ",shuzi);
		
	}
	printf("\n");
	if(p_list->p_cursor>p_list->p_end)
	{
			printf("buffer is full, put into buffer error\n");
			return 1;			
	}
	   *p_list->p_cursor=p_number;
		p_list->p_cursor++;	
		
	printf("after put  ");	
	int n=0;
	for(;n<10;n++)
	{
		int shuzi=*(p_list->p_begin+n);
		printf("%d ",shuzi);
		
	}
	printf("\n");
		
	return 0;
}


void  delete_from_buffer(struct List* p_list, int p_number)
{
	
    printf("before delete  ");
	int m=0;
	for(;m<10;m++)
	{
		int shuzi=*(p_list->p_begin+m);
		printf("%d ",shuzi);
		
	}
	printf("\n");
	int length=get_buffer_length(p_list);
	
	printf("length is %d\n",length);
	int i=0;
	int * biao;
	for(;i<length;i++)
	{
		if(*(p_list->p_begin+i)==p_number)
		  { 
		  	biao=p_list->p_begin+i;
		  	break;
		  }
		
	}
	printf("i is %d\n",i);
	if(i<length)
	{
		bcopy(biao+1,biao,sizeof(int)*(length-(i+1)));
		memset(p_list->p_begin+(length-1),'\0',sizeof(int)*1);
	
		p_list->p_cursor--;
    }
	
	
	printf("after delete  ");	
	int n=0;
	for(;n<10;n++)
	{
		int shuzi=*(p_list->p_begin+n);
		printf("%d ",shuzi);
		
	}
	printf("\n");
}
 

  #include "log.h"

#include "List.h"
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>


#define BUFFER_SIZE 256
//gloable variables
 struct List list;



void * receiveMsg(void * args)
{
	int *p_client_socket_id=(int *)args;
	int client_socket_id=*p_client_socket_id;
	while(1)
	{	
		char buffer[BUFFER_SIZE]={0};
	    mylog("[Server] ","before receive.....",DEBUG);
		int recv_length=recv(client_socket_id,buffer,BUFFER_SIZE,0);
		mylog("[Server] ","after receive.....",DEBUG);
		if (recv_length <= 0)
	    {
	          mylog("[Server] ","error comes when recieve data from server!",DEBUG);
	          delete_from_buffer(&list,client_socket_id);	          
	          if(get_buffer_length(&list)==0)
	          	exit(0);
	          break;
	    }
	    
	    printf("from server:%s\n",buffer);
    }
	
	return NULL;
}

void closeSocket()
{
	mylog("[Server] "," positively close connection socket",DEBUG);
	int i=0;
	for(;i<get_buffer_length(&list);i++)
	{
		int socketid=list.p_buffer[i];
		close(socketid);
	}
	free(list.p_buffer);
	exit(0);	
}

void *sendMsg(void *arg)
{
	while(1)
	{	
		char msg[BUFFER_SIZE];
		//scanf("%[^n]",msg);
		fgets(msg,BUFFER_SIZE,stdin);	
        int i=0;
        for(;i<get_buffer_length(&list);i++)
        {
        	int target_socket_id=*(list.p_begin+i);
			int length_send=send(target_socket_id,msg,BUFFER_SIZE,0);
			//printf("send msg length is %d\n",length_send);
			if(length_send<0)
				printf("send worry\n");	
		}	
	}
}

int main(int argc, char ** argv)
{

  
   init(&list);
//setup the sigint
    signal(SIGINT,closeSocket);
    
	struct sockaddr_in server_addr,client_addr;
	int size_of_addr=sizeof(server_addr);
	//printf("size of addr is %d\n",size_of_addr);
	
	//init server_addr;
	bzero(&server_addr,sizeof(server_addr));
	server_addr.sin_family=AF_INET;
	server_addr.sin_port=htons(9999);
	server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
	char * pc;
	pc=inet_ntoa(server_addr.sin_addr);
	//printf("server11 ip is %s\n",pc);
	
	int socketid=0;
	
	//create socket file
	mylog("[Server] ","before create socket......",DEBUG);
	if((socketid=socket(AF_INET,SOCK_STREAM,0))<0)
		mylog("[Server] ","create socket error",DEBUG);
	mylog("[Server] ","after create socket......",DEBUG);	
	//bind
	if(bind(socketid,(struct sockaddr*)&server_addr,size_of_addr)<0)
		mylog("[Server] ","bind server address error",DEBUG);
	
	//listen
    mylog("[Server] ","before listen......",DEBUG);
    
	if(listen(socketid,10)<0)
		mylog("[Server] ","listen error",DEBUG);
		
	mylog("[Server] ","after listen......",DEBUG);	
	

	socklen_t client_length=sizeof(client_addr);
	mylog("[Server] ","before accept......",DEBUG);
	while(1)
	{
		int client_socket_id=accept(socketid,(struct sockaddr*)&client_addr,&client_length);
		mylog("[Server] ","after accept......",DEBUG);
		if(client_socket_id<0)
			mylog("[Server] "," client socket file id is negetive, worry",DEBUG);
			
		//set receive thead
		pthread_t pthread_id;
		pthread_id=pthread_create(&pthread_id,NULL,	receiveMsg,(void *)&client_socket_id);
		
		char *pcwelcome="welcome\n";
		int length_string=strlen(pcwelcome);
		send(client_socket_id,pcwelcome,length_string,0);
		put_into_buffer(&list,client_socket_id);
		
		//set send thread
		pthread_t send_thread_id;
		send_thread_id=pthread_create(&send_thread_id,NULL,	sendMsg,NULL);
    }
	while(1)
	{	
		char msg[BUFFER_SIZE];
		//scanf("%[^n]",msg);
		fgets(msg,BUFFER_SIZE,stdin);	
        int i=0;
        for(;i<get_buffer_length(&list);i++)
        {
        	int target_socket_id=*(list.p_begin+i);
			int length_send=send(target_socket_id,msg,BUFFER_SIZE,0);
			//printf("send msg length is %d\n",length_send);
			if(length_send<0)
				printf("send worry\n");	
		}	
	}
	//close(client_socket_id);
	return 0;
}

   #include <stdio.h>

#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>
#include "log.h"


#define BUFFER_SIZE 256

int socketid=0;
int client_result=0;
void * receiveMsg(void * args)
{
	while(1)
	{	
		if(client_result<0)
		{
			mylog("[Client] ","no connection",DEBUG);
			exit(0);	
		}
		char buffer[BUFFER_SIZE]={0};
	    mylog("[Client] ","before receive.....",DEBUG);
		int recv_length=recv(socketid,buffer,BUFFER_SIZE,0);
		mylog("[Client] ","after receive.....",DEBUG);
		if (recv_length <= 0)
	    {
	          mylog("[Client] ","error comes when recieve data from server!",DEBUG);
	          exit(0);
	         
	    }
	    
	    printf("from server:%s\n",buffer);
    }
	
	
}

void closeSocket()
{
	printf(" positively close connection socket\n");
	close(socketid);
	exit(0);	
}


int main(int argc, char ** argv)
{
	//setup the sigint
    signal(SIGINT,closeSocket);
    
	struct sockaddr_in server_addr,client_addr;
	int size_of_addr=sizeof(server_addr);
	//printf("size of addr is %d\n",size_of_addr);
	
	//init server_addr;
	bzero(&server_addr,sizeof(client_addr));
	client_addr.sin_family=AF_INET;
	//char* pc_port=argv[1];
	//printf("argv[1]   %s\n",pc_port);
	//int int_port=atoi(pc_port);
	//printf("int_port   %d\n",int_port);
	client_addr.sin_port=htons(9998);
	client_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
	char * pc;
	pc=inet_ntoa(client_addr.sin_addr);
	//printf("server ip is %s\n",pc);
	
	
	
	//create socket file
	if((socketid=socket(AF_INET,SOCK_STREAM,0))<0)
		mylog("[Client] ","create socket error",DEBUG);
		
	//bind
	if(bind(socketid,(struct sockaddr*)&client_addr,size_of_addr)<0)
	{	
		mylog("[Client] ","bind server address error",DEBUG);
		exit(0);
	}
	
	server_addr.sin_family=AF_INET;
	server_addr.sin_port=htons(9999);
	server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");	
	
	socklen_t server_length=sizeof(server_addr);
	client_result=connect(socketid,(struct sockaddr*)&server_addr,server_length);
	
	if(client_result<0)
		printf("connect failed\n");
		
	//set receive thead
	pthread_t pthread_id;
	pthread_id=pthread_create(&pthread_id,NULL,	receiveMsg,NULL);
	
	while(1)
	{	
		char msg[BUFFER_SIZE]={0};
		fgets(msg,BUFFER_SIZE,stdin);
		int messageLength=strlen(msg);
		//printf("message Length is %d\n",messageLength);
		int length_send=send(socketid,msg,messageLength,0);
		//printf("send msg length is %d\n",length_send);
		if(length_send<0)
			mylog("[Client] ","send worry",DEBUG);	
    }
    close(socketid);
	return 0;
	
}

    #include <stdio.h>

#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>
#include "log.h"


#define BUFFER_SIZE 256

int socketid=0;
int client_result=0;
void * receiveMsg(void * args)
{
	while(1)
	{	
		if(client_result<0)
		{
			mylog("[Client] ","no connection",DEBUG);
			exit(0);	
		}
		char buffer[BUFFER_SIZE]={0};
	    mylog("[Client] ","before receive.....",DEBUG);
		int recv_length=recv(socketid,buffer,BUFFER_SIZE,0);
		mylog("[Client] ","after receive.....",DEBUG);
		if (recv_length <= 0)
	    {
	          mylog("[Client] ","error comes when recieve data from server!",DEBUG);
	          exit(0);
	         
	    }
	    
	    printf("from server:%s\n",buffer);
    }
	
	
}

void closeSocket()
{
	printf(" positively close connection socket\n");
	close(socketid);
	exit(0);	
}


int main(int argc, char ** argv)
{
	//setup the sigint
    signal(SIGINT,closeSocket);
    
	struct sockaddr_in server_addr,client_addr;
	int size_of_addr=sizeof(server_addr);
	//printf("size of addr is %d\n",size_of_addr);
	
	//init server_addr;
	bzero(&server_addr,sizeof(client_addr));
	client_addr.sin_family=AF_INET;
	//char* pc_port=argv[1];
	//printf("argv[1]   %s\n",pc_port);
	//int int_port=atoi(pc_port);
	//printf("int_port   %d\n",int_port);
	client_addr.sin_port=htons(9997);
	client_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
	char * pc;
	pc=inet_ntoa(client_addr.sin_addr);
	//printf("server ip is %s\n",pc);
	
	
	
	//create socket file
	if((socketid=socket(AF_INET,SOCK_STREAM,0))<0)
		mylog("[Client] ","create socket error",DEBUG);
		
	//bind
	if(bind(socketid,(struct sockaddr*)&client_addr,size_of_addr)<0)
	{	
		mylog("[Client] ","bind server address error",DEBUG);
		exit(0);
	}
	
	server_addr.sin_family=AF_INET;
	server_addr.sin_port=htons(9999);
	server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");	
	
	socklen_t server_length=sizeof(server_addr);
	client_result=connect(socketid,(struct sockaddr*)&server_addr,server_length);
	
	if(client_result<0)
		printf("connect failed\n");
		
	//set receive thead
	pthread_t pthread_id;
	pthread_id=pthread_create(&pthread_id,NULL,	receiveMsg,NULL);
	
	while(1)
	{	
		char msg[BUFFER_SIZE]={0};
		fgets(msg,BUFFER_SIZE,stdin);
		int messageLength=strlen(msg);
		//printf("message Length is %d\n",messageLength);
		int length_send=send(socketid,msg,messageLength,0);
		//printf("send msg length is %d\n",length_send);
		if(length_send<0)
			mylog("[Client] ","send worry",DEBUG);	
    }
    close(socketid);
	return 0;
	
}

其中client.c和client2.c就是bind的端口不同而已,因为前期设计有问题,所以就用这个蠢办法来验证,这不是一个好的设计和实现。

2017-05-01 00:49:42 HandsomeHong 阅读数 281

#include <netdb.h>

void herror(const char*s)//先输出参数中的字符串,再输出系统提供调用网络函数发生错误时的错误信息,无错误会输出“Resolver Erroe 0(no error)”

extern int h_errno;//存储最近一次错误的错误号,这个变量在外部已经定义了

char *hstrerror(int err)将错误号转换为字符串

2017-09-14 19:27:47 lyn_00 阅读数 555

博主是linux初学者,在装vim智能补全插件YouCompleteMe后发现没有代码补全,配置文件是没有问题的。

在进入vim后出现如下错误提示:
The ycmd server SHUT DOWN(restart with ‘: YcmRestarSever’).Unexpected erroe while loading the YCM core library.Type ‘:YcmToggleLogs ycmd_59745_stderr_Bdov_E.log’ to check the logs

我尝试过将vim升级到8.0,以及各种博客的解决方法,最后在大佬的指导下,以及各种博客的参照下,终于解决了这个问题(博主现在只想说感动天感动地!!)

至于原因:
其实就是类似 linux系统下的缺失 libstdc++.so.6: version’GLIBCXX_3.4.20’的问题
是由于 libstdc++.so.6的版本过低造成的
执行

strings /usr/lib64/libstdc++.so.6 | grep GLIBC

这里写图片描述
我们可以看到里面确实是没有version’GLIBCXX_3.4.20的。

解决方法
首先要升级gcc
但是centos命令直接升级gcc只能升级到4.8.5,要升到更高版本只有去网上下载
(在安装gcc的时候一定要小心谨慎!!!)
我是升级到了4.9.3
下载地址是:http://fr.mirror.babylon.network/gcc/releases/gcc-4.9.3/ 下载其中的包为:gcc-4.9.3.tar.bz2
如果下载后直接安装会出现
configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+
的问题。
因为安装该版本的GCC 需要GMP 版本 >= 4.2 MPFR版本 >= 2.4.0 MPC版本 >= 0.8.0 这3个依赖缺一不可。
需要手动下载

这三个依赖分别是:gmp,mpfr和mpc,下载地址分别如下:
    gmp:http://ftp.gnu.org/gnu/gmp/ 版本:gmp-5.0.1.tar.gz
    mpfr(GNU镜像):http://ftp.gnu.org/gnu/mpfr/ 或者官网:http://www.mpfr.org/mpfr-current/ 版本:mpfr-3.1.5.tar.gz
    mpc:http://ftp.gnu.org/gnu/mpc/ 版本:mpc-1.0.3.tar.gz

下载完成后,执行如下命令
安装gmp

tar -xvzf gmp-5.0.1.tar.gz
cd gmp-5.0.1/
mkdir temp
cd temp/
../configure --prefix=/usr/local/gmp-5.0.1
make
make install

安装mpfr

tar -xvzf mpfr-3.1.5.tar.gz
cd mpfr-3.1.5/
mkdir temp
cd temp/
../configure --prefix=/usr/local/mpfr-3.1.5 --with-gmp=/usr/local/gmp-5.0.1
make
make install

到这里mpfr安装完毕,并且必须添加–with-gmp导入gmp依赖,如果不加这个参数也会安装成功,但是后面安装GCC会报一个内部依赖的错误,如果这里不加会很麻烦,然后再安装mpc,命令如下:

tar -xvzf mpc-1.0.3.tar.gz
cd mpc-1.0.3/
mkdir temp
cd temp/
../configure --prefix=/usr/local/mpc-1.0.3 --with-gmp=/usr/local/gmp-5.0.1 --with-mpfr=/usr/local/mpfr-3.1.5
make
make install

同样一定要加上依赖的参数,现在mpc也安装完毕,然后执行 vim /etc/profile 编辑环境变量配置文件,直接在文件最后添加一行下面的变量:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mpc-1.0.3/lib:/usr/local/gmp-5.0.1/lib:/usr/local/mpfr-3.1.5/lib

上面的路径要和实际安装时编译的路径一致,保存并退出后,执行 source /etc/profile 使环境变量生效

最后就可以开始安装gcc了,安装过程如下:

tar -jxvf gcc-4.9.3.tar.bz2
cd gcc-4.9.3/
mkdir output
cd output/
../configure --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-5.0.1 --with-mpfr=/usr/local/mpfr-3.1.5 --with-mpc=/usr/local/mpc-1.0.3

make -j4
make install

ps安装时间可能有点长…忍忍就过去啦!

安装成功之后执行 gcc -v 或者 gcc –version 查看新的版本信息如下:

这里写图片描述

然后

sudo updatedb 
locate libstdc++.so.6 

这里写图片描述

在这里我们找到了我们需要的libsrdc++.6.0.20

然后root在/usr/lib64目录下创建软链接

ln -sf /usr/local/lib64/libstdc++.so.6.0.20 libstdc++.so.6

然后你执行vim就会发现代码补全神奇地出来了!!!!!

没有更多推荐了,返回首页