Disclosure of Invention
The embodiment of the invention provides a method, a device and equipment for marking a PHP process by using a process file system, which are used for solving the problem that the PHP process cannot be effectively marked in the prior art.
In one aspect, an embodiment of the present invention provides a method for marking a PHP process by using a process file system, including:
writing a marked variable into an environment variable before a program is started, reading the environment variable from a current process environment variable list before the statically marked program starts to run, reading a program process identifier from a marked variable field of the environment variable, and then starting the marked program process;
and aiming at the program needing dynamic marking, loading a preset program marking code in the program, opening a marking file with a specified file name in an appointed directory through the loaded program marking code, and searching a link of the marking file in a file list opened by the program process.
In some optional embodiments, the writing a flag variable in the environment variable before the program starts includes:
before a program is started, writing a mark variable in a key value form through a shell script; or writing a flag variable in a periodically executed work schedule crontab file; or writing the marking variable into the corresponding system daemon system configuration file.
In some optional embodiments, the loading of the preset program marker code in the program includes:
providing program marking code in advance, wherein the program marking code provides an Application Programming Interface (API) of dynamic marking and comprises at least one of creating marking and deleting marking;
and loading the program marking code provided in advance in the program.
In some optional embodiments, the method further includes:
when the marks of the program processes need to be searched, traversing all the program processes which accord with the set conditions, reading the environment variable file, and searching the program process marks written in advance;
and traversing all program processes which accord with the set conditions, reading links of the markup files of the appointed file names in the file list which is opened by the program processes, and finding the corresponding markup files of the appointed file names through the read links.
In some optional embodiments, the method further includes:
and according to the opened file list, carrying out file addressing offset operation on the opened file to find an offset address of the marked file.
The embodiment of the invention also provides a device for marking program processes by using the process file system, which comprises the following steps:
the static marking module is used for writing a marking variable into an environment variable before a program is started for the program needing the static marking, reading the environment variable from a current process environment variable list before the statically marked program starts to run, reading a program process identifier from a marking variable field of the environment variable, and then starting the marked program process;
and the dynamic marking module is used for loading a preset program marking code into the program aiming at the program needing dynamic marking, opening a marking file with a specified file name in the appointed directory through the loaded program marking code, and searching a link of the marking file in a file list opened by the program process.
In some optional embodiments, the static marking module is specifically configured to:
before a program is started, writing a mark variable in a key value form through a shell script; or writing a flag variable in a periodically executed work schedule crontab file; or writing the marking variable into the corresponding system daemon system configuration file.
In some optional embodiments, the dynamic marking module is specifically configured to:
providing program marking code in advance, wherein the program marking code provides an Application Programming Interface (API) of dynamic marking and comprises at least one of creating marking and deleting marking;
and loading the program marking code provided in advance in the program.
In some optional embodiments, the apparatus further comprises:
the identification acquisition module is used for traversing all the program processes meeting the set conditions when the identification of the program processes needs to be searched, reading the environment variable file and searching the pre-written program process identification; and traversing all program processes which accord with the set conditions, reading links of the markup files of the appointed file names in the file list which is opened by the program processes, and finding the corresponding markup files of the appointed file names through the read links.
In some optional embodiments, the dynamic marking module is further configured to:
and according to the opened file list, carrying out file addressing offset operation on the opened file to find an offset address of the marked file.
An embodiment of the present invention further provides a host device, including: means for marking program processes with a process file system as described above.
The embodiment of the invention also discloses a computer storage medium, wherein computer-executable instructions are stored in the computer storage medium and used for executing the method for marking the program process by using the process file system.
The technical scheme has the following beneficial effects:
the method can conveniently mark the program process without being limited by the type supported by the process, can effectively mark the process without controlling the ethnic group identification, does not need the log to carry out correlation query, is not influenced by log deletion and updating, has strong universality and convenient search, and can effectively mark and identify the process.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
In order to solve the problem that the program process cannot be effectively marked in the prior art, embodiments of the present invention provide a method for marking a program process by using a process file system, which can mark a program process by using procfs on linux, is not limited by types supported by the process, has strong versatility, is convenient to search, and can effectively mark and identify the process.
In many Unix-like computer systems, the process file system (procfs) contains a dummy file system that is dynamically generated at startup to access process information through the kernel. This file system is usually mounted to the/proc directory, since the/proc is not a real file system, it does not occupy storage space, but only limited memory. The present invention utilizes a process file system to mark program processes, which is described in detail below with reference to specific embodiments.
The method for marking program process by using process file system provided by the embodiment of the invention has the flow as shown in fig. 1, and comprises the following steps:
step S101: for programs that require static flags, a flag variable is written in an environment variable before the program starts.
Static marking can be conveniently and efficiently completed, and is not easily influenced by the program. Before a program is started, a mark variable in a key value form is written through a shell script, or the mark script is written in a periodically executed work schedule (crontab) file, or the mark variable is written in a corresponding system daemon (systemdemon, systemd) configuration file.
The mark variable written in the key value form through the shell script can be a bash script written in the environment variable in the key-value form.
For example: before the program starts, the process is marked using a bash script in the form of VAR _ promotion, or written directly in crontab. If the variable of VAR _ SOMEKEY exists in the environment variables, then the program to be marked is started in the environment, and the program to be marked which is just started can inherit the environment variables;
bash (Bourne-Again Shell) is a Unixshell written for the leather (GNU) program. The crontab command is commonly found in Unix and Unix-like operating systems and is used to set instructions that are executed periodically. This command reads the instruction from a standard input device and deposits it in a "crontab" file for later reading and execution.
Step S102: before the statically marked program starts to run, reading an environment variable from a current process environment variable list, reading a program process identifier from a marked variable field of the environment variable, and then starting the marked program process.
Before the marked program starts to run, the related environment variables can be read from the list/proc/< pid >/environ of the current process environment variables, and the program process identification is read from the mark variable field of the environment variables, wherein the identification parameter < pid > is replaced by the identification pid of the process. For example: if the pid replacing < pid > is 355, a file in the form of/proc/355/environ can take the aforementioned result. The marked program process is then restarted.
Step S103: and for the program needing dynamic marking, loading preset program marking codes into the program.
For a program needing to be dynamically marked, program marking codes are provided in advance, and the program marking codes provide an Application Programming Interface (API) of the dynamic marking and comprise at least one of creating marks and deleting marks; program markup code provided in advance is loaded in the program. The program markup code may be a PHP markup class.
That is, for a program requiring dynamic marking, a PHP class is provided and the program requiring marking loads such, and some APIs for providing dynamic marking such as creating and deleting marks are provided.
Step S104: and opening a markup file of a specified file name in the convention directory through the loaded program markup code, and searching a link of the markup file in a file list opened by the program process.
When the application needs to be marked, a directory with an unusual service can be appointed, and the aforementioned mark code (module) opens a file with a specific name in the directory, i.e. a link of the file can be found under/proc/< pid >/fd/, for example, the link of/tmp/nonunsedd form/abc _ social value can be opened.
The file addressing offset operation can be carried out on the opened file according to the opened file list, and the offset address of the marked file is found. That is, the offset address of the relevant markup file can be found according to the information of the opened file in the opened file list, such as: the opened file may be subjected to a file addressing offset operation to address to a specified address, i.e., an offset address of the markup file.
Unlike the prior art where the content is stored elsewhere, the file of a specific name proposed in the present application is only effective as its file name, and the file itself may not exist in the corresponding directory, and there is no need to write any content to the file itself. Therefore, the file itself does not need to be acquired, and only the name needs to be acquired, so that the operation is simple, the data storage amount is reduced, and the data transmission amount is also reduced.
Alternatively, the open file may be subjected to a file addressing offset operation to address a specified address. The offset address of the associated markup file can be found from the opened file information.
The method can mark the program process by adopting a static marking mode, can mark the program process by adopting a dynamic marking mode, or can combine the two modes. The method can effectively mark the program process and can ensure the corresponding relation between the mark and the process. The process that needs to be marked may be a PHP process or other program process.
The method for marking program process by using process file system provided by the embodiment of the invention also comprises a process of obtaining program process mark, the flow is shown as figure 2, and the method comprises the following steps:
step S201: and traversing all the program processes meeting the set conditions when the marks of the program processes need to be searched.
Because the mark information is written into the environment variable during the static mark, when the related mark needs to be searched, the program process is traversed to obtain the related mark variable information in the environment variable so as to search the program process mark.
For example: the program process meeting the set condition may be a certain mark process for specifying a certain user, and the set condition is that the process belongs to the user.
Step S202: and reading the environment variable file, and searching the program process identification written in advance.
The relevant program process is traversed, its environment variable (environ) is read, and the static mark written before starting can be read from environ.
For example: if the program uses a bash script markup in the form of VAR _ solution described in the foregoing static markup process, the contents in the form of VAR _ solution can be seen.
Another example is: if the program is marked in the way of crontab described in the static marking process, the crontab can only find the relevant content.
Step S203: and traversing all program processes meeting the set conditions.
When the dynamic marking is carried out, program marking codes are loaded in the program, so that the link of the marking file can be obtained by traversing the related program process meeting the set condition and reading the link in the fd directory. Where the fd directory contains a link to each file opened by the process.
Step S204: and reading the link of the markup file of the specified file name in the file list opened by the program process, and finding the corresponding markup file of the specified file name through the read link.
Links to the marker file can be found from the fd directory, i.e. if the program uses the markers as exemplified in the dynamic marker description above, links to/tmp/nonunseddormark/abc _ somevalue can be found.
Based on the same inventive concept, an embodiment of the present invention further provides an apparatus for marking a program process by using a process file system, where the apparatus may be disposed in a host device, and a structure of the apparatus is as shown in fig. 3, where the apparatus includes: a static marking module 301 and a dynamic marking module 302.
A static marking module 301, configured to, for a program that needs static marking, write a marking variable in an environment variable before the program is started, read the environment variable in a current process environment variable list before the statically marked program starts running, read a program process identifier from a marking variable field of the environment variable, and restart the marked program process;
the dynamic marking module 302 is configured to load a preset program marking code into a program for a program that needs dynamic marking, open a marked file with a specified file name in an appointed directory through the loaded program marking code, and search for a link of the marked file in a file list that has been opened by a program process.
Optionally, the static marking module 301 is specifically configured to:
before the program is started, a marked variable in a key value form is written in a shell script, or the marked variable is written in a periodically executed work schedule crontab file, or the marked variable is written in a corresponding system daemon system configuration file.
Optionally, the dynamic marking module 302 is specifically configured to:
providing program marking code in advance, wherein the program marking code provides an Application Programming Interface (API) of dynamic marking and comprises at least one of creating marking and deleting marking;
and loading the program marking code provided in advance in the program.
Optionally, the dynamic marking module 302 is further configured to: and according to the opened file information, carrying out file addressing offset operation on the opened file to find an offset address of the marked file.
Optionally, the apparatus further includes:
the identifier obtaining module 303 is configured to, when the identifier of the program process needs to be searched, traverse all the program processes that meet the set condition, read the environment variable file, and search for a pre-written program process identifier; and traversing all program processes which accord with the set conditions, reading links of the markup files of the appointed file names in the file list which is opened by the program processes, and finding the corresponding markup files of the appointed file names through the read links.
The embodiment of the invention also provides a computer storage medium, wherein computer-executable instructions are stored in the computer storage medium and are used for the method for marking the program process by using the process file system.
The method for marking the program process by using the process file system can realize that the program process is marked by using the procfs on linux, such as PHP process, and the related identification can be saved after fork and cannot be lost; the related identification does not affect the normal service; the method is convenient to use, can be used for programs of various languages, can process related marks by using multiple languages, and can be easily added into the crontab to enable the program to hold a specific mark when being started.
If the fork function in the fork function computer program design is successfully called once, two values are returned, the child process returns 0, and the parent process returns a child process mark; otherwise, the error returns to-1. The fork function divides the running program into 2 (almost) identical processes, each of which starts a thread that starts from the same location in the code. The threads in the two processes continue to execute as if two users started two copies of the application at the same time.
Those of skill in the art will further appreciate that the various illustrative logical blocks, units, and steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware, computer software, or combinations of both. To clearly illustrate the interchangeability of hardware and software, various illustrative components, elements, and steps have been described above generally in terms of their functionality. Whether such functionality is implemented as hardware or software depends upon the particular application and design requirements of the overall system. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present embodiments.
The various illustrative logical blocks, or elements, described in connection with the embodiments disclosed herein may be implemented or performed with a general purpose processor, a digital signal processor, an Application Specific Integrated Circuit (ASIC), a field programmable gate array or other programmable logic device, discrete gate or transistor logic, discrete hardware components, or any combination thereof designed to perform the functions described herein. A general-purpose processor may be a microprocessor, but in the alternative, the processor may be any conventional processor, controller, microcontroller, or state machine. A processor may also be implemented as a combination of computing devices, e.g., a digital signal processor and a microprocessor, a plurality of microprocessors, one or more microprocessors in conjunction with a digital signal processor core, or any other similar configuration.
The steps of a method or algorithm described in connection with the embodiments disclosed herein may be embodied directly in hardware, in a software module executed by a processor, or in a combination of the two. A software module may be stored in RAM memory, flash memory, ROM memory, EPROM memory, EEPROM memory, registers, hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art. For example, a storage medium may be coupled to the processor such the processor can read information from, and write information to, the storage medium. In the alternative, the storage medium may be integral to the processor. The processor and the storage medium may reside in an ASIC, which may be located in a user terminal. In the alternative, the processor and the storage medium may reside in different components in a user terminal.
In one or more exemplary designs, the functions described above in connection with the embodiments of the invention may be implemented in hardware, software, firmware, or any combination of the three. If implemented in software, the functions may be stored on or transmitted over as one or more instructions or code on a computer-readable medium. Computer-readable media includes both computer storage media and communication media that facilitate transfer of a computer program from one place to another. Storage media may be any available media that can be accessed by a general purpose or special purpose computer. For example, such computer-readable media can include, but is not limited to, RAM, ROM, EEPROM, CD-ROM or other optical disk storage, magnetic disk storage or other magnetic storage devices, or any other medium which can be used to carry or store program code in the form of instructions or data structures and which can be read by a general-purpose or special-purpose computer, or a general-purpose or special-purpose processor. Additionally, any connection is properly termed a computer-readable medium, and, thus, is included if the software is transmitted from a website, server, or other remote source via a coaxial cable, fiber optic cable, twisted pair, Digital Subscriber Line (DSL), or wirelessly, e.g., infrared, radio, and microwave. Such discs (disk) and disks (disc) include compact disks, laser disks, optical disks, DVDs, floppy disks and blu-ray disks where disks usually reproduce data magnetically, while disks usually reproduce data optically with lasers. Combinations of the above may also be included in the computer-readable medium.
The above-mentioned embodiments are intended to illustrate the objects, technical solutions and advantages of the present invention in further detail, and it should be understood that the above-mentioned embodiments are merely exemplary embodiments of the present invention, and are not intended to limit the scope of the present invention, and any modifications, equivalent substitutions, improvements and the like made within the spirit and principle of the present invention should be included in the scope of the present invention.