问题集锦(11-15)

来源:互联网 发布:act网络课程 编辑:程序博客网 时间:2024/06/01 09:57

Problem 11. How to set a password for a user and how to grant some permission to a specified user in mysql?

Ans:see the following example:

CREATE USER'radius'@'localhost';

SETPASSWORDFOR 'radius'@'localhost'= PASSWORD('radpass');

 

# The server canread any table in SQL

GRANT SELECT ONradius.*TO 'radius'@'localhost';

 

# The server can writeto the accounting and post-auth logging table.

#

# i.e.

GRANT ALL onradius.radacctTO 'radius'@'localhost';

GRANT ALL onradius.radpostauthTO 'radius'@'localhost';

 

Added: An example for creating a table in mysql database

###########################################################################

# $Id$                #

#                                                                        #

# schema.sql                       rlm_sql - FreeRADIUS SQL Module      #

#                                                                        #

#    Database schema for MySQL rlm_sql module                           #

#                                                                        #

#    To load:                                                           #

#        mysql -uroot -prootpass radius < schema.sql                    #

#                                                                        #

#                                  Mike Machado <mike@innercite.com>    #

###########################################################################

#

#Table structure for table'radacct'

#

 

CREATE TABLEradacct (

 radacctid bigint(21) NOT NULL auto_increment,

 acctsessionid varchar(64) NOT NULL default '',

 acctuniqueid varchar(32) NOT NULL default '',

 username varchar(64) NOT NULL default '',

 groupname varchar(64) NOT NULL default '',

 realm varchar(64) default '',

 nasipaddress varchar(15) NOT NULL default '',

 nasportid varchar(15) default NULL,

 nasporttype varchar(32) default NULL,

 acctstarttime datetime NULL default NULL,

 acctstoptime datetime NULL default NULL,

 acctsessiontime int(12) default NULL,

 acctauthentic varchar(32) default NULL,

 connectinfo_start varchar(50) default NULL,

 connectinfo_stop varchar(50) default NULL,

 acctinputoctets bigint(20) default NULL,

 acctoutputoctets bigint(20) default NULL,

 calledstationid varchar(50) NOT NULL default '',

 callingstationid varchar(50) NOT NULL default '',

 acctterminatecause varchar(32) NOT NULL default '',

 servicetype varchar(32) default NULL,

 framedprotocol varchar(32) default NULL,

 framedipaddress varchar(15) NOT NULL default '',

 acctstartdelay int(12) default NULL,

 acctstopdelay int(12) default NULL,

 xascendsessionsvrkey varchar(10) default NULL,

 PRIMARY KEY  (radacctid),

 KEY username (username),

 KEY framedipaddress (framedipaddress),

 KEY acctsessionid (acctsessionid),

 KEY acctsessiontime (acctsessiontime),

 KEY acctuniqueid (acctuniqueid),

 KEY acctstarttime (acctstarttime),

 KEY acctstoptime (acctstoptime),

 KEY nasipaddress (nasipaddress)

) ;

 

#

#Table structure for table'radcheck'

#

 

CREATE TABLEradcheck (

 id int(11) unsigned NOT NULL auto_increment,

 username varchar(64) NOT NULL default '',

 attribute varchar(32)  NOT NULL default '',

 op char(2) NOT NULL DEFAULT'==',

 value varchar(253) NOT NULL default '',

 PRIMARY KEY  (id),

 KEY username (username(32))

) ;

 

#

#Table structure for table'radgroupcheck'

#

 

CREATE TABLEradgroupcheck (

 id int(11) unsigned NOT NULL auto_increment,

 groupname varchar(64) NOT NULL default '',

 attribute varchar(32)  NOT NULL default '',

 op char(2) NOT NULL DEFAULT'==',

 value varchar(253)  NOT NULL default '',

 PRIMARY KEY  (id),

 KEY groupname (groupname(32))

) ;

 

#

#Table structure for table'radgroupreply'

#

 

CREATE TABLEradgroupreply (

 id int(11) unsigned NOT NULL auto_increment,

 groupname varchar(64) NOT NULL default '',

 attribute varchar(32)  NOT NULL default '',

 op char(2) NOT NULL DEFAULT'=',

 value varchar(253)  NOT NULL default '',

 PRIMARY KEY  (id),

 KEY groupname (groupname(32))

) ;

 

#

#Table structure for table'radreply'

#

 

CREATE TABLEradreply (

 id int(11) unsigned NOT NULL auto_increment,

 username varchar(64) NOT NULL default '',

 attribute varchar(32) NOT NULL default '',

 op char(2) NOT NULL DEFAULT'=',

 value varchar(253) NOT NULL default '',

 PRIMARY KEY  (id),

 KEY username (username(32))

) ;

 

 

#

#Table structure for table'radusergroup'

#

 

CREATE TABLEradusergroup (

 username varchar(64) NOT NULL default '',

 groupname varchar(64) NOT NULL default '',

 priority int(11) NOT NULL default '1',

 KEY username (username(32))

) ;

 

#

#Table structure for table'radpostauth'

#

 

CREATE TABLEradpostauth (

 id int(11) NOT NULLauto_increment,

 username varchar(64) NOT NULL default '',

 pass varchar(64) NOT NULL default '',

 reply varchar(32) NOT NULL default '',

 authdate timestamp(14) NOT NULL,

 PRIMARY KEY  (id)

) ;

 

 

Problem 12. When I tried to start the mysql server on my linux platform, but failed with the following error message:

nohup: ignoring input and redirecting stderr to stdout

Starting mysqld daemon with databases from /usr/local/mysql/var/localhost.localdomain.pid.

091027 10:30:33 mysqld ended, what’s wrong with mysql configuration?

Ans: please open the file of var/ localhost.localdomain.pid, if you are careful enough, you will notice this error message:

/usr/local/mysql/libexec/mysqld: File './mysql-bin.000009' not found (Errcode: 13)

091022 11:05:54 [ERROR] Failed to open log (file './mysql-bin.000009', errno 13)

091022 11:05:54 [ERROR] Could not open log file

091022 11:05:54 [ERROR] Can't init tc log

091022 11:05:54 [ERROR] Aborting, but mysql-bin.000009 does exist in var directory, so all you have to do is to change access permission to this file using the following command:

chmod 755 mysql-bin.000009

 

Problem 13 Give me an example to manipulate a terminal?

Ans: read the following example:

#include <stdio.h>

#include <unistd.h>

#include <termios.h>

#include <term.h>

#include <curses.h>

 

static FILE *output_stream = (FILE *)0;

 

char *menu[] = {

   "a - add new record",

   "d - delete record",

   "q - quit",

   NULL,

};

 

int getchoice(char *greet, char *choices[], FILE *in, FILE *out);

int char_to_terminal(char char_to_write);

 

int main()

{

   int choice = 0;

   FILE *input;

   FILE *output;

   struct termios initial_settings, new_settings;

 

   if (!isatty(fileno(stdout))) {//判断当前是否已被占用,即标准输出是否指向终端

       fprintf(stderr,"You are not a terminal, OK./n");

   }

 

   input = fopen("/dev/tty", "r");//设备文件/dev/tty代表当前终端

   output = fopen("/dev/tty", "w");

   if(!input || !output) {

       fprintf(stderr, "Unable to open /dev/tty/n");

       exit(1);

   }

   tcgetattr(fileno(input),&initial_settings);

   new_settings = initial_settings;

   new_settings.c_lflag &= ~ICANON;//设置终端为non-cananical

   new_settings.c_lflag &= ~ECHO;//禁用输入回显

   new_settings.c_cc[VMIN] = 1;

   new_settings.c_cc[VTIME] = 0;

   new_settings.c_lflag &= ~ISIG;

   if(tcsetattr(fileno(input), TCSANOW, &new_settingoutputs) != 0) {

       fprintf(stderr,"could not set attributes/n");

   }

 

   do {

       choice = getchoice("Please select an action", menu, input, output);

       printf("You have chosen: %c/n", choice);

   } while (choice != 'q');

 

   tcsetattr(fileno(input),TCSANOW,&initial_settings);

   exit(0);

}

 

int getchoice(char *greet, char *choices[], FILE *in, FILE *out)

{

   int chosen = 0;

   int selected;

   int screenrow, screencol = 10;

 

   char **option;

   char *cursor, *clear;

 

   output_stream = out;

 

   setupterm(NULL,fileno(out), (int *)0);

   cursor = tigetstr("cup");

   clear = tigetstr("clear");

 

   screenrow = 4;

   tputs(clear, 1, char_to_terminal);

   tputs(tparm(cursor, screenrow, screencol), 1, char_to_terminal);

   fprintf(out, "Choice: %s", greet);

   screenrow += 2;

   option = choices;       

   while(*option) {

       tputs(tparm(cursor, screenrow, screencol), 1, char_to_terminal);

       fprintf(out,"%s", *option);stream

       screenrow++;

       option++;

   }

 

   do {

       selected = fgetc(in);

       option = choices;

       while(*option) {

           if(selected == *option[0]) {

               chosen = 1;

               break;

           }

           option++;

       }

       if(!chosen) {

           tputs(tparm(cursor, screenrow, screencol), 1, char_to_terminal);

           fprintf(out,"Incorrect choice, select again/n");

       }

   } while(!chosen);

   tputs(clear, 1, char_to_terminal);

   return selected;

}

 

int char_to_terminal(char char_to_write)

{

   if (output_stream) putc(char_to_write, output_stream);

   return 0;

}

 

PS:利用位运算增加或禁用某个属性的方法:

 

X &= ~ATTR(禁用ATTR)

X |= ATTR(启用ATTR

 

Problem 14 what’s the meaning of line discipline?

Ans:

线路规程位于serial driver的上层,塑造serial driver的行为,serial driver是控制硬件进行数据的接收和传送,而line discipline则负责处理这些数据,并交由上层接口函数。

The line discipline(线路规程) glues the low level device driver code with the high level generic interface routines (such as read(2), write(2) and ioctl(2)), andis responsible for implementing the semantics associated with the device.[2] The policy is separated from the device driver so that the same serial hardware driver can be used by devices that require different data handling.

 

For example, the standard line discipline processes the data it receives from the hardware driver and from applications writing to the device according to the requirements of a terminal on a Unix-like system. On input, it handles special characters such as the interrupt character (typically Control-C) and the erase and kill characters (typically backspace or delete, and Control-U, respectively) and, on output, it replaces all the LF characters with a CR/LF sequence.

 

A serial port could also be used for a dial-up Internet connection using a serial modem and PPP. In this case, a PPP line discipline would be used; it would accumulate input data from the serial line into PPP input packets, delivering them to the networking stack rather than to the character device, and would transmit packets delivered to it by the networking stack on the serial line.

 

Some Unix-like systems use STREAMS to implement line disciplines.

 

Problem 15. How to use file lock to achieve exclusive access?

Ans:

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <fcntl.h>

#include <errno.h>

 

const char *lock_file = "/tmp/LCK.test2";

 

int main() {

   int file_desc;

   int tries = 10;

 

   while (tries--) {

       file_desc = open(lock_file, O_RDWR | O_CREAT | O_EXCL, 0444);

       if (file_desc == -1) {

           printf("%d - Lock already present/n", getpid());

           sleep(3);

       }

       else {

               /* critical region */

           printf("%d - I have exclusive access/n", getpid());

           sleep(1);

           (void)close(file_desc);

           (void)unlink(lock_file);

               /* non-critical region */

           sleep(2);

       }

   } /* while */

   exit(EXIT_SUCCESS);

}

PS:

Creating lock files is fine for controlling exclusive access to resources such as serial ports, but isn't so good for access to large shared files.

原创粉丝点击