kylin-process-manager-4.0.0.0/0000755000175000017500000000000014552362517015323 5ustar debiandebiankylin-process-manager-4.0.0.0/LICENSE0000644000175000017500000010575514536025301016332 0ustar debiandebian GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . kylin-process-manager-4.0.0.0/CMakeLists.txt0000644000175000017500000000072614536025312020057 0ustar debiandebiancmake_minimum_required(VERSION 3.14) project(kylin-process-manager LANGUAGES CXX) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_subdirectory(process-manager) add_subdirectory(process-manager-daemon) set(QAPPLICATION_CLASS QCoreApplication CACHE STRING "Inheritance class for SingleApplication") add_subdirectory(third-party/SingleApplication) kylin-process-manager-4.0.0.0/process-manager-daemon/0000755000175000017500000000000014536223276021652 5ustar debiandebiankylin-process-manager-4.0.0.0/process-manager-daemon/configs/0000755000175000017500000000000014536025312023271 5ustar debiandebiankylin-process-manager-4.0.0.0/process-manager-daemon/configs/.gitkeep0000644000175000017500000000000014536025312024710 0ustar debiandebiankylin-process-manager-4.0.0.0/process-manager-daemon/configs/com.kylin.ProcessManagerDaemon.xml0000644000175000017500000000666214536025312031764 0ustar debiandebian kylin-process-manager-4.0.0.0/process-manager-daemon/configs/com.kylin.ProcessManagerDaemon.conf0000644000175000017500000000115414536025312032100 0ustar debiandebian kylin-process-manager-4.0.0.0/process-manager-daemon/configs/kylin-process-manager.json0000644000175000017500000000204414536025312030376 0ustar debiandebian{ "features": { "memory": true, "io": true, "cpu": true, "frozen": true, "reclaim": true }, "rulesets": [ { "detectors": [ { "resource": "Memory", "threshold": [1000, 750, 500], "normal": 600, "api": "/proc/meminfo", "effectiveNumber": 5 }, { "resource": "CPU", "threshold": [30, 40, 50], "normal": 10, "avg": "10", "api": "/proc/pressure/cpu", "effectiveNumber": 5 }, { "resource": "IO", "threshold": [50, 60, 70], "normal": 10, "avg": "10", "api": "/proc/pressure/io", "effectiveNumber": 5 } ] } ] }kylin-process-manager-4.0.0.0/process-manager-daemon/configs/kylin-process-manager-cleaner.service0000644000175000017500000000040014536025312032466 0ustar debiandebian[Unit] Description=Thaw all processes before shutdown DefaultDependencies=no Conflicts=shutdown.target Before=shutdown.target [Service] Type=oneshot ExecStart=/usr/bin/kylin-process-manager-daemon -t TimeoutStartSec=0 [Install] WantedBy=multi-user.targetkylin-process-manager-4.0.0.0/process-manager-daemon/configs/kylin-process-manager-daemon.service0000644000175000017500000000033114536025312032323 0ustar debiandebian[Unit] Description=kylin process manager daemon After=display-manager.service [Service] Type=dbus BusName=com.kylin.ProcessManager ExecStart=/usr/bin/kylin-process-manager-daemon [Install] WantedBy=multi-user.targetkylin-process-manager-4.0.0.0/process-manager-daemon/configs/com.kylin.ProcessManagerDaemon.service0000644000175000017500000000022514536025312032611 0ustar debiandebian[D-BUS Service] Name=com.kylin.ProcessManagerDaemon Exec=/usr/bin/kylin-process-manager-daemon User=root SystemdService=kylin-process-manager.servicekylin-process-manager-4.0.0.0/process-manager-daemon/pressurewatcher.cpp0000644000175000017500000001256314536025312025602 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "pressurewatcher.h" #include #include #include #include #include #include #include #include #include "common.h" PressureWatcher::PressureWatcher(QObject *parent) : ResourceWatcher{parent} , m_stop(false) { initTriggers(); } void PressureWatcher::start() { if (m_triggers.isEmpty()) { qWarning() << "PressureWatcher: pressure triggers is empty."; return; } struct epoll_event events[1024]; int epfd = epoll_create(1); if (epfd < 0) { qWarning() << "epoll_create failed, " << strerror(errno); return; } qDebug() << "m_triggers" << m_triggers.first().resource; for (auto &trigger : m_triggers) { int fd = open(trigger.fileInterface.toLocal8Bit().data(), O_RDWR | O_NONBLOCK); if (fd < 0) { qWarning() << "open file " << trigger.fileInterface << " failed, " << strerror(errno); continue; } QByteArray ba = trigger.trigger.toLocal8Bit(); const char *trigger2write = ba.data(); if (write(fd, trigger2write, strlen(trigger2write) + 1) < 0) { qWarning() << "write failed, " << strerror(errno); continue; } epoll_event ev; ev.data.fd = fd; ev.events = EPOLLPRI; ev.events |= EPOLLET; int result = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); if (result < 0) { qWarning() << "epoll_ctl failed, " << strerror(errno); continue; } trigger.fd = fd; } while (!m_stop) { qDebug() << "epoll wait "; int n = epoll_wait(epfd, events, 1024, -1); qDebug() << "epoll wait " << n; QMap triggersUrgency; for (int i=0; i::GetInstance(); auto initTrigger = [this, &confMgr](ConfManager::Resource resouce) { auto trigger = confMgr.pressureTriggers(resouce); auto triIt = trigger.constBegin(); QMetaEnum meta = QMetaEnum::fromType(); QString strResouce(meta.valueToKey(resouce)); QString fileInterface = QString("/proc/pressure/%1").arg(strResouce.toLower()); while (triIt != trigger.constEnd()) { m_triggers.push_back({ resouce, triIt.key(), triIt.value(), fileInterface, -1 }); ++ triIt; } }; auto pressureResources = confMgr.pressureResouces(); for (const auto &resource : pressureResources) { initTrigger(resource); } } PressureWatcher::PressureTrigger PressureWatcher::trigger(int fd) { for (const auto &trigger : qAsConst(m_triggers)) { if (trigger.fd == fd) { return trigger; } } return PressureWatcher::PressureTrigger {}; } void PressureWatcher::handleWarningMessage(QMap triggerUrgency) { ConfManager &confMgr = common::Singleton::GetInstance(); QMetaEnum meta = QMetaEnum::fromType(); auto it = triggerUrgency.constBegin(); while (it != triggerUrgency.constEnd()) { int level = confMgr.resourceUrgencyIndex(it.value()); if (level == -1) { qWarning() << "Get urgency index error, urgency is" << it.value(); ++ it; continue; } ++ level; qDebug() << "level " << level << it.key(); switch (it.key()) { case ConfManager::CPU: Q_EMIT ResourceThresholdWarning("CPU", level); break; case ConfManager::IO: Q_EMIT ResourceThresholdWarning("IO", level); break; case ConfManager::Memory: Q_EMIT ResourceThresholdWarning("Memory", level); break; } ++ it; } } kylin-process-manager-4.0.0.0/process-manager-daemon/systemresourcewatcher.h0000644000175000017500000000246114536247754026507 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef SYSTEMRESOURCEWATCHER_H #define SYSTEMRESOURCEWATCHER_H #include #include #include #include #include "resourcewatcher.h" #include "confmanager.h" class QTimer; class SystemResourceWatcher : public ResourceWatcher { Q_OBJECT public: explicit SystemResourceWatcher(QObject *parent = nullptr); public Q_SLOTS: void start(); void stop(); private Q_SLOTS: void detectMemory(); private: unsigned long getAvailableMem(); private: bool m_stop; QTimer *m_timer; ConfManager::ResourceUrgencyThrehold m_resourceThrehold; }; #endif // SYSTEMRESOURCEWATCHER_H kylin-process-manager-4.0.0.0/process-manager-daemon/LICENSE0000644000175000017500000010575514536025312022663 0ustar debiandebian GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . kylin-process-manager-4.0.0.0/process-manager-daemon/processmanager.h0000644000175000017500000000417514536025312025032 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef PROCESSMANAGER_H #define PROCESSMANAGER_H #include class AppManagerCgroupd; class ResourceWatcher; class ProcessReclaimer; class ProcessManager : public QObject { Q_OBJECT public: explicit ProcessManager(QObject *parent = nullptr); void startWatchers(); void thawAllProcesses(bool walk = false); QMap InitCGroup(const QString &rootPath); QMap CreateProcessCGroup(const QString &appId, int pid); QMap CreateProcessCGroup(const QString &appId, const QList &pids); QMap DeleteProcessCGroup(const QString &cgroupName); QMap SetProcessCGroupResourceLimit(const QString &cgroupName, const QString &attrName, int value); QMap CGroupNameWithPid(int pid); QMap PidsOfCGroup(const QString &cgroupName); QMap ReclaimProcesses(const QList &pids); QMap ReclaimProcesses(const QString &cgroup); QStringList Features() const; Q_SIGNALS: void ResourceThresholdWarning(const QString &resource, int level); private: void startWatcherThread(ResourceWatcher *watcher, QThread *thread); private: AppManagerCgroupd *m_appMgrCgroupd; ProcessReclaimer *m_processReclaimer; }; #endif // PROCESSMANAGER_H kylin-process-manager-4.0.0.0/process-manager-daemon/processreclaimer.cpp0000644000175000017500000000410714536025312025711 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "processreclaimer.h" #include #include #include #include ProcessReclaimer::ProcessReclaimer(QObject *parent) : QObject{parent}, m_isAvailable(false) { if (access("/proc/1/reclaim",F_OK) != -1) { m_isAvailable = true; } } bool ProcessReclaimer::isAvailable() { return access("/proc/1/reclaim", F_OK) != -1; } common::DBusResult ProcessReclaimer::reclaimProcesses(const QList pids) { auto result = common::initDbusResult(); if (pids.isEmpty()) { result[common::kDbusErrMsg] = "The pids is empty!"; result[common::kDbusResult] = false; return result; } const char *content = "all"; for (auto const &pid : qAsConst(pids)) { QString api = QString("/proc/%1/reclaim").arg(pid); QByteArray ba = api.toLocal8Bit(); int fd = open(ba.data(), O_RDWR | O_NONBLOCK); if (fd < 0) { result[common::kDbusErrMsg] = "Open api file " + api + " failed, " + strerror(errno); result[common::kDbusResult] = false; return result; } if (write(fd, content, strlen(content) + 1) < 0) { result[common::kDbusErrMsg] = "Write api file " + api + " failed, " + strerror(errno); result[common::kDbusResult] = false; return result; } } result[common::kDbusResult] = true; return result; } kylin-process-manager-4.0.0.0/process-manager-daemon/confmanager.cpp0000644000175000017500000001530014536025312024624 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "confmanager.h" #include #include #include #include #include #include #include ConfManager::ConfManager(QObject *parent) : QObject{parent} { confFileWatcher = new QFileSystemWatcher(QStringList() << kConfFilename, this); connect(confFileWatcher, &QFileSystemWatcher::fileChanged, this, &ConfManager::readConf); readConf(); } QStringList ConfManager::features() const { return m_features; } QList ConfManager::pressureResouces() const { QList resouces = m_pressureTriggers.keys(); for (auto &resource : qAsConst(resouces)) { switch (resource) { case CPU: if (!m_features.contains("cpu")) { resouces.removeOne(resource); } break; case IO: if (!m_features.contains("io")) { resouces.removeOne(resource); } break; case Memory: if (!m_features.contains("memory")) { resouces.removeOne(resource); } break; } } return resouces; } QMap ConfManager::pressureTriggers(Resource resource) const { if (!m_pressureTriggers.contains(resource)) { return QMap(); } QMetaEnum meta = QMetaEnum::fromType(); ResourceUrgencyThrehold threholds = m_pressureTriggers.value(resource); if (threholds.size() != meta.keyCount()) { return QMap(); } QMap result; for (int i=0; i(); for (int i=0; i(); QMetaEnum resouceMeta = QMetaEnum::fromType(); if (thresholds.size() != urgencyMeta.keyCount()) { qWarning() << "Config file thresholds error, " << thresholds << " " << urgencyMeta.keyCount(); return; } int enumValue = resouceMeta.keyToValue(resource.toLocal8Bit().data()); bool isResourcePressure = api.startsWith("/proc/pressure") ? true : false; QMap resources; for (int i=0; i. */ #ifndef PRESSUREWATCHER_H #define PRESSUREWATCHER_H #include #include #include #include "resourcewatcher.h" #include "confmanager.h" class PressureWatcher : public ResourceWatcher { Q_OBJECT public: explicit PressureWatcher(QObject *parent = nullptr); public Q_SLOTS: void start() override; void stop() override; Q_SIGNALS: void finished(); private: struct PressureTrigger { ConfManager::Resource resource; ConfManager::ResourceUrgency urgency; QString trigger; QString fileInterface; int fd = -1; }; private: void initTriggers(); PressureTrigger trigger(int fd); void handleWarningMessage(QMap triggerUrgency); private: bool m_stop; QList m_triggers; QMap m_resourceThreholds; }; #endif // PRESSUREWATCHER_H kylin-process-manager-4.0.0.0/process-manager-daemon/appmanagercgroupd.cpp0000644000175000017500000003330314536025312026046 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "appmanagercgroupd.h" #include #include #include #include #include #include #include #include "common.h" #include "confmanager.h" #define CPU_MAX "cpu.max" #define CGROUP_FREEZE "cgroup.freeze" AppManagerCgroupd::AppManagerCgroupd(QObject *parent) : QObject{parent} , m_cgroupInited(false) { qDBusRegisterMetaType>(); } QList AppManagerCgroupd::pids(const QString &cgroupName) { int size = 0; int ret = 0; const char *controller = "pids"; int retNumber = 0; QList result; pid_t *pids = nullptr; QByteArray ba = cgroupName.toLocal8Bit(); retNumber = cgroup_get_procs(ba.data(), (char *)controller, &pids, &size); if (retNumber != 0) { qWarning() << "cgroup_get_procs error, " << cgroup_strerror(cgroup_get_last_errno()); return QList(); } if (size == 0 || !pids) { return QList(); } for (int i=0; itype == CGROUP_FILE_TYPE_DIR) { printf("path %s, parent %s, relative %s, full %s\n", info->path, info->parent, info->full_path + strlen(root) - 1, info->full_path); QString cgroupName = QString(info->full_path + strlen(root) - 1).prepend("kylin-app-manager"); if (cgroupName.contains("instance_")) { SetProcessCGroupResourceLimit(cgroupName, CGROUP_FREEZE, 0); } } } void AppManagerCgroupd::thawAllProcesses(bool walk) { if (!walk) { for (auto &cgroupName : qAsConst(m_frozenCGroups)) { SetProcessCGroupResourceLimit(cgroupName, CGROUP_FREEZE, 0); } return; } struct cgroup_file_info info; char root[FILENAME_MAX]; char *controller; void *handle; int lvl, i; int ret; ret = cgroup_init(); if (ret != 0) { fprintf(stderr, "Init failed\n"); exit(EXIT_FAILURE); } ret = cgroup_walk_tree_begin("cgroup", "kylin-app-manager", 0, &handle, &info, &lvl); if (ret != 0) { fprintf(stderr, "Walk failed\n"); exit(EXIT_FAILURE); } strcpy(root, info.full_path); printf("Begin pre-order walk\n"); printf("root is %s\n", root); thawProcess(&info, root); while ((ret = cgroup_walk_tree_next(0, &handle, &info, lvl)) != ECGEOF) { thawProcess(&info, root); } cgroup_walk_tree_end(&handle); } QMap AppManagerCgroupd::InitCGroup(const QString &rootPath) { return { { kResult, initCgroup(rootPath) }, { kErrMsg, m_cgroupInitErrMsg } }; } QMap AppManagerCgroupd::CreateProcessCGroup(const QString &appId, int pid) { auto result = initResult(); if (!m_cgroupInited) { result[kErrMsg] = m_cgroupInitErrMsg; return result; } if (appId.isEmpty() || pid <=0) { result[kErrMsg] = "The parameter is invalid"; return result; } cgroup *appIdCgroup = createCgroup(m_rootCgroup + "/" + appId); if (!appIdCgroup) { result[kErrMsg] = cgroup_strerror(cgroup_get_last_errno()); return result; } cgroup_free(&appIdCgroup); QString retCgroupName = m_rootCgroup + "/" + appId + "/instance_" + QString::number(pid); cgroup *appInstCgroup = createCgroup(retCgroupName, kControllerNames); if (!appInstCgroup) { result[kErrMsg] = cgroup_strerror(cgroup_get_last_errno()); return result; } int ret = cgroup_attach_task_pid(appInstCgroup, pid); if (ret != 0) { result[kErrMsg] = cgroup_strerror(ret); cgroup_free(&appInstCgroup); return result; } cgroup_free(&appInstCgroup); result[kResult] = retCgroupName; m_createdCGroupNames.push_back(retCgroupName); return result; } QMap AppManagerCgroupd::CreateProcessCGroup(const QString &appId, const QList &pids) { auto result = initResult(); if (!m_cgroupInited) { result[kErrMsg] = m_cgroupInitErrMsg; return result; } if (appId.isEmpty() || pids.isEmpty()) { result[kErrMsg] = "The parameter is invalid"; return result; } cgroup *appIdCgroup = createCgroup(m_rootCgroup + "/" + appId); if (!appIdCgroup) { result[kErrMsg] = cgroup_strerror(cgroup_get_last_errno()); return result; } cgroup_free(&appIdCgroup); QString retCgroupName = m_rootCgroup + "/" + appId + "/instance_" + QString::number(pids.first()); cgroup *appInstCgroup = createCgroup(retCgroupName, kControllerNames); if (!appInstCgroup) { result[kErrMsg] = cgroup_strerror(cgroup_get_last_errno()); return result; } for (auto const &pid : pids) { int ret = cgroup_attach_task_pid(appInstCgroup, pid); if (ret != 0) { result[kErrMsg] = cgroup_strerror(ret); cgroup_free(&appInstCgroup); return result; } } cgroup_free(&appInstCgroup); result[kResult] = retCgroupName; m_createdCGroupNames.push_back(retCgroupName); return result; } QMap AppManagerCgroupd::DeleteProcessCGroup(const QString &cgroupName) { auto result = initResult(); int retNumber = 0; cgroup *cgroup = cgroup_new_cgroup(cgroupName.toLatin1().data()); if (!cgroup) { result[kResult] = false; result[kErrMsg] = cgroup_strerror(cgroup_get_last_errno()); qWarning() << "DeleteProcessCGroup error " << result[kErrMsg]; return result; } retNumber = cgroup_get_cgroup(cgroup); if (retNumber != 0) { result[kResult] = false; result[kErrMsg] = cgroup_strerror(retNumber); qWarning() << "DeleteProcessCGroup cgroup_get_cgroup error " << result[kErrMsg]; return result; } int deleteCounter = 0; do { retNumber = cgroup_delete_cgroup_ext(cgroup, CGFLAG_DELETE_RECURSIVE); qApp->processEvents(); usleep(10000); ++ deleteCounter; } while (retNumber == ECGOTHER && deleteCounter <= 1000); if (retNumber != 0) { result[kResult] = false; result[kErrMsg] = cgroup_strerror(retNumber); qWarning() << "DeleteProcessCGroup cgroup_delete_cgroup error " << result[kErrMsg] << retNumber; return result; } result[kResult] = true; m_createdCGroupNames.removeOne(cgroupName); return result; } QMap AppManagerCgroupd::SetProcessCGroupResourceLimit(const QString &cgroupName, const QString &attrName, int value) { auto result = initResult(); int retNumber = 0; auto &config = common::Singleton::GetInstance(); if (attrName == CGROUP_FREEZE && !config.features().contains(FEAT_FROZEN) && value == 1) { result[kErrMsg] = "The 'frozen' feature is not enabled."; result[kResult] = false; return result; } if (!m_cgroupInited) { result[kErrMsg] = m_cgroupInitErrMsg; result[kResult] = false; return result; } if (cgroupName.isEmpty() || attrName.isEmpty() || attrName.split(".").size() < 2) { result[kErrMsg] = "The parameter is invalid"; result[kResult] = false; return result; } cgroup *cgroup = cgroup_new_cgroup(cgroupName.toLocal8Bit().data()); if (!cgroup) { qWarning() << cgroup_get_last_errno(); } QString resourceName = attrName.split(".").first(); QByteArray ba = resourceName.toLocal8Bit(); cgroup_controller *controller = cgroup_add_controller(cgroup, ba.data()); if (!controller) { result[kErrMsg] = cgroup_strerror(cgroup_get_last_errno()); result[kResult] = false; cgroup_free(&cgroup); qWarning() << "cgroup_get_controller failed, " << result[kErrMsg] << attrName.toLocal8Bit().data() << ba.data() << value; return result; } if (attrName == CPU_MAX) { QString strVale = value == 100000 ? QString("max 100000"): QString("%1 100000").arg(value); retNumber = cgroup_set_value_string(controller, attrName.toLocal8Bit().data(), strVale.toLocal8Bit().data()); } else { retNumber = cgroup_set_value_int64(controller, attrName.toLocal8Bit().data(), value); } if (retNumber != 0) { result[kErrMsg] = cgroup_strerror(retNumber); result[kResult] = false; cgroup_free_controllers(cgroup); cgroup_free(&cgroup); qWarning() << "cgroup_add_value failed, " << cgroup_strerror(retNumber) << attrName.toLocal8Bit().data() << value; return result; } retNumber = cgroup_modify_cgroup(cgroup); if (retNumber != 0) { result[kErrMsg] = cgroup_strerror(retNumber); result[kResult] = false; cgroup_free_controllers(cgroup); cgroup_free(&cgroup); qWarning() << "cgroup_modify_cgroup failed, " << cgroup_strerror(retNumber) << attrName.toLocal8Bit().data() << value; return result; } if (attrName == CGROUP_FREEZE) { if (value == 1) { m_frozenCGroups.push_back(cgroupName); } else { m_frozenCGroups.removeOne(cgroupName); } } cgroup_free_controllers(cgroup); cgroup_free(&cgroup); result[kResult] = true; return result; } QMap AppManagerCgroupd::CGroupNameWithPid(int pid) { int size = 0; int ret = 0; const char *controller = "pids"; auto result = initResult(); int retNumber = 0; for (auto const &cgroupName : qAsConst(m_createdCGroupNames)) { pid_t *pids = nullptr; QByteArray ba = cgroupName.toLocal8Bit(); retNumber = cgroup_get_procs(ba.data(), (char *)controller, &pids, &size); if (retNumber != 0) { qWarning() << "cgroup_get_procs error, " << cgroup_strerror(cgroup_get_last_errno()); continue; } if (size == 0 || !pids) { continue; } for (int i=0; i AppManagerCgroupd::PidsOfCGroup(const QString &cgroupName) { pid_t *pids = nullptr; const char *controller = "pids"; int size = 0; auto result = initResult(); QList retPids; QByteArray ba = cgroupName.toLocal8Bit(); int retNumber = cgroup_get_procs(ba.data(), (char *)controller, &pids, &size); if (retNumber != 0) { result[kErrMsg] = cgroup_strerror(retNumber); qWarning() << "cgroup_get_procs error, " << result[kErrMsg]; return result; } if (size == 0 || !pids) { return result; } for (int i=0; i>(retPids); delete[] pids; return result; } bool AppManagerCgroupd::initCgroup(const QString &rootPath) { if (m_cgroupInited && m_rootCgroup == rootPath) { return true; } auto initFailed = [this] { int errorNo = cgroup_get_last_errno(); m_cgroupInitErrMsg = cgroup_strerror(errorNo); m_cgroupInited = false; qWarning() << "Cgroup init failed, " << m_cgroupInitErrMsg; }; if (cgroup_init() != 0) { initFailed(); return false; } cgroup *rootCgroup = createCgroup(rootPath, kControllerNames); if (!rootCgroup) { initFailed(); return false; } m_cgroupInited = true; m_rootCgroup = rootPath; qDebug() << "init cgroup success. path is " << rootPath; return true; } cgroup *AppManagerCgroupd::createCgroup(const QString &cgroupName, const QStringList &controllers) { cgroup *cgroup = cgroup_new_cgroup(cgroupName.toLocal8Bit().data()); if (!cgroup) { return nullptr; } if (!controllers.isEmpty()) { for (auto const &controlName : qAsConst(controllers)) { cgroup_controller *controller = cgroup_add_controller(cgroup, controlName.toLocal8Bit().data()); if (!controller) { qWarning() << "cgroup_add_controller failed " << cgroup_strerror(cgroup_get_last_errno()); cgroup_free(&cgroup); return nullptr; } } } if (cgroup_create_cgroup(cgroup, 0) != 0) { cgroup_free_controllers(cgroup); cgroup_free(&cgroup); return nullptr; } return cgroup; } QMap AppManagerCgroupd::initResult() { QMap result; result.insert(kResult, ""); result.insert(kResult, ""); return result; } kylin-process-manager-4.0.0.0/process-manager-daemon/common.h0000644000175000017500000000261414536025312023305 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef COMMON_H #define COMMON_H #include #include #include #define CGROUP_FREEZE "cgroup.freeze" namespace common { typedef QMap DBusResult; static const char *kDbusResult = "result"; static const char *kDbusErrMsg = "errorMessage"; static DBusResult initDbusResult() { DBusResult result; result[kDbusResult] = ""; result[kDbusErrMsg] = ""; return result; } template class Singleton { public: static T& GetInstance() { static T instance; return instance; } protected: virtual ~Singleton() {} Singleton() {} Singleton(const Singleton&) {} Singleton& operator =(const Singleton&) {} }; } #endif // COMMON_H kylin-process-manager-4.0.0.0/process-manager-daemon/processmanager.cpp0000644000175000017500000001025514536025312025361 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "processmanager.h" #include #include #include "appmanagercgroupd.h" #include "pressurewatcher.h" #include "systemresourcewatcher.h" #include "processreclaimer.h" #include "confmanager.h" #include "common.h" ProcessManager::ProcessManager(QObject *parent) : QObject{parent}, m_appMgrCgroupd(new AppManagerCgroupd()), m_processReclaimer(new ProcessReclaimer(this)) { startWatchers(); } void ProcessManager::startWatchers() { QThread *pressureWatcherThread = new QThread(this); PressureWatcher *pressureWatcher = new PressureWatcher; startWatcherThread(pressureWatcher, pressureWatcherThread); if (common::Singleton::GetInstance().features().contains("memory")) { QThread *memResourceWatcheThread = new QThread(this); SystemResourceWatcher *resourceWatcher = new SystemResourceWatcher; startWatcherThread(resourceWatcher, memResourceWatcheThread); } } void ProcessManager::thawAllProcesses(bool walk) { m_appMgrCgroupd->thawAllProcesses(walk); } QMap ProcessManager::InitCGroup(const QString &rootPath) { return m_appMgrCgroupd->InitCGroup(rootPath); } QMap ProcessManager::CreateProcessCGroup(const QString &appId, int pid ) { return m_appMgrCgroupd->CreateProcessCGroup(appId, pid); } QMap ProcessManager::CreateProcessCGroup(const QString &appId, const QList &pids) { return m_appMgrCgroupd->CreateProcessCGroup(appId, pids); } QMap ProcessManager::DeleteProcessCGroup(const QString &cgroupName) { return m_appMgrCgroupd->DeleteProcessCGroup(cgroupName); } QMap ProcessManager::SetProcessCGroupResourceLimit(const QString &cgroupName, const QString &attrName, int value) { return m_appMgrCgroupd->SetProcessCGroupResourceLimit(cgroupName, attrName, value); } QMap ProcessManager::CGroupNameWithPid(int pid) { return m_appMgrCgroupd->CGroupNameWithPid(pid); } QMap ProcessManager::ReclaimProcesses(const QList &pids) { return m_processReclaimer->reclaimProcesses(pids); } QMap ProcessManager::ReclaimProcesses(const QString &cgroup) { auto pids = m_appMgrCgroupd->pids(cgroup); return m_processReclaimer->reclaimProcesses(pids); } QMap ProcessManager::PidsOfCGroup(const QString &cgroupName) { return m_appMgrCgroupd->PidsOfCGroup(cgroupName); } QStringList ProcessManager::Features() const { ConfManager &confManager = common::Singleton::GetInstance(); QStringList confFeatures = confManager.features(); if (confFeatures.contains("reclaim") && !ProcessReclaimer::isAvailable()) { confFeatures.removeOne("reclaim"); } return confFeatures; } void ProcessManager::startWatcherThread(ResourceWatcher *watcher, QThread *thread) { if (!watcher || !thread) { return; } watcher->moveToThread(thread); connect(thread, &QThread::started, watcher, &ResourceWatcher::start); connect(thread, &QThread::finished, thread, &QThread::deleteLater); connect(thread, &QThread::finished, watcher, &ResourceWatcher::deleteLater); connect(watcher, &ResourceWatcher::finished, thread, &QThread::quit); connect(watcher, &ResourceWatcher::ResourceThresholdWarning, this, &ProcessManager::ResourceThresholdWarning); thread->start(); } kylin-process-manager-4.0.0.0/process-manager-daemon/main.cpp0000644000175000017500000000464214536256644023315 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include #include #include #include #include #include "singleapplication.h" #include "processmanager.h" #include "processmanagerservice.h" #include "appmanagercgroupd.h" int main(int argc, char *argv[]) { SingleApplication app(argc, argv, true); QCommandLineParser parser; parser.setApplicationDescription("Kylin Process Manager"); parser.addHelpOption(); parser.addVersionOption(); QCommandLineOption thawProcessesOption("t", QCoreApplication::translate("main", "Thaw all frozen processes")); parser.addOption(thawProcessesOption); parser.process(app); bool thawProcesses = parser.isSet(thawProcessesOption); qWarning() << "app.isSecondary())" << app.isSecondary() << thawProcesses; // If this is a secondary instance if (app.isSecondary()) { if (thawProcesses) { app.sendMessage("thawProcesses"); } return 0; } if (thawProcesses) { AppManagerCgroupd cgroupManager; cgroupManager.thawAllProcesses(true); return 0; } ProcessManager processManager; QDBusConnection connection = QDBusConnection::systemBus(); if (!connection.registerService("com.kylin.ProcessManager") || !connection.registerObject("/com/kylin/ProcessManager", &processManager)) { qWarning() << "register dbus service failed" << connection.lastError(); } ProcessManagerService service(&processManager); Q_UNUSED(service) QObject::connect(&app, &SingleApplication::receivedMessage, &app, [&processManager] (quint32 instanceId, QByteArray message) { Q_UNUSED(instanceId) processManager.thawAllProcesses(); }); return app.exec(); } kylin-process-manager-4.0.0.0/process-manager-daemon/resourcewatcher.h0000644000175000017500000000233014536025312025215 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef RESOURCEWATCHER_H #define RESOURCEWATCHER_H #include #include class ResourceWatcher : public QObject { Q_OBJECT public: explicit ResourceWatcher(QObject *parent = nullptr); public Q_SLOTS: virtual void start() = 0; virtual void stop() = 0; signals: void finished(); void ResourceThresholdWarning(const QString &resource, int level); protected: bool thresholdWarning(int urgency); protected: int m_detectEffectiveNumber; QQueue m_detectValues; }; #endif // RESOURCEWATCHER_H kylin-process-manager-4.0.0.0/process-manager-daemon/resourcewatcher.cpp0000644000175000017500000000234214536025312025553 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "resourcewatcher.h" ResourceWatcher::ResourceWatcher(QObject *parent) : QObject{parent} , m_detectEffectiveNumber(5) { } bool ResourceWatcher::thresholdWarning(int urgency) { m_detectValues.enqueue(urgency); if (m_detectValues.size() > m_detectEffectiveNumber) { m_detectValues.dequeue(); } if (m_detectValues.size() < m_detectEffectiveNumber) { return false; } int result = m_detectValues.first(); for (const auto &val : m_detectValues) { result &= val; } return result; } kylin-process-manager-4.0.0.0/process-manager-daemon/appmanagercgroupd.h0000644000175000017500000000517514536025312025521 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef APPMANAGERCGROUPD_H #define APPMANAGERCGROUPD_H #include #include #include struct cgroup; class SystemResourceWatcher; class AppManagerCgroupd : public QObject { Q_OBJECT public: explicit AppManagerCgroupd(QObject *parent = nullptr); QList pids(const QString &cgroupName); void thawAllProcesses(bool walk = false); public Q_SLOTS: QMap InitCGroup(const QString &rootPath); QMap CreateProcessCGroup(const QString &appId, int pid); QMap CreateProcessCGroup(const QString &appId, const QList &pids); QMap DeleteProcessCGroup(const QString &cgroupName); QMap SetProcessCGroupResourceLimit(const QString &cgroupName, const QString &attrName, int value); QMap CGroupNameWithPid(int pid); QMap PidsOfCGroup(const QString &cgroupName); Q_SIGNALS: void ResourceThresholdWarning(const QString &resource, const QString &shareTime, const QString &threshold, const QString &avg); private: bool initCgroup(const QString &rootPath); cgroup *createCgroup(const QString &cgroupName, const QStringList &controllers = QStringList()); QMap initResult(); void thawProcess(struct cgroup_file_info *info, char *root); private: bool m_cgroupInited; QString m_cgroupInitErrMsg; QStringList m_createdCGroupNames; QStringList m_frozenCGroups; // user.slice/user-1000.slice/ QString m_rootCgroup; const QStringList kControllerNames = { "cpu", "memory", "io", "pids" }; const QString kResult = "result"; const QString kErrMsg = "errorMessage"; }; #endif // APPMANAGERCGROUPD_H kylin-process-manager-4.0.0.0/process-manager-daemon/README.md0000644000175000017500000000407714536025312023130 0ustar debiandebian# kylin-app-cgroupd介绍 ## 简介 + kylin-app-cgroupd是分级冻结的后端服务,为kylin-app-manager提供方法和信号,是实现分级冻结的核心模块。 + kylin-app-cgroupd利用Control Groups技术(`https://docs.kernel.org/admin-guide/cgroup-v2.html`)创建应用进程分组或者删除应用进程分组,获取系统中支持限制进程的资源名称,设置分组中进程的资源使用配比。 + kylin-app-cgroupd通过对/proc/pressure/cpu、/proc/pressure/io和/proc/meminfo文件进行监测,实现了对CPU、I/O、存储等系统资源监测预警的功能。 ## 编译依赖 - `pkg-config` - `cmake` - `qtbase5-dev` - ` libcgroup-dev (>= 3.0.0-1)` - `libprocps-dev` - `libspdlog-dev` ## 编译 ```shell mkdir build cd build cmake .. make ``` ## 使用要求 + 当前系统需要支持cgroup v2版本,终端输入$ `mount | grep cgroup`,输出结果显示`cgroup2 on /sys/fs/cgroup`,说明支持cgroup v2系统。再输入$ `ll /sys/fs.cgroup`,输出结果有blick/、memory/和freezer/等目录结构的是cgroup v1结构,需要修改默认配置,$`sudo vim /etc/default/grub`,添加以下配置项 `GRUB_CMDLINE_LINUX_DEFAULT="quiet splash systemd.unified_cgroup_hierarchy=1"`,然后输入$`sudo update-grub`, $`reboot`。 + 重启后检验是否默认开启cgroup v2版本,终端输入$`ll /sys/fs/cgroup`,输出结果中显示`cgroup.controllers`和`cgroup.subtree_control`等系统文件的表明已开启cgroup v2结构。 + kylin-app-cgroupd源码包地址:https://gitee.com/openkylin/kylin-app-cgroupd ## 配置文件 + com.kylin.ProcessManager.conf 存放目录: /etc/dbus-1/system.d/ + com.kylin.ProcessManager.service 存放目录: /usr/share/dbus-1/system-services/ + configs/kylin-process-manager.service 存放目录: /lib/systemd/system/ + kylin-process-manager-cleaner.service 存放目录: /lib/systemd/system/ + com.kylin.ProcessManager.xml 存放目录: /usr/share/dbus-1/interfaces/ + kylin-process-manager.json 存放目录: /etc/kylin-process-manager/ ## 联系我们 - `https://gitee.com/openkylin/kylin-app-cgroupd` kylin-process-manager-4.0.0.0/process-manager-daemon/processreclaimer.h0000644000175000017500000000206714536025312025361 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef PROCESSRECLAIMER_H #define PROCESSRECLAIMER_H #include #include "common.h" class ProcessReclaimer : public QObject { Q_OBJECT public: explicit ProcessReclaimer(QObject *parent = nullptr); static bool isAvailable(); common::DBusResult reclaimProcesses(const QList pids); signals: private: bool m_isAvailable; }; #endif // PROCESSRECLAIMER_H kylin-process-manager-4.0.0.0/process-manager-daemon/confmanager.h0000644000175000017500000000433114536025312024273 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef CONFMANAGER_H #define CONFMANAGER_H #include #include #define FEAT_FROZEN "frozen" class QFileSystemWatcher; class ConfManager : public QObject { Q_OBJECT public: enum Resource { CPU, IO, Memory }; Q_ENUM(Resource); enum ResourceUrgency { Low = 1, Medium = 2, High = 4, }; Q_ENUM(ResourceUrgency); typedef QMap ResourceUrgencyThrehold; explicit ConfManager(QObject *parent = nullptr); QStringList features() const; QList pressureResouces() const; QMap pressureTriggers(Resource resource) const; ResourceUrgencyThrehold resourceThreshold(Resource resource) const; int detectEffectiveNumber(Resource resource) const; int resourceUrgencyIndex(ResourceUrgency urgency) const; private Q_SLOTS: void readConf(); private: void updateFeatures(const QJsonObject &jsonObj); void updateResourceConfig(const QString &resource, const QString &api, const QJsonArray &thresholds, const int &effectiveNumber); private: QStringList m_features; QMap m_pressureTriggers; QMap m_resourceThresholds; QMap m_detectEffectiveNumbers; QFileSystemWatcher *confFileWatcher; const char *kConfFilename = "/etc/kylin-process-manager/kylin-process-manager.json"; }; #endif // CONFMANAGER_H kylin-process-manager-4.0.0.0/process-manager-daemon/logger.h0000644000175000017500000000227314536025312023275 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef LOGGER_H #define LOGGER_H #include #include #include namespace common { class Logger : public QObject { Q_OBJECT public: explicit Logger(QObject *parent = nullptr); static void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg); private: static std::shared_ptr logger(); }; }; // namespace common #endif // LOGGER_H kylin-process-manager-4.0.0.0/process-manager-daemon/systemresourcewatcher.cpp0000644000175000017500000000545614536250254027036 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "systemresourcewatcher.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include "common.h" SystemResourceWatcher::SystemResourceWatcher(QObject *parent) : ResourceWatcher{parent} , m_stop(false) , m_timer(new QTimer(this)) { connect(m_timer, &QTimer::timeout, this, &SystemResourceWatcher::detectMemory); ConfManager &confMgr = common::Singleton::GetInstance(); m_resourceThrehold = confMgr.resourceThreshold(ConfManager::Memory); m_detectEffectiveNumber = confMgr.detectEffectiveNumber(ConfManager::Memory); if (m_detectEffectiveNumber == 0) { m_detectEffectiveNumber = 5; } } void SystemResourceWatcher::stop() { m_stop = true; } unsigned long SystemResourceWatcher::getAvailableMem() { meminfo_info *meminfo = nullptr; procps_meminfo_new(&meminfo); if (!meminfo) { return 0; } meminfo_result *memResult = procps_meminfo_get(meminfo, MEMINFO_MEM_AVAILABLE); if (!memResult) { return 0; } procps_meminfo_unref(&meminfo); return memResult->result.ul_int; } void SystemResourceWatcher::detectMemory() { uint mbMainAvailable = getAvailableMem() / (double)1024; if (mbMainAvailable > m_resourceThrehold.value(ConfManager::Low)) { return; } if (mbMainAvailable <= m_resourceThrehold.value(ConfManager::Low) && mbMainAvailable > m_resourceThrehold.value(ConfManager::Medium)) { Q_EMIT ResourceThresholdWarning("Memory", 1); return; } if (mbMainAvailable <= m_resourceThrehold.value(ConfManager::Medium) && mbMainAvailable > m_resourceThrehold.value(ConfManager::High)) { Q_EMIT ResourceThresholdWarning("Memory", 2); return; } if (mbMainAvailable <= m_resourceThrehold.value(ConfManager::High)) { Q_EMIT ResourceThresholdWarning("Memory", 3); } } void SystemResourceWatcher::start() { m_timer->start(1000); } kylin-process-manager-4.0.0.0/process-manager-daemon/logger.cpp0000644000175000017500000000344614536025312023633 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "logger.h" #include #include #include #include #include #include namespace common { Logger::Logger(QObject *parent) : QObject(parent) {} void Logger::outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg) { Q_UNUSED(context) switch (type) { case QtDebugMsg: { logger()->debug(msg.toStdString()); break; } case QtWarningMsg: { logger()->warn(msg.toStdString()); break; } case QtCriticalMsg: { logger()->critical(msg.toStdString()); break; } case QtInfoMsg: { logger()->info(msg.toStdString()); break; } default: break; } logger()->flush(); } std::shared_ptr Logger::logger() { QString logFileName = "/var/log/kylin-process-manager/daily.txt"; static auto logger = spdlog::daily_logger_mt( "daily_logger", logFileName.toStdString().c_str(), 2, 30); logger->set_level(spdlog::level::info); return logger; } } // namespace common kylin-process-manager-4.0.0.0/CMakeLists.txt.user0000644000175000017500000004403214536223476021045 0ustar debiandebian EnvironmentId {6ce07058-5a9a-41bf-9d97-48c725341da9} ProjectExplorer.Project.ActiveTarget 0 ProjectExplorer.Project.EditorSettings true false true Cpp CppGlobal QmlJS QmlJSGlobal 2 UTF-8 false 4 false 80 true true 1 false true false 0 true true 0 8 true false 1 true true true *.md, *.MD, Makefile false true true ProjectExplorer.Project.PluginSettings true false true true true true 0 true true true Builtin.DefaultTidyAndClazy 2 true ProjectExplorer.Project.Target.0 Desktop 桌面 桌面 {a4daab46-0115-4a1d-9cbc-6b2a3220749e} 0 0 0 Debug false -DCMAKE_GENERATOR:STRING=Unix Makefiles -DCMAKE_BUILD_TYPE:STRING=Debug -DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} -DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} -DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} -DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} -DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} 0 /home/debian/openkylin/kylin-process-manager/../build-kylin-process-manager-unknown-Debug all false true 构建 CMakeProjectManager.MakeStep 1 构建 构建 ProjectExplorer.BuildSteps.Build clean false true 构建 CMakeProjectManager.MakeStep 1 Clean Clean ProjectExplorer.BuildSteps.Clean 2 false false Debug CMakeProjectManager.CMakeBuildConfiguration Release false -DCMAKE_GENERATOR:STRING=Unix Makefiles -DCMAKE_BUILD_TYPE:STRING=Release -DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} -DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} -DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} -DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} -DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} /home/debian/openkylin/kylin-process-manager/../build-kylin-process-manager-unknown-Release all false true CMakeProjectManager.MakeStep 1 构建 构建 ProjectExplorer.BuildSteps.Build clean false true CMakeProjectManager.MakeStep 1 Clean Clean ProjectExplorer.BuildSteps.Clean 2 false false Release CMakeProjectManager.CMakeBuildConfiguration 2 0 部署 部署 ProjectExplorer.BuildSteps.Deploy 1 false ProjectExplorer.DefaultDeployConfiguration 1 true true true 2 kylin-process-manager CMakeProjectManager.CMakeRunConfiguration.kylin-process-manager kylin-process-manager false true true false true /home/debian/openkylin/build-kylin-process-manager-unknown-Debug/process-manager true true true 2 kylin-process-manager-daemon CMakeProjectManager.CMakeRunConfiguration.kylin-process-manager-daemon kylin-process-manager-daemon false true true false true /home/debian/openkylin/build-kylin-process-manager-unknown-Debug/process-manager-daemon 2 ProjectExplorer.Project.TargetCount 1 ProjectExplorer.Project.Updater.FileVersion 22 Version 22 kylin-process-manager-4.0.0.0/man/0000755000175000017500000000000014536025312016065 5ustar debiandebiankylin-process-manager-4.0.0.0/man/kylin-process-manager-daemon.10000644000175000017500000000102514536025312023620 0ustar debiandebian.TH kylin-process-manager-daemon 1 "26 July 2023" "UKUI Desktop Environment" .SH "NAME" kylin-process-manager-daemon \- The process manager daemon service of UKUI4.0. .SH "DESCRIPTION" kylin-process-manager-daemon is application process resource manager daemon of UKUI4.0. .SH "BUGS" .SS Should you encounter any bugs, they may be reported at: https://gitee.com/openkylin/kylin-process-manager/issues .SH "AUTHORS" .SS This Manual Page has been written for the UKUI Desktop Environment by: Pengfei Guo (2023)kylin-process-manager-4.0.0.0/man/kylin-process-manager.10000644000175000017500000000075214536025312022365 0ustar debiandebian.TH kylin-process-manager 1 "26 July 2023" "UKUI Desktop Environment" .SH "NAME" kylin-process-manager \- The process manager of UKUI4.0. .SH "DESCRIPTION" kylin-process-manager is application process resource manager of UKUI4.0. .SH "BUGS" .SS Should you encounter any bugs, they may be reported at: https://gitee.com/openkylin/kylin-process-manager/issues .SH "AUTHORS" .SS This Manual Page has been written for the UKUI Desktop Environment by: Pengfei Guo (2023)kylin-process-manager-4.0.0.0/third-party/0000755000175000017500000000000014536025312017561 5ustar debiandebiankylin-process-manager-4.0.0.0/third-party/SingleApplication/0000755000175000017500000000000014536025312023166 5ustar debiandebiankylin-process-manager-4.0.0.0/third-party/SingleApplication/SingleApplication0000644000175000017500000000003714536025312026516 0ustar debiandebian#include "singleapplication.h" kylin-process-manager-4.0.0.0/third-party/SingleApplication/singleapplication.cpp0000644000175000017500000002174414536025312027407 0ustar debiandebian// The MIT License (MIT) // // Copyright (c) Itay Grudev 2015 - 2020 // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. #include #include #include #include "singleapplication.h" #include "singleapplication_p.h" /** * @brief Constructor. Checks and fires up LocalServer or closes the program * if another instance already exists * @param argc * @param argv * @param allowSecondary Whether to enable secondary instance support * @param options Optional flags to toggle specific behaviour * @param timeout Maximum time blocking functions are allowed during app load */ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSecondary, Options options, int timeout, const QString &userData ) : app_t( argc, argv ), d_ptr( new SingleApplicationPrivate( this ) ) { Q_D( SingleApplication ); #if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) // On Android and iOS since the library is not supported fallback to // standard QApplication behaviour by simply returning at this point. qWarning() << "SingleApplication is not supported on Android and iOS systems."; return; #endif // Store the current mode of the program d->options = options; // Add any unique user data if ( ! userData.isEmpty() ) d->addAppData( userData ); // Generating an application ID used for identifying the shared memory // block and QLocalServer d->genBlockServerName(); // To mitigate QSharedMemory issues with large amount of processes // attempting to attach at the same time SingleApplicationPrivate::randomSleep(); #ifdef Q_OS_UNIX // By explicitly attaching it and then deleting it we make sure that the // memory is deleted even after the process has crashed on Unix. d->memory = new QSharedMemory( d->blockServerName ); d->memory->attach(); delete d->memory; #endif // Guarantee thread safe behaviour with a shared memory block. d->memory = new QSharedMemory( d->blockServerName ); // Create a shared memory block if( d->memory->create( sizeof( InstancesInfo ) )){ // Initialize the shared memory block if( ! d->memory->lock() ){ qCritical() << "SingleApplication: Unable to lock memory block after create."; abortSafely(); } d->initializeMemoryBlock(); } else { if( d->memory->error() == QSharedMemory::AlreadyExists ){ // Attempt to attach to the memory segment if( ! d->memory->attach() ){ qCritical() << "SingleApplication: Unable to attach to shared memory block."; abortSafely(); } if( ! d->memory->lock() ){ qCritical() << "SingleApplication: Unable to lock memory block after attach."; abortSafely(); } } else { qCritical() << "SingleApplication: Unable to create block."; abortSafely(); } } auto *inst = static_cast( d->memory->data() ); QElapsedTimer time; time.start(); // Make sure the shared memory block is initialised and in consistent state while( true ){ // If the shared memory block's checksum is valid continue if( d->blockChecksum() == inst->checksum ) break; // If more than 5s have elapsed, assume the primary instance crashed and // assume it's position if( time.elapsed() > 5000 ){ qWarning() << "SingleApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure."; d->initializeMemoryBlock(); } // Otherwise wait for a random period and try again. The random sleep here // limits the probability of a collision between two racing apps and // allows the app to initialise faster if( ! d->memory->unlock() ){ qDebug() << "SingleApplication: Unable to unlock memory for random wait."; qDebug() << d->memory->errorString(); } SingleApplicationPrivate::randomSleep(); if( ! d->memory->lock() ){ qCritical() << "SingleApplication: Unable to lock memory after random wait."; abortSafely(); } } if( inst->primary == false ){ d->startPrimary(); if( ! d->memory->unlock() ){ qDebug() << "SingleApplication: Unable to unlock memory after primary start."; qDebug() << d->memory->errorString(); } return; } // Check if another instance can be started if( allowSecondary ){ d->startSecondary(); if( d->options & Mode::SecondaryNotification ){ d->connectToPrimary( timeout, SingleApplicationPrivate::SecondaryInstance ); } if( ! d->memory->unlock() ){ qDebug() << "SingleApplication: Unable to unlock memory after secondary start."; qDebug() << d->memory->errorString(); } return; } if( ! d->memory->unlock() ){ qDebug() << "SingleApplication: Unable to unlock memory at end of execution."; qDebug() << d->memory->errorString(); } d->connectToPrimary( timeout, SingleApplicationPrivate::NewInstance ); delete d; ::exit( EXIT_SUCCESS ); } SingleApplication::~SingleApplication() { Q_D( SingleApplication ); delete d; } /** * Checks if the current application instance is primary. * @return Returns true if the instance is primary, false otherwise. */ bool SingleApplication::isPrimary() const { Q_D( const SingleApplication ); return d->server != nullptr; } /** * Checks if the current application instance is secondary. * @return Returns true if the instance is secondary, false otherwise. */ bool SingleApplication::isSecondary() const { Q_D( const SingleApplication ); return d->server == nullptr; } /** * Allows you to identify an instance by returning unique consecutive instance * ids. It is reset when the first (primary) instance of your app starts and * only incremented afterwards. * @return Returns a unique instance id. */ quint32 SingleApplication::instanceId() const { Q_D( const SingleApplication ); return d->instanceNumber; } /** * Returns the OS PID (Process Identifier) of the process running the primary * instance. Especially useful when SingleApplication is coupled with OS. * specific APIs. * @return Returns the primary instance PID. */ qint64 SingleApplication::primaryPid() const { Q_D( const SingleApplication ); return d->primaryPid(); } /** * Returns the username the primary instance is running as. * @return Returns the username the primary instance is running as. */ QString SingleApplication::primaryUser() const { Q_D( const SingleApplication ); return d->primaryUser(); } /** * Returns the username the current instance is running as. * @return Returns the username the current instance is running as. */ QString SingleApplication::currentUser() const { return SingleApplicationPrivate::getUsername(); } /** * Sends message to the Primary Instance. * @param message The message to send. * @param timeout the maximum timeout in milliseconds for blocking functions. * @param sendMode mode of operation * @return true if the message was sent successfuly, false otherwise. */ bool SingleApplication::sendMessage( const QByteArray &message, int timeout, SendMode sendMode ) { Q_D( SingleApplication ); // Nobody to connect to if( isPrimary() ) return false; // Make sure the socket is connected if( ! d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect ) ) return false; return d->writeConfirmedMessage( timeout, message, sendMode ); } /** * Cleans up the shared memory block and exits with a failure. * This function halts program execution. */ void SingleApplication::abortSafely() { Q_D( SingleApplication ); qCritical() << "SingleApplication: " << d->memory->error() << d->memory->errorString(); delete d; ::exit( EXIT_FAILURE ); } QStringList SingleApplication::userData() const { Q_D( const SingleApplication ); return d->appData(); } kylin-process-manager-4.0.0.0/third-party/SingleApplication/LICENSE0000644000175000017500000000223214536025312024172 0ustar debiandebianThe MIT License (MIT) Copyright (c) Itay Grudev 2015 - 2020 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Note: Some of the examples include code not distributed under the terms of the MIT License. kylin-process-manager-4.0.0.0/third-party/SingleApplication/CHANGELOG.md0000644000175000017500000002021614536025312025000 0ustar debiandebian# Changelog ## 3.4.0 * Provide API for blocking sendMessage. - _Christoph Cullmann_ ## 3.3.4 * Fix compilation under Qt 6.2+ and stricter Qt compile settings. - _Christoph Cullmann_ ## 3.3.3 * Support for Qt 6.3+ - Fixed deprecated `QCryptographicHash::addData()` that will only support `QByteArrayView` going further. - _Moody Liu_ ## 3.3.2 * Fixed crash caused by sending a `writeAck` on a removed connection. - _Nicolas Werner_ ## 3.3.1 * Added support for _AppImage_ dynamic executable paths. - _Michael Klein_ ## 3.3.0 * Fixed message fragmentation issue causing crashes and incorrectly / inconsistently received messages. - _Nils Jeisecke_ ## 3.2.0 * Added support for Qt 6 - _Jonas Kvinge_ * Fixed warning in `Qt 5.9` with `min`/`max` functions on Windows - _Nick Korotysh_ * Fix return value of connectToPrimary() when connect is successful - _Jonas Kvinge_ * Fix build issue with MinGW GCC pedantic mode - _Iakov Kirilenko_ * Fixed conversion from `int` to `quint32` and Clang Tidy warnings - _Hennadii Chernyshchyk_ ## 3.1.5 * Improved library stability in edge cases and very rapid process initialisation * Fixed Bug where the shared memory block may have been modified without a lock * Fixed Bug causing `instanceStarted()` to not get emitted when a second instance has been started before the primary has initiated it's `QLocalServer`. ## 3.1.4 * Officially supporting and build-testing against Qt 5.15 * Fixed an MSVC C4996 warning that suggests using `strncpy_s`. _Hennadii Chernyshchyk_ ## 3.1.3.1 * CMake build system improvements * Fixed Clang Tidy warnings _Hennadii Chernyshchyk_ ## 3.1.3 * Improved `CMakeLists.txt` _Hennadii Chernyshchyk_ ## 3.1.2 * Fix a crash when exiting an application on Android and iOS _Emeric Grange_ ## 3.1.1a * Added currentUser() method that returns the user the current instance is running as. _Leander Schulten_ ## 3.1.0a * Added primaryUser() method that returns the user the primary instance is running as. ## 3.0.19 * Fixed code warning for depricated functions in Qt 5.10 related to `QTime` and `qrand()`. _Hennadii Chernyshchyk_ _Anton Filimonov_ _Jonas Kvinge_ ## 3.0.18 * Fallback to standard QApplication class on iOS and Android systems where the library is not supported. * Added Build CI tests to verify the library builds successfully on Linux, Windows and MacOS across multiple Qt versions. _Anton Filimonov_ ## 3.0.17 * Fixed compilation warning/error caused by `geteuid()` on unix based systems. _Iakov Kirilenko_ * Added CMake support _Hennadii Chernyshchyk_ ## 3.0.16 * Use geteuid and getpwuid to get username on Unix, fallback to environment variable. _Jonas Kvinge_ ## 3.0.15 * Bug Fix: sendMessage() might return false even though data was actually written. _Jonas Kvinge_ ## 3.0.14 * Fixed uninitialised variables in the `SingleApplicationPrivate` constructor. ## 3.0.13a * Process socket events asynchronously * Fix undefined variable error on Windows _Francis Giraldeau_ ## 3.0.12a * Removed signal handling. ## 3.0.11a * Fixed bug where the message sent by the second process was not received correctly when the message is sent immediately following a connection. _Francis Giraldeau_ * Refactored code and implemented shared memory block consistency checks via `qChecksum()` (CRC-16). * Explicit `qWarning` and `qCritical` when the library is unable to initialise correctly. ## 3.0.10 * Removed C style casts and eliminated all clang warnings. Fixed `instanceId` reading from only one byte in the message deserialization. Cleaned up serialization code using `QDataStream`. Changed connection type to use `quint8 enum` rather than `char`. * Renamed `SingleAppConnectionType` to `ConnectionType`. Added initialization values to all `ConnectionType` enum cases. _Jedidiah Buck McCready_ ## 3.0.9 * Added SingleApplicationPrivate::primaryPid() as a solution to allow bringing the primary window of an application to the foreground on Windows. _Eelco van Dam from Peacs BV_ ## 3.0.8 * Bug fix - changed QApplication::instance() to QCoreApplication::instance() _Evgeniy Bazhenov_ ## 3.0.7a * Fixed compilation error with Mingw32 in MXE thanks to Vitaly Tonkacheyev. * Removed QMutex used for thread safe behaviour. The implementation now uses QCoreApplication::instance() to get an instance to SingleApplication for memory deallocation. ## 3.0.6a * Reverted GetUserName API usage on Windows. Fixed bug with missing library. * Fixed bug in the Calculator example, preventing it's window to be raised on Windows. Special thanks to Charles Gunawan. ## 3.0.5a * Fixed a memory leak in the SingleApplicationPrivate destructor. _Sergei Moiseev_ ## 3.0.4a * Fixed shadow and uninitialised variable warnings. _Paul Walmsley_ ## 3.0.3a * Removed Microsoft Windows specific code for getting username due to multiple problems and compiler differences on Windows platforms. On Windows the shared memory block in User mode now includes the user's home path (which contains the user's username). * Explicitly getting absolute path of the user's home directory as on Unix a relative path (`~`) may be returned. ## 3.0.2a * Fixed bug on Windows when username containing wide characters causes the library to crash. _Le Liu_ ## 3.0.1a * Allows the application path and version to be excluded from the server name hash. The following flags were added for this purpose: * `SingleApplication::Mode::ExcludeAppVersion` * `SingleApplication::Mode::ExcludeAppPath` * Allow a non elevated process to connect to a local server created by an elevated process run by the same user on Windows * Fixes a problem with upper case letters in paths on Windows _Le Liu_ ## v3.0a * Deprecated secondary instances count. * Added a sendMessage() method to send a message to the primary instance. * Added a receivedMessage() signal, emitted when a message is received from a secondary instance. * The SingleApplication constructor's third parameter is now a bool specifying if the current instance should be allowed to run as a secondary instance if there is already a primary instance. * The SingleApplication constructor accept a fourth parameter specifying if the SingleApplication block should be User-wide or System-wide. * SingleApplication no longer relies on `applicationName` and `organizationName` to be set. It instead concatenates all of the following data and computes a `SHA256` hash which is used as the key of the `QSharedMemory` block and the `QLocalServer`. Since at least `applicationFilePath` is always present there is no need to explicitly set any of the following prior to initialising `SingleApplication`. * `QCoreApplication::applicationName` * `QCoreApplication::applicationVersion` * `QCoreApplication::applicationFilePath` * `QCoreApplication::organizationName` * `QCoreApplication::organizationDomain` * User name or home directory path if in User mode * The primary instance is no longer notified when a secondary instance had been started by default. A `Mode` flag for this feature exists. * Added `instanceNumber()` which represents a unique identifier for each secondary instance started. When called from the primary instance will return `0`. ## v2.4 * Stability improvements * Support for secondary instances. * The library now recovers safely after the primary process has crashed and the shared memory had not been deleted. ## v2.3 * Improved pimpl design and inheritance safety. _Vladislav Pyatnichenko_ ## v2.2 * The `QAPPLICATION_CLASS` macro can now be defined in the file including the Single Application header or with a `DEFINES+=` statement in the project file. ## v2.1 * A race condition can no longer occur when starting two processes nearly simultaneously. Fix issue [#3](https://github.com/itay-grudev/SingleApplication/issues/3) ## v2.0 * SingleApplication is now being passed a reference to `argc` instead of a copy. Fix issue [#1](https://github.com/itay-grudev/SingleApplication/issues/1) * Improved documentation. kylin-process-manager-4.0.0.0/third-party/SingleApplication/singleapplication_p.cpp0000644000175000017500000004070114536025312027720 0ustar debiandebian// The MIT License (MIT) // // Copyright (c) Itay Grudev 2015 - 2020 // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // // W A R N I N G !!! // ----------------- // // This file is not part of the SingleApplication API. It is used purely as an // implementation detail. This header file may change from version to // version without notice, or may even be removed. // #include #include #include #include #include #include #include #include #include #include #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) #include #else #include #endif #include "singleapplication.h" #include "singleapplication_p.h" #ifdef Q_OS_UNIX #include #include #include #endif #ifdef Q_OS_WIN #ifndef NOMINMAX #define NOMINMAX 1 #endif #include #include #endif SingleApplicationPrivate::SingleApplicationPrivate( SingleApplication *q_ptr ) : q_ptr( q_ptr ) { server = nullptr; socket = nullptr; memory = nullptr; instanceNumber = 0; } SingleApplicationPrivate::~SingleApplicationPrivate() { if( socket != nullptr ){ socket->close(); delete socket; } if( memory != nullptr ){ memory->lock(); auto *inst = static_cast(memory->data()); if( server != nullptr ){ server->close(); delete server; inst->primary = false; inst->primaryPid = -1; inst->primaryUser[0] = '\0'; inst->checksum = blockChecksum(); } memory->unlock(); delete memory; } } QString SingleApplicationPrivate::getUsername() { #ifdef Q_OS_WIN wchar_t username[UNLEN + 1]; // Specifies size of the buffer on input DWORD usernameLength = UNLEN + 1; if( GetUserNameW( username, &usernameLength ) ) return QString::fromWCharArray( username ); #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) return QString::fromLocal8Bit( qgetenv( "USERNAME" ) ); #else return qEnvironmentVariable( "USERNAME" ); #endif #endif #ifdef Q_OS_UNIX QString username; uid_t uid = geteuid(); struct passwd *pw = getpwuid( uid ); if( pw ) username = QString::fromLocal8Bit( pw->pw_name ); if ( username.isEmpty() ){ #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) username = QString::fromLocal8Bit( qgetenv( "USER" ) ); #else username = qEnvironmentVariable( "USER" ); #endif } return username; #endif } void SingleApplicationPrivate::genBlockServerName() { QCryptographicHash appData( QCryptographicHash::Sha256 ); #if QT_VERSION < QT_VERSION_CHECK(6, 3, 0) appData.addData( "SingleApplication", 17 ); #else appData.addData( QByteArrayView{"SingleApplication"} ); #endif appData.addData( SingleApplication::app_t::applicationName().toUtf8() ); appData.addData( SingleApplication::app_t::organizationName().toUtf8() ); appData.addData( SingleApplication::app_t::organizationDomain().toUtf8() ); if ( ! appDataList.isEmpty() ) appData.addData( appDataList.join(QString()).toUtf8() ); if( ! (options & SingleApplication::Mode::ExcludeAppVersion) ){ appData.addData( SingleApplication::app_t::applicationVersion().toUtf8() ); } if( ! (options & SingleApplication::Mode::ExcludeAppPath) ){ #if defined(Q_OS_WIN) appData.addData( SingleApplication::app_t::applicationFilePath().toLower().toUtf8() ); #elif defined(Q_OS_LINUX) // If the application is running as an AppImage then the APPIMAGE env var should be used // instead of applicationPath() as each instance is launched with its own executable path const QByteArray appImagePath = qgetenv( "APPIMAGE" ); if( appImagePath.isEmpty() ){ // Not running as AppImage: use path to executable file appData.addData( SingleApplication::app_t::applicationFilePath().toUtf8() ); } else { // Running as AppImage: Use absolute path to AppImage file appData.addData( appImagePath ); }; #else appData.addData( SingleApplication::app_t::applicationFilePath().toUtf8() ); #endif } // User level block requires a user specific data in the hash if( options & SingleApplication::Mode::User ){ appData.addData( getUsername().toUtf8() ); } // Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with // server naming requirements. blockServerName = QString::fromUtf8(appData.result().toBase64().replace("/", "_")); } void SingleApplicationPrivate::initializeMemoryBlock() const { auto *inst = static_cast( memory->data() ); inst->primary = false; inst->secondary = 0; inst->primaryPid = -1; inst->primaryUser[0] = '\0'; inst->checksum = blockChecksum(); } void SingleApplicationPrivate::startPrimary() { // Reset the number of connections auto *inst = static_cast ( memory->data() ); inst->primary = true; inst->primaryPid = QCoreApplication::applicationPid(); qstrncpy( inst->primaryUser, getUsername().toUtf8().data(), sizeof(inst->primaryUser) ); inst->checksum = blockChecksum(); instanceNumber = 0; // Successful creation means that no main process exists // So we start a QLocalServer to listen for connections QLocalServer::removeServer( blockServerName ); server = new QLocalServer(); // Restrict access to the socket according to the // SingleApplication::Mode::User flag on User level or no restrictions if( options & SingleApplication::Mode::User ){ server->setSocketOptions( QLocalServer::UserAccessOption ); } else { server->setSocketOptions( QLocalServer::WorldAccessOption ); } server->listen( blockServerName ); QObject::connect( server, &QLocalServer::newConnection, this, &SingleApplicationPrivate::slotConnectionEstablished ); } void SingleApplicationPrivate::startSecondary() { auto *inst = static_cast ( memory->data() ); inst->secondary += 1; inst->checksum = blockChecksum(); instanceNumber = inst->secondary; } bool SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType connectionType ) { QElapsedTimer time; time.start(); // Connect to the Local Server of the Primary Instance if not already // connected. if( socket == nullptr ){ socket = new QLocalSocket(); } if( socket->state() == QLocalSocket::ConnectedState ) return true; if( socket->state() != QLocalSocket::ConnectedState ){ while( true ){ randomSleep(); if( socket->state() != QLocalSocket::ConnectingState ) socket->connectToServer( blockServerName ); if( socket->state() == QLocalSocket::ConnectingState ){ socket->waitForConnected( static_cast(msecs - time.elapsed()) ); } // If connected break out of the loop if( socket->state() == QLocalSocket::ConnectedState ) break; // If elapsed time since start is longer than the method timeout return if( time.elapsed() >= msecs ) return false; } } // Initialisation message according to the SingleApplication protocol QByteArray initMsg; QDataStream writeStream(&initMsg, QIODevice::WriteOnly); #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) writeStream.setVersion(QDataStream::Qt_5_6); #endif writeStream << blockServerName.toLatin1(); writeStream << static_cast(connectionType); writeStream << instanceNumber; #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) quint16 checksum = qChecksum(QByteArray(initMsg.constData(), static_cast(initMsg.length()))); #else quint16 checksum = qChecksum(initMsg.constData(), static_cast(initMsg.length())); #endif writeStream << checksum; return writeConfirmedMessage( static_cast(msecs - time.elapsed()), initMsg ); } void SingleApplicationPrivate::writeAck( QLocalSocket *sock ) { sock->putChar('\n'); } bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArray &msg, SingleApplication::SendMode sendMode) { QElapsedTimer time; time.start(); // Frame 1: The header indicates the message length that follows QByteArray header; QDataStream headerStream(&header, QIODevice::WriteOnly); #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) headerStream.setVersion(QDataStream::Qt_5_6); #endif headerStream << static_cast ( msg.length() ); if( ! writeConfirmedFrame( static_cast(msecs - time.elapsed()), header )) return false; // Frame 2: The message const bool result = writeConfirmedFrame( static_cast(msecs - time.elapsed()), msg ); // Block if needed if (socket && sendMode == SingleApplication::BlockUntilPrimaryExit) socket->waitForDisconnected(-1); return result; } bool SingleApplicationPrivate::writeConfirmedFrame( int msecs, const QByteArray &msg ) { socket->write( msg ); socket->flush(); bool result = socket->waitForReadyRead( msecs ); // await ack byte if (result) { socket->read( 1 ); return true; } return false; } quint16 SingleApplicationPrivate::blockChecksum() const { #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) quint16 checksum = qChecksum(QByteArray(static_cast(memory->constData()), offsetof(InstancesInfo, checksum))); #else quint16 checksum = qChecksum(static_cast(memory->constData()), offsetof(InstancesInfo, checksum)); #endif return checksum; } qint64 SingleApplicationPrivate::primaryPid() const { qint64 pid; memory->lock(); auto *inst = static_cast( memory->data() ); pid = inst->primaryPid; memory->unlock(); return pid; } QString SingleApplicationPrivate::primaryUser() const { QByteArray username; memory->lock(); auto *inst = static_cast( memory->data() ); username = inst->primaryUser; memory->unlock(); return QString::fromUtf8( username ); } /** * @brief Executed when a connection has been made to the LocalServer */ void SingleApplicationPrivate::slotConnectionEstablished() { QLocalSocket *nextConnSocket = server->nextPendingConnection(); connectionMap.insert(nextConnSocket, ConnectionInfo()); QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose, this, [nextConnSocket, this](){ auto &info = connectionMap[nextConnSocket]; this->slotClientConnectionClosed( nextConnSocket, info.instanceId ); } ); QObject::connect(nextConnSocket, &QLocalSocket::disconnected, nextConnSocket, &QLocalSocket::deleteLater); QObject::connect(nextConnSocket, &QLocalSocket::destroyed, this, [nextConnSocket, this](){ connectionMap.remove(nextConnSocket); } ); QObject::connect(nextConnSocket, &QLocalSocket::readyRead, this, [nextConnSocket, this](){ auto &info = connectionMap[nextConnSocket]; switch(info.stage){ case StageInitHeader: readMessageHeader( nextConnSocket, StageInitBody ); break; case StageInitBody: readInitMessageBody(nextConnSocket); break; case StageConnectedHeader: readMessageHeader( nextConnSocket, StageConnectedBody ); break; case StageConnectedBody: this->slotDataAvailable( nextConnSocket, info.instanceId ); break; default: break; }; } ); } void SingleApplicationPrivate::readMessageHeader( QLocalSocket *sock, SingleApplicationPrivate::ConnectionStage nextStage ) { if (!connectionMap.contains( sock )){ return; } if( sock->bytesAvailable() < ( qint64 )sizeof( quint64 ) ){ return; } QDataStream headerStream( sock ); #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) headerStream.setVersion( QDataStream::Qt_5_6 ); #endif // Read the header to know the message length quint64 msgLen = 0; headerStream >> msgLen; ConnectionInfo &info = connectionMap[sock]; info.stage = nextStage; info.msgLen = msgLen; writeAck( sock ); } bool SingleApplicationPrivate::isFrameComplete( QLocalSocket *sock ) { if (!connectionMap.contains( sock )){ return false; } ConnectionInfo &info = connectionMap[sock]; if( sock->bytesAvailable() < ( qint64 )info.msgLen ){ return false; } return true; } void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock ) { Q_Q(SingleApplication); if( !isFrameComplete( sock ) ) return; // Read the message body QByteArray msgBytes = sock->readAll(); QDataStream readStream(msgBytes); #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) readStream.setVersion( QDataStream::Qt_5_6 ); #endif // server name QByteArray latin1Name; readStream >> latin1Name; // connection type ConnectionType connectionType = InvalidConnection; quint8 connTypeVal = InvalidConnection; readStream >> connTypeVal; connectionType = static_cast ( connTypeVal ); // instance id quint32 instanceId = 0; readStream >> instanceId; // checksum quint16 msgChecksum = 0; readStream >> msgChecksum; #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) const quint16 actualChecksum = qChecksum(QByteArray(msgBytes.constData(), static_cast(msgBytes.length() - sizeof(quint16)))); #else const quint16 actualChecksum = qChecksum(msgBytes.constData(), static_cast(msgBytes.length() - sizeof(quint16))); #endif bool isValid = readStream.status() == QDataStream::Ok && QLatin1String(latin1Name) == blockServerName && msgChecksum == actualChecksum; if( !isValid ){ sock->close(); return; } ConnectionInfo &info = connectionMap[sock]; info.instanceId = instanceId; info.stage = StageConnectedHeader; if( connectionType == NewInstance || ( connectionType == SecondaryInstance && options & SingleApplication::Mode::SecondaryNotification ) ) { Q_EMIT q->instanceStarted(); } writeAck( sock ); } void SingleApplicationPrivate::slotDataAvailable( QLocalSocket *dataSocket, quint32 instanceId ) { Q_Q(SingleApplication); if ( !isFrameComplete( dataSocket ) ) return; const QByteArray message = dataSocket->readAll(); writeAck( dataSocket ); ConnectionInfo &info = connectionMap[dataSocket]; info.stage = StageConnectedHeader; Q_EMIT q->receivedMessage( instanceId, message); } void SingleApplicationPrivate::slotClientConnectionClosed( QLocalSocket *closedSocket, quint32 instanceId ) { if( closedSocket->bytesAvailable() > 0 ) slotDataAvailable( closedSocket, instanceId ); } void SingleApplicationPrivate::randomSleep() { #if QT_VERSION >= QT_VERSION_CHECK( 5, 10, 0 ) QThread::msleep( QRandomGenerator::global()->bounded( 8u, 18u )); #else qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits::max() ); QThread::msleep( qrand() % 11 + 8); #endif } void SingleApplicationPrivate::addAppData(const QString &data) { appDataList.push_back(data); } QStringList SingleApplicationPrivate::appData() const { return appDataList; } kylin-process-manager-4.0.0.0/third-party/SingleApplication/singleapplication.pri0000644000175000017500000000060214536025312027405 0ustar debiandebianQT += core network CONFIG += c++11 HEADERS += $$PWD/SingleApplication \ $$PWD/singleapplication.h \ $$PWD/singleapplication_p.h SOURCES += $$PWD/singleapplication.cpp \ $$PWD/singleapplication_p.cpp INCLUDEPATH += $$PWD win32 { msvc:LIBS += Advapi32.lib gcc:LIBS += -ladvapi32 } DISTFILES += \ $$PWD/README.md \ $$PWD/CHANGELOG.md \ $$PWD/Windows.md kylin-process-manager-4.0.0.0/third-party/SingleApplication/CMakeLists.txt0000644000175000017500000000550014536025312025726 0ustar debiandebiancmake_minimum_required(VERSION 3.12.0) project(SingleApplication LANGUAGES CXX DESCRIPTION "Replacement for QtSingleApplication") set(CMAKE_AUTOMOC ON) add_library(${PROJECT_NAME} STATIC singleapplication.cpp singleapplication_p.cpp ) add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) if(NOT QT_DEFAULT_MAJOR_VERSION) set(QT_DEFAULT_MAJOR_VERSION 5 CACHE STRING "Qt version to use (5 or 6), defaults to 5") endif() # Find dependencies set(QT_COMPONENTS Core Network) set(QT_LIBRARIES Qt${QT_DEFAULT_MAJOR_VERSION}::Core Qt${QT_DEFAULT_MAJOR_VERSION}::Network) message(QAPPLICATION_CLASS-is:${QAPPLICATION_CLASS}) if(QAPPLICATION_CLASS STREQUAL QApplication) list(APPEND QT_COMPONENTS Widgets) list(APPEND QT_LIBRARIES Qt${QT_DEFAULT_MAJOR_VERSION}::Widgets) elseif(QAPPLICATION_CLASS STREQUAL QGuiApplication) list(APPEND QT_COMPONENTS Gui) list(APPEND QT_LIBRARIES Qt${QT_DEFAULT_MAJOR_VERSION}::Gui) else() set(QAPPLICATION_CLASS QCoreApplication) endif() find_package(Qt${QT_DEFAULT_MAJOR_VERSION} COMPONENTS ${QT_COMPONENTS} REQUIRED) option(SINGLEAPPLICATION_DOCUMENTATION "Generate Doxygen documentation" OFF) if(SINGLEAPPLICATION_DOCUMENTATION) find_package(Doxygen) endif() target_link_libraries(${PROJECT_NAME} PUBLIC ${QT_LIBRARIES}) if(WIN32) target_link_libraries(${PROJECT_NAME} PRIVATE advapi32) endif() target_compile_definitions(${PROJECT_NAME} PUBLIC QAPPLICATION_CLASS=${QAPPLICATION_CLASS}) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_compile_definitions(${PROJECT_NAME} PRIVATE QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII QT_NO_URL_CAST_FROM_STRING QT_NO_CAST_FROM_BYTEARRAY QT_USE_QSTRINGBUILDER QT_NO_NARROWING_CONVERSIONS_IN_CONNECT QT_NO_KEYWORDS QT_NO_FOREACH ) if(DOXYGEN_FOUND) # Doxygen theme include(FetchContent) FetchContent_Declare(DoxygenAwesome GIT_REPOSITORY https://github.com/jothepro/doxygen-awesome-css GIT_TAG 4cd62308d825fe0396d2f66ffbab45d0e247724c # 2.0.3 ) FetchContent_MakeAvailable(DoxygenAwesome) FetchContent_GetProperties(DoxygenAwesome SOURCE_DIR DoxygenAwesome_SOURCE_DIR) set(DOXYGEN_USE_MDFILE_AS_MAINPAGE README.md) set(DOXYGEN_GENERATE_TREEVIEW YES) set(DOXYGEN_HTML_HEADER ${DoxygenAwesome_SOURCE_DIR}/doxygen-custom/header.html) set(DOXYGEN_HTML_EXTRA_STYLESHEET ${DoxygenAwesome_SOURCE_DIR}/doxygen-awesome.css) set(DOXYGEN_HTML_EXTRA_FILES ${DoxygenAwesome_SOURCE_DIR}/doxygen-awesome-fragment-copy-button.js ${DoxygenAwesome_SOURCE_DIR}/doxygen-awesome-paragraph-link.js ${DoxygenAwesome_SOURCE_DIR}/doxygen-awesome-darkmode-toggle.js ) doxygen_add_docs(${PROJECT_NAME}Documentation singleapplication.h CHANGELOG.md Windows.md README.md ) endif() kylin-process-manager-4.0.0.0/third-party/SingleApplication/singleapplication_p.h0000644000175000017500000000722314536025312027367 0ustar debiandebian// The MIT License (MIT) // // Copyright (c) Itay Grudev 2015 - 2020 // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // // W A R N I N G !!! // ----------------- // // This file is not part of the SingleApplication API. It is used purely as an // implementation detail. This header file may change from version to // version without notice, or may even be removed. // #ifndef SINGLEAPPLICATION_P_H #define SINGLEAPPLICATION_P_H #include #include #include #include "singleapplication.h" struct InstancesInfo { bool primary; quint32 secondary; qint64 primaryPid; char primaryUser[128]; quint16 checksum; // Must be the last field }; struct ConnectionInfo { qint64 msgLen = 0; quint32 instanceId = 0; quint8 stage = 0; }; class SingleApplicationPrivate : public QObject { Q_OBJECT public: enum ConnectionType : quint8 { InvalidConnection = 0, NewInstance = 1, SecondaryInstance = 2, Reconnect = 3 }; enum ConnectionStage : quint8 { StageInitHeader = 0, StageInitBody = 1, StageConnectedHeader = 2, StageConnectedBody = 3, }; Q_DECLARE_PUBLIC(SingleApplication) SingleApplicationPrivate( SingleApplication *q_ptr ); ~SingleApplicationPrivate() override; static QString getUsername(); void genBlockServerName(); void initializeMemoryBlock() const; void startPrimary(); void startSecondary(); bool connectToPrimary( int msecs, ConnectionType connectionType ); quint16 blockChecksum() const; qint64 primaryPid() const; QString primaryUser() const; bool isFrameComplete(QLocalSocket *sock); void readMessageHeader(QLocalSocket *socket, ConnectionStage nextStage); void readInitMessageBody(QLocalSocket *socket); void writeAck(QLocalSocket *sock); bool writeConfirmedFrame(int msecs, const QByteArray &msg); bool writeConfirmedMessage(int msecs, const QByteArray &msg, SingleApplication::SendMode sendMode = SingleApplication::NonBlocking); static void randomSleep(); void addAppData(const QString &data); QStringList appData() const; SingleApplication *q_ptr; QSharedMemory *memory; QLocalSocket *socket; QLocalServer *server; quint32 instanceNumber; QString blockServerName; SingleApplication::Options options; QMap connectionMap; QStringList appDataList; public Q_SLOTS: void slotConnectionEstablished(); void slotDataAvailable( QLocalSocket*, quint32 ); void slotClientConnectionClosed( QLocalSocket*, quint32 ); }; #endif // SINGLEAPPLICATION_P_H kylin-process-manager-4.0.0.0/third-party/SingleApplication/singleapplication.h0000644000175000017500000001446314536025312027054 0ustar debiandebian// The MIT License (MIT) // // Copyright (c) Itay Grudev 2015 - 2018 // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. #ifndef SINGLE_APPLICATION_H #define SINGLE_APPLICATION_H #include #include #ifndef QAPPLICATION_CLASS #define QAPPLICATION_CLASS QCoreApplication #endif #include QT_STRINGIFY(QAPPLICATION_CLASS) class SingleApplicationPrivate; /** * @brief Handles multiple instances of the same * Application * @see QCoreApplication */ class SingleApplication : public QAPPLICATION_CLASS { Q_OBJECT using app_t = QAPPLICATION_CLASS; public: /** * @brief Mode of operation of `SingleApplication`. * Whether the block should be user-wide or system-wide and whether the * primary instance should be notified when a secondary instance had been * started. * @note Operating system can restrict the shared memory blocks to the same * user, in which case the User/System modes will have no effect and the * block will be user wide. */ enum Mode { /** The `SingleApplication` block should apply user wide * (this adds user specific data to the key used for the shared memory and server name) * */ User = 1 << 0, /** * The `SingleApplication` block applies system-wide. */ System = 1 << 1, /** * Whether to trigger `instanceStarted()` even whenever secondary instances are started */ SecondaryNotification = 1 << 2, /** * Excludes the application version from the server name (and memory block) hash */ ExcludeAppVersion = 1 << 3, /** * Excludes the application path from the server name (and memory block) hash */ ExcludeAppPath = 1 << 4 }; Q_DECLARE_FLAGS(Options, Mode) /** * @brief Intitializes a `SingleApplication` instance with argc command line * arguments in argv * @arg argc - Number of arguments in argv * @arg argv - Supplied command line arguments * @arg allowSecondary - Whether to start the instance as secondary * if there is already a primary instance. * @arg mode - Whether for the `SingleApplication` block to be applied * User wide or System wide. * @arg timeout - Timeout to wait in milliseconds. * @note argc and argv may be changed as Qt removes arguments that it * recognizes * @note `Mode::SecondaryNotification` only works if set on both the primary * instance and the secondary instance. * @note The timeout is just a hint for the maximum time of blocking * operations. It does not guarantee that the `SingleApplication` * initialisation will be completed in given time, though is a good hint. * Usually 4*timeout would be the worst case (fail) scenario. * @see See the corresponding `QAPPLICATION_CLASS` constructor for reference */ explicit SingleApplication( int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 1000, const QString &userData = {} ); ~SingleApplication() override; /** * @brief Checks if the instance is primary instance * @returns `true` if the instance is primary */ bool isPrimary() const; /** * @brief Checks if the instance is a secondary instance * @returns `true` if the instance is secondary */ bool isSecondary() const; /** * @brief Returns a unique identifier for the current instance * @returns instance id */ quint32 instanceId() const; /** * @brief Returns the process ID (PID) of the primary instance * @returns pid */ qint64 primaryPid() const; /** * @brief Returns the username of the user running the primary instance * @returns user name */ QString primaryUser() const; /** * @brief Returns the username of the current user * @returns user name */ QString currentUser() const; /** * @brief Mode of operation of sendMessage. */ enum SendMode { NonBlocking, /** Do not wait for the primary instance termination and return immediately */ BlockUntilPrimaryExit, /** Wait until the primary instance is terminated */ }; /** * @brief Sends a message to the primary instance * @param message data to send * @param timeout timeout for connecting * @param sendMode - Mode of operation * @returns `true` on success * @note sendMessage() will return false if invoked from the primary instance */ bool sendMessage( const QByteArray &message, int timeout = 100, SendMode sendMode = NonBlocking ); /** * @brief Get the set user data. * @returns user data */ QStringList userData() const; Q_SIGNALS: /** * @brief Triggered whenever a new instance had been started, * except for secondary instances if the `Mode::SecondaryNotification` flag is not specified */ void instanceStarted(); /** * @brief Triggered whenever there is a message received from a secondary instance */ void receivedMessage( quint32 instanceId, QByteArray message ); private: SingleApplicationPrivate *d_ptr; Q_DECLARE_PRIVATE(SingleApplication) void abortSafely(); }; Q_DECLARE_OPERATORS_FOR_FLAGS(SingleApplication::Options) #endif // SINGLE_APPLICATION_H kylin-process-manager-4.0.0.0/third-party/SingleApplication/README.md0000644000175000017500000001401514536025312024446 0ustar debiandebian# SingleApplication [![CI](https://github.com/itay-grudev/SingleApplication/workflows/CI:%20Build%20Test/badge.svg)](https://github.com/itay-grudev/SingleApplication/actions) This is a replacement of the QtSingleApplication for `Qt5` and `Qt6`. Keeps the Primary Instance of your Application and kills each subsequent instances. It can (if enabled) spawn secondary (non-related to the primary) instances and can send data to the primary instance from secondary instances. ## Documentation You can find the full usage reference [here](https://itay-grudev.github.io/SingleApplication/classSingleApplication.html). ## Usage The `SingleApplication` class inherits from whatever `Q[Core|Gui]Application` class you specify via the `QAPPLICATION_CLASS` macro (`QCoreApplication` is the default). Further usage is similar to the use of the `Q[Core|Gui]Application` classes. You can use the library as if you use any other `QCoreApplication` derived class: ```cpp #include #include int main( int argc, char* argv[] ) { SingleApplication app( argc, argv ); return app.exec(); } ``` To include the library files I would recommend that you add it as a git submodule to your project. Here is how: ```bash git submodule add https://github.com/itay-grudev/SingleApplication.git singleapplication ``` **Qmake:** Then include the `singleapplication.pri` file in your `.pro` project file. ```qmake include(singleapplication/singleapplication.pri) DEFINES += QAPPLICATION_CLASS=QApplication ``` **CMake:** Then include the subdirectory in your `CMakeLists.txt` project file. ```cmake set(QAPPLICATION_CLASS QApplication CACHE STRING "Inheritance class for SingleApplication") add_subdirectory(src/third-party/singleapplication) target_link_libraries(${PROJECT_NAME} SingleApplication::SingleApplication) ``` The library sets up a `QLocalServer` and a `QSharedMemory` block. The first instance of your Application is your Primary Instance. It would check if the shared memory block exists and if not it will start a `QLocalServer` and listen for connections. Each subsequent instance of your application would check if the shared memory block exists and if it does, it will connect to the QLocalServer to notify the primary instance that a new instance had been started, after which it would terminate with status code `0`. In the Primary Instance `SingleApplication` would emit the `instanceStarted()` signal upon detecting that a new instance had been started. The library uses `stdlib` to terminate the program with the `exit()` function. Also don't forget to specify which `QCoreApplication` class your app is using if it is not `QCoreApplication` as in examples above. ## Instance started signal The `SingleApplication` class implements a `instanceStarted()` signal. You can bind to that signal to raise your application's window when a new instance had been started, for example. ```cpp // window is a QWindow instance QObject::connect( &app, &SingleApplication::instanceStarted, &window, &QWindow::raise ); ``` Using `SingleApplication::instance()` is a neat way to get the `SingleApplication` instance for binding to it's signals anywhere in your program. _Note:_ On Windows the ability to bring the application windows to the foreground is restricted. See [Windows specific implementations](Windows.md) for a workaround and an example implementation. ## Secondary Instances If you want to be able to launch additional Secondary Instances (not related to your Primary Instance) you have to enable that with the third parameter of the `SingleApplication` constructor. The default is `false` meaning no Secondary Instances. Here is an example of how you would start a Secondary Instance send a message with the command line arguments to the primary instance and then shut down. ```cpp int main(int argc, char *argv[]) { SingleApplication app( argc, argv, true ); if( app.isSecondary() ) { app.sendMessage( app.arguments().join(' ')).toUtf8() ); app.exit( 0 ); } return app.exec(); } ``` _Note:_ A secondary instance won't cause the emission of the `instanceStarted()` signal by default. See `SingleApplication::Mode` for more details.* You can check whether your instance is a primary or secondary with the following methods: ```cpp app.isPrimary(); // or app.isSecondary(); ``` _Note:_ If your Primary Instance is terminated a newly launched instance will replace the Primary one even if the Secondary flag has been set.* ## Examples There are three examples provided in this repository: * Basic example that prevents a secondary instance from starting [`examples/basic`](https://github.com/itay-grudev/SingleApplication/tree/master/examples/basic) * An example of a graphical application raising it's parent window [`examples/calculator`](https://github.com/itay-grudev/SingleApplication/tree/master/examples/calculator) * A console application sending the primary instance it's command line parameters [`examples/sending_arguments`](https://github.com/itay-grudev/SingleApplication/tree/master/examples/sending_arguments) ## Versioning Each major version introduces either very significant changes or is not backwards compatible with the previous version. Minor versions only add additional features, bug fixes or performance improvements and are backwards compatible with the previous release. See [CHANGELOG.md](CHANGELOG.md) for more details. ## Implementation The library is implemented with a `QSharedMemory` block which is thread safe and guarantees a race condition will not occur. It also uses a `QLocalSocket` to notify the main process that a new instance had been spawned and thus invoke the `instanceStarted()` signal and for messaging the primary instance. Additionally the library can recover from being forcefully killed on *nix systems and will reset the memory block given that there are no other instances running. ## License This library and it's supporting documentation are released under `The MIT License (MIT)` with the exception of the Qt calculator examples which is distributed under the BSD license. kylin-process-manager-4.0.0.0/third-party/SingleApplication/Windows.md0000644000175000017500000000243514536025312025146 0ustar debiandebian# Windows Specifics ## Setting the foreground window In the `instanceStarted()` example in the `README` we demonstrated how an application can bring it's primary instance window whenever a second copy of the application is started. On Windows the ability to bring the application windows to the foreground is restricted, see [AllowSetForegroundWindow()][https://msdn.microsoft.com/en-us/library/windows/desktop/ms632668.aspx] for more details. The background process (the primary instance) can bring its windows to the foreground if it is allowed by the current foreground process (the secondary instance). To bypass this `SingleApplication` must be initialized with the `allowSecondary` parameter set to `true` and the `options` parameter must include `Mode::SecondaryNotification`, See `SingleApplication::Mode` for more details. Here is an example: ```cpp if( app.isSecondary() ) { // This API requires LIBS += User32.lib to be added to the project AllowSetForegroundWindow( DWORD( app.primaryPid() ) ); } if( app.isPrimary() ) { QObject::connect( &app, &SingleApplication::instanceStarted, this, &App::instanceStarted ); } ``` ```cpp void App::instanceStarted() { QApplication::setActiveWindow( [window/widget to set to the foreground] ); } ``` kylin-process-manager-4.0.0.0/README.md0000644000175000017500000001307114536025312016573 0ustar debiandebian# kylin-app-manager介绍 ## 简介 + 在传统Linux操作系统中,应用进程的生命周期一般由应用本身直接控制。然而,系统资源(包括CPU、I/O、存储等)是有限的,当我们运行大量的I/O密集型或CPU密集型应用时,经常会出现系统卡顿甚至死机的现象,这大大影响了用户的操作体验。所以,为了优化用户操作体验,在openKylin操作系统设计中,应用进程的生命周期并不由应用本身直接控制,而是由系统综合多种因素来确定,kylin-app-manager会对处于不同状态的应用进行分级处理,进行不同策略的CPU、磁盘I/O、内存等资源限制,尽可能地保障当前用户能够感知应用的资源分配,充分保证系统的整体流畅性,这是应用管理实现分级冻结的核心思想。 + 应用管理还负责桌面应用程序启动,在平板模式下实现了应用单实例功能。 ## 编译依赖 - `qtbase5-dev` - `libkf5config-dev` - `libkf5windowsystem-dev` - `libkf5wayland-dev` - `libkysdk-waylandhelper-dev` - `libukui-log4qt-dev` - ` pkg-config` - `libgsettings-qt-dev` - `libglib2.0-dev` ## 编译 ```shell mkdir build cd build qmake .. make ``` ## 使用要求 + 当前系统需要支持cgroup v2版本,终端输入$ `mount | grep cgroup`,输出结果显示`cgroup2 on /sys/fs/cgroup`,说明支持cgroup v2系统。再输入$ `ll /sys/fs.cgroup`,输出结果有blick/、memory/和freezer/等目录结构的是cgroup v1结构,需要修改默认配置,$`sudo vim /etc/default/grub`,添加以下配置项 `GRUB_CMDLINE_LINUX_DEFAULT="quiet splash systemd.unified_cgroup_hierarchy=1"`,然后输入$`sudo update-grub`, $`reboot`。 + 重启后检验是否默认开启cgroup v2版本,终端输入$`ll /sys/fs/cgroup`,输出结果中显示`cgroup.controllers`和`cgroup.subtree_control`等系统文件的表明已开启cgroup v2结构。 + 需要结合kylin-app-cgroupd共同实现分级冻结功能 + kylin-app-cgroupd源码包地址:https://gitee.com/openkylin/kylin-app-cgroupd ## 应用状态的定义 + 焦点应用:表示当前正在操作的即获取焦点的应用,不对其进行资源限制 + 前台应用:只存在pc模式下的状态,表示当前运行的未最小化但没有获得焦点的应用 + 后台应用:pc模式下指窗口最小化状态且持续时间<=30min或者前台应用持续时间>=30min的应用,平板模式下指非当前打开且持续时间<=30min的应用进程,平板模式下处于该状态下的多媒体进程需要暂停音视频的播放(dbus无法获取所有实例的状态 + 缓存应用:指处于后台应用状态且持续时间>30min的应用进程,平板模式下,处于该状态的应用会被冻结 + 休眠应用:被冻结并且放至swap分区的应用,在内存资源不足的情况下会转换至此状态 ## 提供的Dbus功能接口 + kylin-app-manager 是一个开机自启动的dbus服务 - `type: session dbus` - `service: com.kylin.AppManager` - `path: /com/kylin/AppManager` - `interface: com.kylin.AppManager` - `LaunchApp(in 's' desktopFile, out 'b' succeed) ` + 功能:打开desktopFile文件对应的应用程序 - `LaunchAppWithArguments(in 's' desktopFile, in 'as' args, out 'b' succeed)` + 功能:打开desktopFile文件对应的应用程序。以启动参数args的方式运行 - `LaunchDefaultAppWithUrl(in 's' url, out 'b' succeed)` + 功能:以默认应用打开url对应的资源 - `LaunchXdgFile(in 's' desktopFile, in 's' file, out 'b' succeed)` + 功能:使用desktopFile对应的应用程序,打开file文件。 - `AppDesktopFileNameByWid(in 'qint' wid, out 's' desktopFile) ` + 功能:根据应用窗口wid的值找到应用对应的desktop名称。 - `ActiveProcessByWid(in 'uint' wid, out 'b' succeed) ` + 功能:此接口为应用打开时,通过多任务视图滑动关闭时提供,根据应用的窗口wid的值找到应用对应的进pid将其进程移动至ForegroundProcess分组,应用正常关闭。 - `AppWhiteList( in 's' option, out 'as' whiteLists)` + 功能:获取某一个白名单名称下的所有应用列表 - `Open(int 's' fileName, out 'b' succeed)` + 功能:打开文件 - `RecommendAppLists(in 's' fileName, out 'as' appLists)` + 功能:获取某一个文件可支持打开的应用列表 - `AddToWhiteList(in 's' desktopFile, in 's' option, out 'b' succeed)` + 功能:将desktopFile文件对应的应用程序添加到白名单。 - `WhiteListsOfApp(in 's' desktopFile, out 'as' whiteLists)` + 功能:查询desktopFile文件对应的应用程序所在的白名单列表。 - `RemoveFromWhiteList(in 's' desktopFile, in 's' option, out 'b' succeed)` + 功能:将desktopFile文件对应的应用程序从option对应的白名单中删除。 - `Inhibit(in 's' desktopFile, in 'u' pid, in 's' reason, in 'i' flags, out 's' cookie)` + 功能:通过该接口可设置指定运行应用不受某些资源使用的限制 - `UnInhibit(in 's' cookie)` + 功能:取消Inhibit接口添加的限制 - `ThawApps(out 'b' succeed)` + 功能:通过该接口解冻所有已打开应用 ## 其他组件的交互 + 所有需要启动第三方桌面应用或系统软件均需要通过应用管理的接口进行启动,比如开始菜单,侧边栏,任务栏,桌面和文件管理器等。 ## 配置文件 + com.kylin.AppManager.service 应用管理dbus自启动服务文件,保存在/usr/share/dbus-1/services目录下。 + kylin-app-manager.json 应用管理资源限制策略保存在/etc/kylin-app-manager目录下。 ## 联系我们 - `https://gitee.com/openkylin/kylin-app-manager` kylin-process-manager-4.0.0.0/docs/0000755000175000017500000000000014536025312016242 5ustar debiandebiankylin-process-manager-4.0.0.0/docs/状态定义.md0000644000175000017500000000417514536025312023013 0ustar debiandebian# 说明 **注意:** 1. 本文中的定义目前只适用于桌面客户端的app。~~~~ 2. 优先级的数字越小代表优先级越高。 3. 本文的状态管理和切换都建立在PC处于正常模式的情况下。暂不考虑省电模式等。 4. 对进程的管理策略也仅限在平板模式下,PC模式下所有进程均恢复至前台进程的管理策略。 # 关于进程状态的定义 ## 前台进程 - 优先级: 1 - 定义:用户目前执行操作的进程,即当前显示的应用的进程。平板模式下当前只能有一个前台进程。 - ~~管理:不受任何 `CPU` 和 `IO` 使用的限制。~~ - 管理:不受任何资源使用的限制。 ## 不可见进程 - 优先级: 2 - ~~定义:除前台进程之外正在执行当前用户知晓的任务的应用进程。比如打开应用商店下载安装软件的过程中切回到桌面,再打开 `WPS`,此时 `WPS` 就是前台进程,应用商店就是可见进程。~~ - ~~管理:`CPU` 和 `IO` 的调度优先级降一级。~~ - 定义:不可见进程是前台进程最小化后不显示应用界面的进程。 - 管理:资源使用限制降级。 ## 缓存进程 - 优先级: 3 - ~~定义:目前不需要的进程,未显示在屏幕的应用的进程,在 `30min` 之内未进行任何任务的进程即可视为缓存进程。~~ - 定义:不可见进程在 `30min` 之内未切换至前台进程的话会转换成缓存进程。 - 管理:冻结该进程。 # 状态时序转换 ![](./out/状态时序/状态时序.svg) **注意:** 一旦进程被判定为缓存进程之后,该进程便无法直接切换至可见进程。 # 内存管理策略 极端情况下,系统可能会出现内存不足的情况,~~在出现启动某应用失败时会获取当前内存情况~~ 每`30S`获取一次内存的使用情况,如果内存使用超过 `90%`,应用管理会尝试结束掉部分应用。进程优先级越低的应用会被优先结束,**前台进程永远不允许被结束掉**~~,如果还是启动失败或者没有可结束的进程则不予处理~~。 # 资源限制 `Cgroup` kylin-process-manager-4.0.0.0/docs/状态时序.puml0000644000175000017500000000107514536025312023402 0ustar debiandebian@startuml 前台进程->不可见进程: 屏幕上打开的应用最小化至后台 不可见进程->缓存进程: 不可见进程30min内未切换至前台进程 不可见进程-->前台进程: 应用重新显示在屏幕上或者平板模式切换至PC模式 缓存进程-->前台进程: 应用重新显示在屏幕上或者平板模式切换至PC模式 缓存进程->被结束进程: 当前系统内存占用过高,优先结束缓存进程 不可见进程->被结束进程: 当前系统内存占用过高,没有缓存进程,结束不可见进程 @endumlkylin-process-manager-4.0.0.0/docs/应用管理架构.drawio0000644000175000017500000001464314536025312026053 0ustar debiandebian kylin-process-manager-4.0.0.0/process-manager/0000755000175000017500000000000014536025312020400 5ustar debiandebiankylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/0000755000175000017500000000000014536025312024312 5ustar debiandebiankylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/QtSingleApplication0000644000175000017500000000004114536025312030142 0ustar debiandebian#include "qtsingleapplication.h" kylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/qtsingleapplication.h0000644000175000017500000000763214536025312030545 0ustar debiandebian/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Solutions component. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names ** of its contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QTSINGLEAPPLICATION_H #define QTSINGLEAPPLICATION_H #include class QtLocalPeer; #if defined(Q_OS_WIN) # if !defined(QT_QTSINGLEAPPLICATION_EXPORT) && !defined(QT_QTSINGLEAPPLICATION_IMPORT) # define QT_QTSINGLEAPPLICATION_EXPORT # elif defined(QT_QTSINGLEAPPLICATION_IMPORT) # if defined(QT_QTSINGLEAPPLICATION_EXPORT) # undef QT_QTSINGLEAPPLICATION_EXPORT # endif # define QT_QTSINGLEAPPLICATION_EXPORT __declspec(dllimport) # elif defined(QT_QTSINGLEAPPLICATION_EXPORT) # undef QT_QTSINGLEAPPLICATION_EXPORT # define QT_QTSINGLEAPPLICATION_EXPORT __declspec(dllexport) # endif #else # define QT_QTSINGLEAPPLICATION_EXPORT #endif class QT_QTSINGLEAPPLICATION_EXPORT QtSingleApplication : public QApplication { Q_OBJECT public: QtSingleApplication(int &argc, char **argv, bool GUIenabled = true); QtSingleApplication(const QString &id, int &argc, char **argv); #if QT_VERSION < 0x050000 QtSingleApplication(int &argc, char **argv, Type type); # if defined(Q_WS_X11) QtSingleApplication(Display* dpy, Qt::HANDLE visual = 0, Qt::HANDLE colormap = 0); QtSingleApplication(Display *dpy, int &argc, char **argv, Qt::HANDLE visual = 0, Qt::HANDLE cmap= 0); QtSingleApplication(Display* dpy, const QString &appId, int argc, char **argv, Qt::HANDLE visual = 0, Qt::HANDLE colormap = 0); # endif // Q_WS_X11 #endif // QT_VERSION < 0x050000 bool isRunning(); QString id() const; void setActivationWindow(QObject* aw, bool activateOnMessage = true); QObject* activationWindow() const; // Obsolete: void initialize(bool dummy = true) { isRunning(); Q_UNUSED(dummy) } public Q_SLOTS: bool sendMessage(const QString &message, int timeout = 5000); void activateWindow(); Q_SIGNALS: void messageReceived(const QString &message); private: void sysInit(const QString &appId = QString()); QtLocalPeer *peer; QObject *actObj = nullptr; }; #endif // QTSINGLEAPPLICATION_H kylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/qtsinglecoreapplication.h0000644000175000017500000000502514536025312031410 0ustar debiandebian/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Solutions component. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names ** of its contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QTSINGLECOREAPPLICATION_H #define QTSINGLECOREAPPLICATION_H #include class QtLocalPeer; class QtSingleCoreApplication : public QCoreApplication { Q_OBJECT public: QtSingleCoreApplication(int &argc, char **argv); QtSingleCoreApplication(const QString &id, int &argc, char **argv); bool isRunning(); QString id() const; public Q_SLOTS: bool sendMessage(const QString &message, int timeout = 5000); Q_SIGNALS: void messageReceived(const QString &message); private: QtLocalPeer* peer; }; #endif // QTSINGLECOREAPPLICATION_H kylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/qtsingleapplication.pri0000644000175000017500000000106114536025312031076 0ustar debiandebianINCLUDEPATH += $$PWD DEPENDPATH += $$PWD QT *= network greaterThan(QT_MAJOR_VERSION, 4): QT *= widgets qtsingleapplication-uselib:!qtsingleapplication-buildlib { LIBS += -L$$QTSINGLEAPPLICATION_LIBDIR -l$$QTSINGLEAPPLICATION_LIBNAME } else { SOURCES += $$PWD/qtsingleapplication.cpp $$PWD/qtlocalpeer.cpp HEADERS += $$PWD/qtsingleapplication.h $$PWD/qtlocalpeer.h } win32 { contains(TEMPLATE, lib):contains(CONFIG, shared):DEFINES += QT_QTSINGLEAPPLICATION_EXPORT else:qtsingleapplication-uselib:DEFINES += QT_QTSINGLEAPPLICATION_IMPORT } kylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/qtsinglecoreapplication.pri0000644000175000017500000000050414536025312031750 0ustar debiandebianINCLUDEPATH += $$PWD DEPENDPATH += $$PWD HEADERS += $$PWD/qtsinglecoreapplication.h $$PWD/qtlocalpeer.h SOURCES += $$PWD/qtsinglecoreapplication.cpp $$PWD/qtlocalpeer.cpp QT *= network win32:contains(TEMPLATE, lib):contains(CONFIG, shared) { DEFINES += QT_QTSINGLECOREAPPLICATION_EXPORT=__declspec(dllexport) } kylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/qtlockedfile_unix.cpp0000644000175000017500000000661414536025312030536 0ustar debiandebian/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Solutions component. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names ** of its contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include #include #include #include #include "qtlockedfile.h" bool QtLockedFile::lock(LockMode mode, bool block) { if (!isOpen()) { qWarning("QtLockedFile::lock(): file is not opened"); return false; } if (mode == NoLock) return unlock(); if (mode == m_lock_mode) return true; if (m_lock_mode != NoLock) unlock(); struct flock fl; fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 0; fl.l_type = (mode == ReadLock) ? F_RDLCK : F_WRLCK; int cmd = block ? F_SETLKW : F_SETLK; int ret = fcntl(handle(), cmd, &fl); if (ret == -1) { if (errno != EINTR && errno != EAGAIN) qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno)); return false; } m_lock_mode = mode; return true; } bool QtLockedFile::unlock() { if (!isOpen()) { qWarning("QtLockedFile::unlock(): file is not opened"); return false; } if (!isLocked()) return true; struct flock fl; fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 0; fl.l_type = F_UNLCK; int ret = fcntl(handle(), F_SETLKW, &fl); if (ret == -1) { qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno)); return false; } m_lock_mode = NoLock; return true; } QtLockedFile::~QtLockedFile() { if (isOpen()) unlock(); } kylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/qtsingleapplication.cpp0000644000175000017500000002674314536025312031104 0ustar debiandebian/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Solutions component. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names ** of its contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "qtsingleapplication.h" #include "qtlocalpeer.h" //#include #include #include #include #include #include /*! \class QtSingleApplication qtsingleapplication.h \brief The QtSingleApplication class provides an API to detect and communicate with running instances of an application. This class allows you to create applications where only one instance should be running at a time. I.e., if the user tries to launch another instance, the already running instance will be activated instead. Another usecase is a client-server system, where the first started instance will assume the role of server, and the later instances will act as clients of that server. By default, the full path of the executable file is used to determine whether two processes are instances of the same application. You can also provide an explicit identifier string that will be compared instead. The application should create the QtSingleApplication object early in the startup phase, and call isRunning() to find out if another instance of this application is already running. If isRunning() returns false, it means that no other instance is running, and this instance has assumed the role as the running instance. In this case, the application should continue with the initialization of the application user interface before entering the event loop with exec(), as normal. The messageReceived() signal will be emitted when the running application receives messages from another instance of the same application. When a message is received it might be helpful to the user to raise the application so that it becomes visible. To facilitate this, QtSingleApplication provides the setActivationWindow() function and the activateWindow() slot. If isRunning() returns true, another instance is already running. It may be alerted to the fact that another instance has started by using the sendMessage() function. Also data such as startup parameters (e.g. the name of the file the user wanted this new instance to open) can be passed to the running instance with this function. Then, the application should terminate (or enter client mode). If isRunning() returns true, but sendMessage() fails, that is an indication that the running instance is frozen. Here's an example that shows how to convert an existing application to use QtSingleApplication. It is very simple and does not make use of all QtSingleApplication's functionality (see the examples for that). \code // Original int main(int argc, char **argv) { QApplication app(argc, argv); MyMainWidget mmw; mmw.show(); return app.exec(); } // Single instance int main(int argc, char **argv) { QtSingleApplication app(argc, argv); if (app.isRunning()) return !app.sendMessage(someDataString); MyMainWidget mmw; app.setActivationWindow(&mmw); mmw.show(); return app.exec(); } \endcode Once this QtSingleApplication instance is destroyed (normally when the process exits or crashes), when the user next attempts to run the application this instance will not, of course, be encountered. The next instance to call isRunning() or sendMessage() will assume the role as the new running instance. For console (non-GUI) applications, QtSingleCoreApplication may be used instead of this class, to avoid the dependency on the QtGui library. \sa QtSingleCoreApplication */ void QtSingleApplication::sysInit(const QString &appId) { actObj = 0; peer = new QtLocalPeer(this, appId); connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&))); } /*! Creates a QtSingleApplication object. The application identifier will be QCoreApplication::applicationFilePath(). \a argc, \a argv, and \a GUIenabled are passed on to the QAppliation constructor. If you are creating a console application (i.e. setting \a GUIenabled to false), you may consider using QtSingleCoreApplication instead. */ QtSingleApplication::QtSingleApplication(int &argc, char **argv, bool GUIenabled) : QApplication(argc, argv, GUIenabled) { sysInit(); } /*! Creates a QtSingleApplication object with the application identifier \a appId. \a argc and \a argv are passed on to the QAppliation constructor. */ QtSingleApplication::QtSingleApplication(const QString &appId, int &argc, char **argv) : QApplication(argc, argv) { sysInit(appId); } #if QT_VERSION < 0x050000 /*! Creates a QtSingleApplication object. The application identifier will be QCoreApplication::applicationFilePath(). \a argc, \a argv, and \a type are passed on to the QAppliation constructor. */ QtSingleApplication::QtSingleApplication(int &argc, char **argv, Type type) : QApplication(argc, argv, type) { sysInit(); } # if defined(Q_WS_X11) /*! Special constructor for X11, ref. the documentation of QApplication's corresponding constructor. The application identifier will be QCoreApplication::applicationFilePath(). \a dpy, \a visual, and \a cmap are passed on to the QApplication constructor. */ QtSingleApplication::QtSingleApplication(Display* dpy, Qt::HANDLE visual, Qt::HANDLE cmap) : QApplication(dpy, visual, cmap) { sysInit(); } /*! Special constructor for X11, ref. the documentation of QApplication's corresponding constructor. The application identifier will be QCoreApplication::applicationFilePath(). \a dpy, \a argc, \a argv, \a visual, and \a cmap are passed on to the QApplication constructor. */ QtSingleApplication::QtSingleApplication(Display *dpy, int &argc, char **argv, Qt::HANDLE visual, Qt::HANDLE cmap) : QApplication(dpy, argc, argv, visual, cmap) { sysInit(); } /*! Special constructor for X11, ref. the documentation of QApplication's corresponding constructor. The application identifier will be \a appId. \a dpy, \a argc, \a argv, \a visual, and \a cmap are passed on to the QApplication constructor. */ QtSingleApplication::QtSingleApplication(Display* dpy, const QString &appId, int argc, char **argv, Qt::HANDLE visual, Qt::HANDLE cmap) : QApplication(dpy, argc, argv, visual, cmap) { sysInit(appId); } # endif // Q_WS_X11 #endif // QT_VERSION < 0x050000 /*! Returns true if another instance of this application is running; otherwise false. This function does not find instances of this application that are being run by a different user (on Windows: that are running in another session). \sa sendMessage() */ bool QtSingleApplication::isRunning() { return peer->isClient(); } /*! Tries to send the text \a message to the currently running instance. The QtSingleApplication object in the running instance will emit the messageReceived() signal when it receives the message. This function returns true if the message has been sent to, and processed by, the current instance. If there is no instance currently running, or if the running instance fails to process the message within \a timeout milliseconds, this function return false. \sa isRunning(), messageReceived() */ bool QtSingleApplication::sendMessage(const QString &message, int timeout) { return peer->sendMessage(message, timeout); } /*! Returns the application identifier. Two processes with the same identifier will be regarded as instances of the same application. */ QString QtSingleApplication::id() const { return peer->applicationId(); } /*! Sets the activation window of this application to \a aw. The activation window is the widget that will be activated by activateWindow(). This is typically the application's main window. If \a activateOnMessage is true (the default), the window will be activated automatically every time a message is received, just prior to the messageReceived() signal being emitted. \sa activateWindow(), messageReceived() */ void QtSingleApplication::setActivationWindow(QObject* aw, bool activateOnMessage) { actObj = aw; if (activateOnMessage) connect(peer, SIGNAL(messageReceived(const QString&)), this, SLOT(activateWindow())); else disconnect(peer, SIGNAL(messageReceived(const QString&)), this, SLOT(activateWindow())); } /*! Returns the applications activation window if one has been set by calling setActivationWindow(), otherwise returns 0. \sa setActivationWindow() */ QObject* QtSingleApplication::activationWindow() const { return actObj; } /*! De-minimizes, raises, and activates this application's activation window. This function does nothing if no activation window has been set. This is a convenience function to show the user that this application instance has been activated when he has tried to start another instance. This function should typically be called in response to the messageReceived() signal. By default, that will happen automatically, if an activation window has been set. \sa setActivationWindow(), messageReceived(), initialize() */ void QtSingleApplication::activateWindow() { } /*! \fn void QtSingleApplication::messageReceived(const QString& message) This signal is emitted when the current instance receives a \a message from another instance of this application. \sa sendMessage(), setActivationWindow(), activateWindow() */ /*! \fn void QtSingleApplication::initialize(bool dummy = true) \obsolete */ kylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/qtlockedfile_win.cpp0000644000175000017500000001466314536025312030353 0ustar debiandebian/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Solutions component. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names ** of its contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "qtlockedfile.h" #include #include #define MUTEX_PREFIX "QtLockedFile mutex " // Maximum number of concurrent read locks. Must not be greater than MAXIMUM_WAIT_OBJECTS #define MAX_READERS MAXIMUM_WAIT_OBJECTS #if QT_VERSION >= 0x050000 #define QT_WA(unicode, ansi) unicode #endif Qt::HANDLE QtLockedFile::getMutexHandle(int idx, bool doCreate) { if (mutexname.isEmpty()) { QFileInfo fi(*this); mutexname = QString::fromLatin1(MUTEX_PREFIX) + fi.absoluteFilePath().toLower(); } QString mname(mutexname); if (idx >= 0) mname += QString::number(idx); Qt::HANDLE mutex; if (doCreate) { QT_WA( { mutex = CreateMutexW(NULL, FALSE, (TCHAR*)mname.utf16()); }, { mutex = CreateMutexA(NULL, FALSE, mname.toLocal8Bit().constData()); } ); if (!mutex) { qErrnoWarning("QtLockedFile::lock(): CreateMutex failed"); return 0; } } else { QT_WA( { mutex = OpenMutexW(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, (TCHAR*)mname.utf16()); }, { mutex = OpenMutexA(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, mname.toLocal8Bit().constData()); } ); if (!mutex) { if (GetLastError() != ERROR_FILE_NOT_FOUND) qErrnoWarning("QtLockedFile::lock(): OpenMutex failed"); return 0; } } return mutex; } bool QtLockedFile::waitMutex(Qt::HANDLE mutex, bool doBlock) { Q_ASSERT(mutex); DWORD res = WaitForSingleObject(mutex, doBlock ? INFINITE : 0); switch (res) { case WAIT_OBJECT_0: case WAIT_ABANDONED: return true; break; case WAIT_TIMEOUT: break; default: qErrnoWarning("QtLockedFile::lock(): WaitForSingleObject failed"); } return false; } bool QtLockedFile::lock(LockMode mode, bool block) { if (!isOpen()) { qWarning("QtLockedFile::lock(): file is not opened"); return false; } if (mode == NoLock) return unlock(); if (mode == m_lock_mode) return true; if (m_lock_mode != NoLock) unlock(); if (!wmutex && !(wmutex = getMutexHandle(-1, true))) return false; if (!waitMutex(wmutex, block)) return false; if (mode == ReadLock) { int idx = 0; for (; idx < MAX_READERS; idx++) { rmutex = getMutexHandle(idx, false); if (!rmutex || waitMutex(rmutex, false)) break; CloseHandle(rmutex); } bool ok = true; if (idx >= MAX_READERS) { qWarning("QtLockedFile::lock(): too many readers"); rmutex = 0; ok = false; } else if (!rmutex) { rmutex = getMutexHandle(idx, true); if (!rmutex || !waitMutex(rmutex, false)) ok = false; } if (!ok && rmutex) { CloseHandle(rmutex); rmutex = 0; } ReleaseMutex(wmutex); if (!ok) return false; } else { Q_ASSERT(rmutexes.isEmpty()); for (int i = 0; i < MAX_READERS; i++) { Qt::HANDLE mutex = getMutexHandle(i, false); if (mutex) rmutexes.append(mutex); } if (rmutexes.size()) { DWORD res = WaitForMultipleObjects(rmutexes.size(), rmutexes.constData(), TRUE, block ? INFINITE : 0); if (res != WAIT_OBJECT_0 && res != WAIT_ABANDONED) { if (res != WAIT_TIMEOUT) qErrnoWarning("QtLockedFile::lock(): WaitForMultipleObjects failed"); m_lock_mode = WriteLock; // trick unlock() to clean up - semiyucky unlock(); return false; } } } m_lock_mode = mode; return true; } bool QtLockedFile::unlock() { if (!isOpen()) { qWarning("QtLockedFile::unlock(): file is not opened"); return false; } if (!isLocked()) return true; if (m_lock_mode == ReadLock) { ReleaseMutex(rmutex); CloseHandle(rmutex); rmutex = 0; } else { Q_FOREACH(Qt::HANDLE mutex, rmutexes) { ReleaseMutex(mutex); CloseHandle(mutex); } rmutexes.clear(); ReleaseMutex(wmutex); } m_lock_mode = QtLockedFile::NoLock; return true; } QtLockedFile::~QtLockedFile() { if (isOpen()) unlock(); if (wmutex) CloseHandle(wmutex); } kylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/qtsinglecoreapplication.cpp0000644000175000017500000001235514536025312031747 0ustar debiandebian/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Solutions component. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names ** of its contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "qtsinglecoreapplication.h" #include "qtlocalpeer.h" /*! \class QtSingleCoreApplication qtsinglecoreapplication.h \brief A variant of the QtSingleApplication class for non-GUI applications. This class is a variant of QtSingleApplication suited for use in console (non-GUI) applications. It is an extension of QCoreApplication (instead of QApplication). It does not require the QtGui library. The API and usage is identical to QtSingleApplication, except that functions relating to the "activation window" are not present, for obvious reasons. Please refer to the QtSingleApplication documentation for explanation of the usage. A QtSingleCoreApplication instance can communicate to a QtSingleApplication instance if they share the same application id. Hence, this class can be used to create a light-weight command-line tool that sends commands to a GUI application. \sa QtSingleApplication */ /*! Creates a QtSingleCoreApplication object. The application identifier will be QCoreApplication::applicationFilePath(). \a argc and \a argv are passed on to the QCoreAppliation constructor. */ QtSingleCoreApplication::QtSingleCoreApplication(int &argc, char **argv) : QCoreApplication(argc, argv) { peer = new QtLocalPeer(this); connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&))); } /*! Creates a QtSingleCoreApplication object with the application identifier \a appId. \a argc and \a argv are passed on to the QCoreAppliation constructor. */ QtSingleCoreApplication::QtSingleCoreApplication(const QString &appId, int &argc, char **argv) : QCoreApplication(argc, argv) { peer = new QtLocalPeer(this, appId); connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&))); } /*! Returns true if another instance of this application is running; otherwise false. This function does not find instances of this application that are being run by a different user (on Windows: that are running in another session). \sa sendMessage() */ bool QtSingleCoreApplication::isRunning() { return peer->isClient(); } /*! Tries to send the text \a message to the currently running instance. The QtSingleCoreApplication object in the running instance will emit the messageReceived() signal when it receives the message. This function returns true if the message has been sent to, and processed by, the current instance. If there is no instance currently running, or if the running instance fails to process the message within \a timeout milliseconds, this function return false. \sa isRunning(), messageReceived() */ bool QtSingleCoreApplication::sendMessage(const QString &message, int timeout) { return peer->sendMessage(message, timeout); } /*! Returns the application identifier. Two processes with the same identifier will be regarded as instances of the same application. */ QString QtSingleCoreApplication::id() const { return peer->applicationId(); } /*! \fn void QtSingleCoreApplication::messageReceived(const QString& message) This signal is emitted when the current instance receives a \a message from another instance of this application. \sa sendMessage() */ kylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/qtlockedfile.cpp0000644000175000017500000001374214536025312027473 0ustar debiandebian/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Solutions component. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names ** of its contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "qtlockedfile.h" /*! \class QtLockedFile \brief The QtLockedFile class extends QFile with advisory locking functions. A file may be locked in read or write mode. Multiple instances of \e QtLockedFile, created in multiple processes running on the same machine, may have a file locked in read mode. Exactly one instance may have it locked in write mode. A read and a write lock cannot exist simultaneously on the same file. The file locks are advisory. This means that nothing prevents another process from manipulating a locked file using QFile or file system functions offered by the OS. Serialization is only guaranteed if all processes that access the file use QLockedFile. Also, while holding a lock on a file, a process must not open the same file again (through any API), or locks can be unexpectedly lost. The lock provided by an instance of \e QtLockedFile is released whenever the program terminates. This is true even when the program crashes and no destructors are called. */ /*! \enum QtLockedFile::LockMode This enum describes the available lock modes. \value ReadLock A read lock. \value WriteLock A write lock. \value NoLock Neither a read lock nor a write lock. */ /*! Constructs an unlocked \e QtLockedFile object. This constructor behaves in the same way as \e QFile::QFile(). \sa QFile::QFile() */ QtLockedFile::QtLockedFile() : QFile() { #ifdef Q_OS_WIN wmutex = 0; rmutex = 0; #endif m_lock_mode = NoLock; } /*! Constructs an unlocked QtLockedFile object with file \a name. This constructor behaves in the same way as \e QFile::QFile(const QString&). \sa QFile::QFile() */ QtLockedFile::QtLockedFile(const QString &name) : QFile(name) { #ifdef Q_OS_WIN wmutex = 0; rmutex = 0; #endif m_lock_mode = NoLock; } /*! Opens the file in OpenMode \a mode. This is identical to QFile::open(), with the one exception that the Truncate mode flag is disallowed. Truncation would conflict with the advisory file locking, since the file would be modified before the write lock is obtained. If truncation is required, use resize(0) after obtaining the write lock. Returns true if successful; otherwise false. \sa QFile::open(), QFile::resize() */ bool QtLockedFile::open(OpenMode mode) { if (mode & QIODevice::Truncate) { qWarning("QtLockedFile::open(): Truncate mode not allowed."); return false; } return QFile::open(mode); } /*! Returns \e true if this object has a in read or write lock; otherwise returns \e false. \sa lockMode() */ bool QtLockedFile::isLocked() const { return m_lock_mode != NoLock; } /*! Returns the type of lock currently held by this object, or \e QtLockedFile::NoLock. \sa isLocked() */ QtLockedFile::LockMode QtLockedFile::lockMode() const { return m_lock_mode; } /*! \fn bool QtLockedFile::lock(LockMode mode, bool block = true) Obtains a lock of type \a mode. The file must be opened before it can be locked. If \a block is true, this function will block until the lock is aquired. If \a block is false, this function returns \e false immediately if the lock cannot be aquired. If this object already has a lock of type \a mode, this function returns \e true immediately. If this object has a lock of a different type than \a mode, the lock is first released and then a new lock is obtained. This function returns \e true if, after it executes, the file is locked by this object, and \e false otherwise. \sa unlock(), isLocked(), lockMode() */ /*! \fn bool QtLockedFile::unlock() Releases a lock. If the object has no lock, this function returns immediately. This function returns \e true if, after it executes, the file is not locked by this object, and \e false otherwise. \sa lock(), isLocked(), lockMode() */ /*! \fn QtLockedFile::~QtLockedFile() Destroys the \e QtLockedFile object. If any locks were held, they are released. */ kylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/qtlockedfile.h0000644000175000017500000000630714536025312027137 0ustar debiandebian/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Solutions component. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names ** of its contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QTLOCKEDFILE_H #define QTLOCKEDFILE_H #include #ifdef Q_OS_WIN #include #endif #if defined(Q_OS_WIN) # if !defined(QT_QTLOCKEDFILE_EXPORT) && !defined(QT_QTLOCKEDFILE_IMPORT) # define QT_QTLOCKEDFILE_EXPORT # elif defined(QT_QTLOCKEDFILE_IMPORT) # if defined(QT_QTLOCKEDFILE_EXPORT) # undef QT_QTLOCKEDFILE_EXPORT # endif # define QT_QTLOCKEDFILE_EXPORT __declspec(dllimport) # elif defined(QT_QTLOCKEDFILE_EXPORT) # undef QT_QTLOCKEDFILE_EXPORT # define QT_QTLOCKEDFILE_EXPORT __declspec(dllexport) # endif #else # define QT_QTLOCKEDFILE_EXPORT #endif namespace QtLP_Private { class QT_QTLOCKEDFILE_EXPORT QtLockedFile : public QFile { public: enum LockMode { NoLock = 0, ReadLock, WriteLock }; QtLockedFile(); QtLockedFile(const QString &name); ~QtLockedFile(); bool open(OpenMode mode); bool lock(LockMode mode, bool block = true); bool unlock(); bool isLocked() const; LockMode lockMode() const; private: #ifdef Q_OS_WIN Qt::HANDLE wmutex; Qt::HANDLE rmutex; QVector rmutexes; QString mutexname; Qt::HANDLE getMutexHandle(int idx, bool doCreate); bool waitMutex(Qt::HANDLE mutex, bool doBlock); #endif LockMode m_lock_mode; }; } #endif kylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/qtlocalpeer.cpp0000644000175000017500000001543614536025312027342 0ustar debiandebian/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Solutions component. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names ** of its contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "qtlocalpeer.h" #include #include #include #if defined(Q_OS_WIN) #include #include typedef BOOL(WINAPI*PProcessIdToSessionId)(DWORD,DWORD*); static PProcessIdToSessionId pProcessIdToSessionId = 0; #endif #if defined(Q_OS_UNIX) #include #include #include #endif namespace QtLP_Private { #include "qtlockedfile.cpp" #if defined(Q_OS_WIN) #include "qtlockedfile_win.cpp" #else #include "qtlockedfile_unix.cpp" #endif } const char* QtLocalPeer::ack = "ack"; QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId) : QObject(parent), id(appId) { QString prefix = id; if (id.isEmpty()) { id = QCoreApplication::applicationFilePath(); #if defined(Q_OS_WIN) id = id.toLower(); #endif prefix = id.section(QLatin1Char('/'), -1); } prefix.remove(QRegExp("[^a-zA-Z]")); prefix.truncate(6); QByteArray idc = id.toUtf8(); quint16 idNum = qChecksum(idc.constData(), idc.size()); socketName = QLatin1String("qtsingleapp-") + prefix + QLatin1Char('-') + QString::number(idNum, 16); #if defined(Q_OS_WIN) if (!pProcessIdToSessionId) { QLibrary lib("kernel32"); pProcessIdToSessionId = (PProcessIdToSessionId)lib.resolve("ProcessIdToSessionId"); } if (pProcessIdToSessionId) { DWORD sessionId = 0; pProcessIdToSessionId(GetCurrentProcessId(), &sessionId); socketName += QLatin1Char('-') + QString::number(sessionId, 16); } #else socketName += QLatin1Char('-') + QString::number(::getuid(), 16); #endif server = new QLocalServer(this); QString lockName = QDir(QDir::tempPath()).absolutePath() + QLatin1Char('/') + socketName + QLatin1String("-lockfile"); lockFile.setFileName(lockName); lockFile.open(QIODevice::ReadWrite); } bool QtLocalPeer::isClient() { if (lockFile.isLocked()) return false; if (!lockFile.lock(QtLP_Private::QtLockedFile::WriteLock, false)) return true; bool res = server->listen(socketName); #if defined(Q_OS_UNIX) && (QT_VERSION >= QT_VERSION_CHECK(4,5,0)) // ### Workaround if (!res && server->serverError() == QAbstractSocket::AddressInUseError) { QFile::remove(QDir::cleanPath(QDir::tempPath())+QLatin1Char('/')+socketName); res = server->listen(socketName); } #endif if (!res) qWarning("QtSingleCoreApplication: listen on local socket failed, %s", qPrintable(server->errorString())); QObject::connect(server, SIGNAL(newConnection()), SLOT(receiveConnection())); return false; } bool QtLocalPeer::sendMessage(const QString &message, int timeout) { if (!isClient()) return false; QLocalSocket socket; bool connOk = false; for(int i = 0; i < 2; i++) { // Try twice, in case the other instance is just starting up socket.connectToServer(socketName); connOk = socket.waitForConnected(timeout/2); if (connOk || i) break; int ms = 250; #if defined(Q_OS_WIN) Sleep(DWORD(ms)); #else struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 }; nanosleep(&ts, NULL); #endif } if (!connOk) return false; QByteArray uMsg(message.toUtf8()); QDataStream ds(&socket); ds.writeBytes(uMsg.constData(), uMsg.size()); bool res = socket.waitForBytesWritten(timeout); if (res) { res &= socket.waitForReadyRead(timeout); // wait for ack if (res) res &= (socket.read(qstrlen(ack)) == ack); } return res; } void QtLocalPeer::receiveConnection() { QLocalSocket* socket = server->nextPendingConnection(); if (!socket) return; while (true) { if (socket->state() == QLocalSocket::UnconnectedState) { qWarning("QtLocalPeer: Peer disconnected"); delete socket; return; } if (socket->bytesAvailable() >= qint64(sizeof(quint32))) break; socket->waitForReadyRead(); } QDataStream ds(socket); QByteArray uMsg; quint32 remaining; ds >> remaining; uMsg.resize(remaining); int got = 0; char* uMsgBuf = uMsg.data(); do { got = ds.readRawData(uMsgBuf, remaining); remaining -= got; uMsgBuf += got; } while (remaining && got >= 0 && socket->waitForReadyRead(2000)); if (got < 0) { qWarning("QtLocalPeer: Message reception failed %s", socket->errorString().toLatin1().constData()); delete socket; return; } QString message(QString::fromUtf8(uMsg)); socket->write(ack, qstrlen(ack)); socket->waitForBytesWritten(1000); socket->waitForDisconnected(1000); // make sure client reads ack delete socket; Q_EMIT messageReceived(message); //### (might take a long time to return) } kylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/QtLockedFile0000644000175000017500000000003214536025312026536 0ustar debiandebian#include "qtlockedfile.h" kylin-process-manager-4.0.0.0/process-manager/QtSingleApplication/qtlocalpeer.h0000644000175000017500000000520514536025312027000 0ustar debiandebian/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Solutions component. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names ** of its contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QTLOCALPEER_H #define QTLOCALPEER_H #include #include #include #include "qtlockedfile.h" class QtLocalPeer : public QObject { Q_OBJECT public: QtLocalPeer(QObject *parent = 0, const QString &appId = QString()); bool isClient(); bool sendMessage(const QString &message, int timeout); QString applicationId() const { return id; } Q_SIGNALS: void messageReceived(const QString &message); protected Q_SLOTS: void receiveConnection(); protected: QString id; QString socketName; QLocalServer* server; QtLP_Private::QtLockedFile lockFile; private: static const char* ack; }; #endif // QTLOCALPEER_H kylin-process-manager-4.0.0.0/process-manager/configs/0000755000175000017500000000000014536025312022030 5ustar debiandebiankylin-process-manager-4.0.0.0/process-manager/configs/.gitkeep0000644000175000017500000000000014536025312023447 0ustar debiandebiankylin-process-manager-4.0.0.0/process-manager/configs/com.kylin.AppManager.service0000644000175000017500000000011614536025312027325 0ustar debiandebian[D-BUS Service] Name=com.kylin.AppManager Exec=/usr/bin/kylin-process-manager kylin-process-manager-4.0.0.0/process-manager/configs/kylin-app-manager.json0000644000175000017500000000135214536025312026240 0ustar debiandebian{ "rulesets": { "actions": [ { "resource": "Memory", "adjs": [300, 200, 100], "cgroup.freeze": [1, 1, 1] }, { "resource": "CPU", "adjs": [300, 200, 100], "cpu.weight": [20, 40, 60], "cpu.max": [20000, 40000, 60000] }, { "resource": "IO", "adjs": [300, 200, 100], "io.weight": [20, 40, 60], "cgroup.freeze": [1, 0, 0] } ] }, "statusSwitchInterval": 1800 } kylin-process-manager-4.0.0.0/process-manager/configs/com.kylin.ProcessManager.xml0000644000175000017500000000666214536025312027377 0ustar debiandebian kylin-process-manager-4.0.0.0/process-manager/CMakeLists.txt0000644000175000017500000000553014536260415023150 0ustar debiandebiancmake_minimum_required(VERSION 3.14) project(kylin-process-manager LANGUAGES CXX) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_definitions(-DQT_NO_KEYWORDS) #set(QAPPLICATION_CLASS QApplication1 CACHE STRING "Inheritance class for SingleApplication") find_package(PkgConfig REQUIRED) find_package(KF5KIO) find_package(KF5WindowSystem) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Widgets DBus) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Widgets DBus) pkg_check_modules(KYSDKWAYLANDHELPER_PKG kysdk-waylandhelper) pkg_check_modules(GIO REQUIRED gio-unix-2.0) include_directories(${GIO_INCLUDE_DIRS}) include_directories(src src/core src/utils src/dbusinterfaces src/dbusservices) include_directories(../) set (SRC_Sources src/core/appcgroup.cpp src/core/appinfo.cpp src/core/appinfomanager.cpp src/core/appstatusmanager.cpp src/core/basecontroller.cpp src/core/eventwatcher.cpp src/core/policy.cpp src/core/timerwheel.cpp src/dbusinterfaces/statusmanagerinterface.cpp src/main.cpp src/appmanager.cpp src/dbusinterfaces/cgroupinterface.cpp src/dbusinterfaces/kwininterface.cpp src/dbusservices/applauncher.cpp src/dbusservices/applauncherdaemon.cpp src/dbusservices/appmanagerservice.cpp src/dbusservices/whitelistmanager.cpp src/memorymonitor.cpp src/utils/misc.cpp src/utils/processinfo.cpp ) set (SRC_Headers src/core/appcgroup.h src/core/appinfo.h src/core/appinfomanager.h src/core/appstatusmanager.h src/core/basecontroller.h src/core/eventwatcher.h src/core/policy.h src/core/timerwheel.h src/dbusinterfaces/statusmanagerinterface.h src/appmanager.h src/dbusinterfaces/cgroupinterface.h src/dbusinterfaces/kwininterface.h src/dbusservices/applauncher.h src/dbusservices/applauncherdaemon.h src/dbusservices/appmanagerservice.h src/dbusservices/whitelistmanager.h src/memorymonitor.h src/utils/misc.h src/utils/processinfo.h) qt5_add_dbus_interface(SRC_Sources configs/com.kylin.ProcessManager.xml processmanagerinterface) add_executable(kylin-process-manager ${SRC_Sources} ${SRC_Headers} ) target_include_directories(kylin-process-manager PRIVATE ${KYSDKWAYLANDHELPER_PKG_INCLUDE_DIRS}) target_link_directories(kylin-process-manager PRIVATE ${KYSDKWAYLANDHELPER_PKG_LIBRARY_DIRS}) target_link_libraries(kylin-process-manager Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::DBus ${GIO_LIBRARIES} ${KYSDKWAYLANDHELPER_PKG_LIBRARIES} KF5::KIOCore KF5::WindowSystem SingleApplication::SingleApplication cgroup proc2 spdlog ukui-log4qt) install(TARGETS kylin-process-manager DESTINATION /usr/bin) kylin-process-manager-4.0.0.0/process-manager/src/0000755000175000017500000000000014536025312021167 5ustar debiandebiankylin-process-manager-4.0.0.0/process-manager/src/memorymonitor.h0000644000175000017500000000216514536025312024264 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef MEMORYMONITOR_H #define MEMORYMONITOR_H #include #include class MemoryMonitor : public QObject { Q_OBJECT public: explicit MemoryMonitor(QObject *parent = nullptr); void startTime(int time); Q_SIGNALS: void lowMemoryWaining(); public Q_SLOTS: void getSystemInfo(); private: QTimer *m_timer; double m_memtotal; double m_memavailable; double m_available; }; #endif // MEMORYMONITOR_H kylin-process-manager-4.0.0.0/process-manager/src/childrenprocesswatcher.h0000644000175000017500000000204614536025312026107 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef CHILDRENPROCESSWATCHER_H #define CHILDRENPROCESSWATCHER_H #include class QFileSystemWatcher; class ChildrenProcessWatcher : public QObject { Q_OBJECT public: explicit ChildrenProcessWatcher(QObject *parent = nullptr); public Q_SLOTS: void start(); signals: private: void handleEvents(int fd); }; #endif // CHILDRENPROCESSWATCHER_H kylin-process-manager-4.0.0.0/process-manager/src/common.h0000644000175000017500000000210214536025312022623 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef COMMON_H #define COMMON_H #define current_func __FUNCTION__ namespace common { template class Singleton { public: static T& GetInstance() { static T instance; return instance; } protected: virtual ~Singleton() {} Singleton() {} Singleton(const Singleton&) {} Singleton& operator =(const Singleton&) {} }; } // namespace common #endif kylin-process-manager-4.0.0.0/process-manager/src/core/0000755000175000017500000000000014536025312022117 5ustar debiandebiankylin-process-manager-4.0.0.0/process-manager/src/core/policy.cpp0000644000175000017500000001336714536025312024134 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "policy.h" #include #include #include #include #include #include #include #include #include Policy::Policy(QObject *parent) : QObject(parent) , m_statusSwitchInterval(1800) , m_confFileWatcher(new QFileSystemWatcher(this)) { m_confFileWatcher->addPath(kConfFilename); readConf(); connect(m_confFileWatcher, &QFileSystemWatcher::fileChanged, this, &Policy::readConf); } bool Policy::isWaylandPlatform() { return QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive); } QList Policy::features() { } bool Policy::featureEnabled(bool tablet, Policy::Feature feat) { } int Policy::statusSwitchInterval() const { return m_statusSwitchInterval; } Policy::AppStatus Policy::appStatus(bool appAvtived, bool appMinimized) { if (appAvtived) { return ActiveAppAdj; } if (!appMinimized) { return ForegroundAppAdj; } return BackendAppAdj; } Policy::AppStatus Policy::nextAppStatus(Policy::AppStatus currentStatus) { QMetaEnum meta = QMetaEnum::fromType(); for (int i=0; i> Policy::actions(Feature feat, ResourceUrgency urgency) { if (!m_actions.contains(feat)) { return QList>(); } QList> actions; int nUrg = urgency; do { actions.push_back(m_actions.value(feat).value((ResourceUrgency)nUrg)); } while(--nUrg >= 0); return actions; } int Policy::defaultCGroupVale(const QString &name) { if (name == CGROUP_FREEZE) { return 0; } if (name == CPU_MAX) { return 100000; } if (name.endsWith("weight")) { return 100; } return -1; } void Policy::readConf() { QFile confFile(kConfFilename); if (!confFile.open(QIODevice::ReadOnly)) { qWarning() << "Open config file failed, " << confFile.errorString(); return; } QJsonDocument jsonDoc = QJsonDocument::fromJson(confFile.readAll()); QJsonObject jsonObj = jsonDoc.object(); if (jsonObj.isEmpty()) { qWarning() << "Config file error, the json object is empty. " << jsonObj; return; } m_statusSwitchInterval = jsonObj.value("statusSwitchInterval").toInt(); if (m_statusSwitchInterval == 0) { m_statusSwitchInterval = 1800; } auto ruleSets = jsonObj.value("rulesets").toObject(); if (ruleSets.isEmpty()) { qWarning() << "Config file error, the 'rulesets' is empty."; return; } if (!ruleSets.contains("actions")) { qWarning() << "Config file error, there is no 'actions' key."; return; } auto actions = ruleSets.value("actions").toArray(); if (actions.isEmpty()) { qWarning() << "Config file error, 'actions' is empty."; return; } QMetaEnum featureMeta = QMetaEnum::fromType(); QMetaEnum urgencyMeta = QMetaEnum::fromType(); for (auto const &action : qAsConst(actions)) { auto actionObj = action.toObject(); if (actionObj.isEmpty()) { qWarning() << "Config file error, action " << action << " is empty."; continue; } if (!actionObj.contains("resource")) { qWarning() << "Config file error, there is no 'resource' key."; continue; } auto resource = actionObj.value("resource"); int enumResource = featureMeta.keyToValue(resource.toString().toLocal8Bit().data()); if (enumResource == -1) { qWarning() << "Config file error, cat not detect the 'resource' key: " << resource; continue; } auto it = actionObj.constBegin(); CGroupUrgencyStatValue urgencyStatValue; while (it != actionObj.constEnd()) { if (it.key() == "resource") { ++ it; continue; } auto urgencyValus = it.value().toArray(); if (urgencyValus.size() != urgencyMeta.keyCount()) { qWarning() << "Config file error, urgemcy values count error, " << urgencyValus; ++ it; continue; } for (int i=0; i. */ #include "appinfomanager.h" #include #include #include #include #include "utils/misc.h" #include "core/appstatusmanager.h" #include "common.h" AppInfoManager::AppInfoManager(QObject *parent) : QObject(parent) , m_appStatusManager(new AppStatusManager(this)) , m_desktopFileWatcher(new QFileSystemWatcher(this)) { initDesktopExecsInfoWatcher(); initConnections(); } QString AppInfoManager::newAppInstance(const QString &desktopFile, const QStringList &args, const QList &pids, appinfo::AppType type) { qDebug() << current_func << desktopFile << pids; if (desktopFile.isEmpty() || pids.isEmpty()) { qWarning() << current_func << "desktopFile or forkPid is inValid!" << desktopFile << " " << pids; return QString(); } QString appId = appIdByDesktopFile(desktopFile); if (appId.isEmpty()) { qWarning() << current_func << "desktopFile or forkPid is inValid!" << desktopFile << " " << pids; return QString(); } QString cgroupName = (type == appinfo::Normal && !kNoCgroupApps.contains(desktopFileName(desktopFile))) ? m_appStatusManager->newAppInstance(appId, pids) : ""; appinfo::AppInstance appInstance; appInstance.pids = pids; appInstance.args = args; appInstance.cgroupName = cgroupName; appInstance.instName = cgroupName.isEmpty() ? QString("kylin-app-manager/%1/instance_%2").arg(appId).arg(pids.last()) : cgroupName; appInstance.appRunningStaus = appinfo::Launching; appInstance.appStatus = Policy::ActiveAppAdj; appInstance.launchTimestamp = QDateTime::currentSecsSinceEpoch(); if (appInfos.contains(appId)) { appInfos[appId].appendAppInstance(appInstance); } else { appinfo::AppInfo appInfo(appId, desktopFile, type); appInfo.appendAppInstance(appInstance); appInfos[appId] = appInfo; } return appInstance.instName; } QString AppInfoManager::desktopFile(int pid) { auto appInfo = iterateThroughAppInstances([pid](const appinfo::AppInstance &instance)->bool { return instance.pids.contains(pid); }); return appInfos.value(appInfo.first).desktopFile(); } QString AppInfoManager::desktopFile(quint32 wid) { auto appInfo = iterateThroughAppInstances([wid](const appinfo::AppInstance &instance)->bool { return instance.wids.contains(wid); }); return appInfos.value(appInfo.first).desktopFile(); } bool AppInfoManager::thawApp(int pid) { auto appInfo = iterateThroughAppInstances([pid](const appinfo::AppInstance &instance)->bool { return instance.pids.contains(pid); }); return m_appStatusManager->thawApp(appInfo.first, appInfo.second.cgroupName); } bool AppInfoManager::thawApp(quint32 wid) { auto appInfo = iterateThroughAppInstances([wid](const appinfo::AppInstance &instance)->bool { return instance.wids.contains(wid); }); return m_appStatusManager->thawApp(appInfo.first, appInfo.second.cgroupName); } QList AppInfoManager::wids(const QString &desktopFile) { if (desktopFile.isEmpty()) { qWarning() << current_func << "desktopFile is empty!"; return QList (); } QString appId = appIdByDesktopFile(desktopFile); if (appId.isEmpty() || !appInfos.contains(appId)) { qWarning() << current_func << "appId is empty!" << desktopFile; return QList (); } QList appWids; auto appInfo = appInfos.value(appId); for (int i=0; i < appInfo.instances().count(); ++i) { if (!appInfo.instances().at(i).wids.isEmpty()) { appWids << appInfo.instances().at(i).wids; } } return appWids; } QList AppInfoManager::wids(const QString &desktopFile, const QString &instName) { if (desktopFile.isEmpty()) { qWarning() << current_func << "desktopFile is empty!"; return QList (); } QString appId = appIdByDesktopFile(desktopFile); if (appId.isEmpty() || !appInfos.contains(appId)) { qWarning() << current_func << "appId is empty!" << desktopFile; return QList (); } auto appInfo = appInfos.value(appId); for (int i=0; i < appInfo.instances().count(); ++i) { if (appInfo.instances().at(i).instName == instName) { return appInfo.instances().at(i).wids; } } return QList (); } QList AppInfoManager::pids(const QString &desktopFile, const QString &instName) { if (desktopFile.isEmpty()) { qWarning() << current_func << "desktopFile is empty!"; return QList (); } QString appId = appIdByDesktopFile(desktopFile); if (appId.isEmpty() || !appInfos.contains(appId)) { qWarning() << current_func << "appId is empty!" << desktopFile; return QList (); } auto appInfo = appInfos.value(appId); for (int i=0; i < appInfo.instances().count(); ++i) { if (appInfo.instances().at(i).instName == instName) { return appInfo.instances().at(i).pids; } } return QList (); } QString AppInfoManager::cmdline(int pid) { if (pid <= 0) { qWarning() << current_func << "pid is inValid!" << pid; return QString(); } QString cmdLineFile = QString("/proc/%1/cmdline").arg(pid); QFile file(cmdLineFile); if (!file.open(QIODevice::ReadOnly)) { qWarning() << "Get cmdline failed, " << pid << file.errorString(); return QString(); } QByteArray cmd = file.readAll(); QString cmdLine; int startIndex = 0; for (int i=0; i AppInfoManager::runningStatus( const QString &desktopFile, const QStringList &args) const { if (desktopFile.isEmpty()) { return std::make_tuple(appinfo::RunningStatus::None, "", false); } QString appId = appIdByDesktopFile(desktopFile); if (!appInfos.contains(appId)) { return std::make_tuple(appinfo::RunningStatus::None, "", false); } bool hasLaunchingStatus = false; bool launchingTimeout = false; QString launchingInstName; auto appInfo = appInfos.value(appId); auto instances = appInfo.instances(); for (const auto &instance : qAsConst(instances)) { if (args.isEmpty()) { if (instance.appRunningStaus == appinfo::RunningStatus::Running) { return std::make_tuple(appinfo::RunningStatus::Running, instance.instName, false); } if (instance.appRunningStaus == appinfo::RunningStatus::Launching) { if (instance.appRunningStaus == appinfo::RunningStatus::Launching) { launchingTimeout = (QDateTime::currentSecsSinceEpoch() - instance.launchTimestamp > 5); } launchingInstName = instance.instName; hasLaunchingStatus = true; } } else if (instance.args == args) { if (instance.appRunningStaus == appinfo::RunningStatus::Launching) { launchingTimeout = (QDateTime::currentSecsSinceEpoch() - instance.launchTimestamp > 5); } return std::make_tuple(instance.appRunningStaus, instance.instName, launchingTimeout); } } if (hasLaunchingStatus) { return std::make_tuple(appinfo::RunningStatus::Launching, launchingInstName, launchingTimeout); } return std::make_tuple(appinfo::RunningStatus::None, "", false); } QString AppInfoManager::inhibit(const QString &desktopFile, uint pid, const QString &reason, uint flags) { QString appId = appIdByDesktopFile(desktopFile); if (appId.isEmpty()) { return QString(); } if (flags & inhibitor::FrozenInhibitor) { thawApp((int)pid); QByteArray srcData = QString(appId + QString::number(QDateTime::currentMSecsSinceEpoch()) + QString::number(pid)).toLocal8Bit(); QString cookie = QCryptographicHash::hash(srcData, QCryptographicHash::Md5).toHex().constData(); qInfo() << "Add frozen inhibitor," << desktopFile << pid << reason; m_inhibitors.push_back({ appId, cookie, (int)pid, inhibitor::FrozenInhibitor }); return cookie; } return QString(); } void AppInfoManager::unInhibit(const QString &cookie) { QString appId; appinfo::AppInstance appInstance; for (int i=0; iappStatusTimeOutCallback({ appId, appInstance.instName, appInstance.appStatus }); } bool AppInfoManager::isInhibited(const QString &appId, const QString &instName) { for (auto &inhibitor : qAsConst(m_inhibitors)) { if (appId != inhibitor.appId) { continue; } int pid = inhibitor.pid; auto ret = iterateThroughAppInstances([pid](const appinfo::AppInstance &instance)->bool { return instance.pids.contains(pid); }); if (ret.first.isEmpty()) { continue; } if (ret.second.instName == instName) { return true; } } return false; } bool AppInfoManager::ThawApps() { return m_appStatusManager->ThawApps(); } void AppInfoManager::onActiveWindowChanged(quint32 currentWid, int currentPid, quint32 preWid, int prePid) { Q_UNUSED(prePid) auto currentApp = appInstanceInfoWithWindowId(currentWid); auto preApp = appInstanceInfoWithWindowId(preWid); if (currentApp.second == preApp.second) { return; } bool preAppIsHidden = appInfos.value(preApp.first).appInstanceIsHidden(preApp.second); m_appStatusManager->activeAppChanged(currentApp.first, currentApp.second, preApp.first, preApp.second, preAppIsHidden); } void AppInfoManager::onWindowAdded(quint32 wid, int pid) { KWindowInfo winfo(wid, NET::WMAllProperties); qDebug() << current_func << wid << pid << winfo.desktopFileName() << winfo.visibleName(); if (wid <= 0 || pid <= 0) { qWarning() << current_func << "wid or pid is inValid!" << wid << pid; return; } // kmre应用窗口添加时将应用desktop名称添加到map中 qDebug() << current_func << wid << pid; QString appCmdline = cmdline(pid); // 过滤peony-qt-desktop桌面 if (appCmdline.contains("peony-qt-desktop")) { return; } if (appCmdline.startsWith(kKmreAppMainWindow)) { QString androidAppName = findAndroidNameByWid(wid); if (androidAppName.isEmpty()) { androidAppName = findAndroidNameByPid(pid); } if (androidAppName.isEmpty()) { qWarning() << current_func << "androidAppName is empty!" << wid << pid; return; } QString fullDesktopName = desktopFullFilename(androidAppName); QString appId = appIdByDesktopFile(fullDesktopName); // kmre应用主进程被gio拉起来后就finish掉了,只留下kmre窗口进程 addAppInfomations(fullDesktopName, appCmdline, wid, pid); return; } // 匹配现有保存的pid auto appIdInstance = appInstanceInfoWithPid(pid); if (!appIdInstance.first.isEmpty()) { updateInstanceInfos(appIdInstance, wid, pid); return; } // 从cgroup组内找pid QString cgroupName = m_appStatusManager->cgroupWithPid(pid); if (!cgroupName.isEmpty()) { appIdInstance = appInstanceInfoWithInstName(cgroupName); if (!appIdInstance.first.isEmpty()) { updateInstanceInfos(appIdInstance, wid, pid); return; } } // 匹配cmdline appIdInstance = findDesktopCmdline(pid); if (!appIdInstance.first.isEmpty()) { updateInstanceInfos(appIdInstance, wid, pid); return; } // 匹配全局desktop QString desktopFile = findGlobalDesktopFile(appCmdline); if (!desktopFile.isEmpty()) { addAppInfomations(desktopFile, appCmdline, wid, pid); return; } // dpkg匹配 auto deskfilesExec = m_desktopsExec; desktopFile = desktopFileFromCmdline(appCmdline); if (desktopFile.isEmpty()) { qWarning() << current_func << "desktopFile is empty!" << appCmdline; return; } if (!deskfilesExec.contains(desktopFile)) { qWarning() << current_func << "deskfilesExec is not contains " << desktopFile; deskfilesExec[desktopFile] = {appCmdline, QStringList() << appCmdline}; } else { deskfilesExec[desktopFile].first = appCmdline; } addAppInfomations(desktopFile, appCmdline, wid, pid); } void AppInfoManager::onWindowRemoved(quint32 wid) { qWarning() << current_func << wid; if (wid == 0) { qWarning() << current_func << "wid or pid is inValid!" << wid; return; } auto ret = iterateThroughAppInstances([wid](const appinfo::AppInstance &instance)->bool { return instance.wids.contains(wid); }); auto appId = ret.first; auto appInstance = ret.second; if (!appId.isEmpty()) { appInfos[appId].removeAppWid(appInstance.instName, wid); if (appInfos.value(appId).appInstanceWids(appInstance.instName).isEmpty()) { appInfos[appId].setInstanceRunningStatus(appInstance.instName, appinfo::Finished); m_appStatusManager->appWidsIsEmpty(appId, appInstance.instName); } return; } qWarning() << current_func << "Can not find the removed window: " << wid; } void AppInfoManager::onMprisDbusAdded(int pid, const QString &dbusService) { if (pid <= 0 || dbusService.isEmpty()) { qWarning() << current_func << "pid or mpris dbus service is inValid!" << pid << dbusService; return; } // 应用启动就创建该dbus服务的话,通常情况下,该接口会先于onWindowAdded被调用 auto appIdInstance = appInstanceInfoWithPid(pid); if (appIdInstance.first.isEmpty()) { m_tmpMprisDbus[pid] = dbusService; return; } appInfos[appIdInstance.first].setMprisDbus(appIdInstance.second, dbusService); } void AppInfoManager::updateDesktopExecsInfo(const QString &path) { QDir dir(path); if (!dir.exists()) { return; } QStringList desktopFiles = dir.entryList(QStringList() << "*.desktop"); for (const auto &desktopFileName : qAsConst(desktopFiles)) { QString deskopFile = path + "/" + desktopFileName; auto exec = desktopFileExec(deskopFile); { if (exec.first.isEmpty()) { qWarning() << current_func << "exec is empty!" << desktopFileName; continue; } QMutexLocker locker(&m_mutex); if (!m_desktopsExec.contains(deskopFile)) { m_desktopsExec[deskopFile] = exec; } } } } void AppInfoManager::onResourceThresholdWarning(Policy::Feature resource, Policy::ResourceUrgency level) { m_appStatusManager->resourceThresholdWarning(resource, level); } void AppInfoManager::onTabletModeChanged(bool tabletMode) { m_appStatusManager->tabletModeChanged(tabletMode); } void AppInfoManager::onChildPidFinished(const int &pid) { qDebug() << current_func << pid; if (pid <= 0) { qWarning() << current_func << "pid is not valid!"; return; } if (appInfos.isEmpty()) { qWarning() << current_func << "m_appsInfo is empty!" << pid; return; } auto appInfo = iterateThroughAppInstances([pid](const appinfo::AppInstance &instance)->bool { return instance.pids.contains(pid); }); auto appId = appInfo.first; auto instanceName = appInfo.second.instName; if (appId.isEmpty()) { return; } appInfos[appId].removePid(instanceName, pid); qDebug() << " app finished.... " << instanceName << appInfos[appId].pids(instanceName).isEmpty(); if (appInfos[appId].pids(instanceName).isEmpty()) { appInfos[appId].removeAppInstance(instanceName); m_appStatusManager->appFinished(appId, instanceName); qDebug() << " app finished.... " << instanceName; } } void AppInfoManager::onAboutToQuitApp() { m_appStatusManager->aboutToQuitApp(); } QString AppInfoManager::appIdByDesktopFile(const QString &desktopFile) const { if (desktopFile.isEmpty()) { qWarning() << current_func << "desktopFile is empty!"; return QString(); } QString desktopFileName = desktopFullFilename(desktopFile); QFile file(desktopFileName); if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) { qWarning() << "file open error!" << desktopFile; file.close(); return QString(); } QByteArray hashData = QCryptographicHash::hash(file.readAll(), QCryptographicHash::Md5); if (hashData.isEmpty()) { qWarning() << "hashData is empty!" << desktopFile; file.close(); return QString(); } file.close(); return QString(hashData.toHex().constData()); } QString AppInfoManager::desktopFileName(const QString &desktopFile) { if (!desktopFile.endsWith(".desktop")) { return QString(); } if (!QFile::exists(desktopFile)) { return desktopFile; } auto items = desktopFile.split("/"); if (items.isEmpty()) { return desktopFile; } return items.last(); } QPair AppInfoManager::desktopFileExec(const QString &desktopFileName) { if (desktopFileName.isEmpty()) { qWarning() << current_func << "desktopFileName is NULL."; return QPair(); } QString fullDesktopName = desktopFullFilename(desktopFileName); KDesktopFile desktop(fullDesktopName); QString exec = desktop.entryMap("Desktop Entry").value("Exec"); if (exec.isEmpty()) { qWarning() << current_func << "Exec is empty!" << desktopFileName; return {QString(), QStringList()}; } exec.remove(QRegExp("%.")); exec.remove(QRegExp("^\"")); exec.remove(QRegExp(" *$")); exec.remove(QRegExp("\"")); QStringList execArgs = exec.split(" "); if (execArgs.count() > 0) { exec = execArgs.first(); } return { exec, execArgs }; } QString AppInfoManager::desktopFullFilename(const QString &desktopFileName) const { QFile desktopFile(desktopFileName); if (desktopFile.exists()) { return desktopFileName; } QStringList desktopfilePaths = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation); for (auto &path : qAsConst(desktopfilePaths)) { if (desktopFileName.contains(path)) { return desktopFileName; } else if (QFile::exists(path + "/" + desktopFileName)) { return path + "/" + desktopFileName; } } return QString(); } AppInfoManager::AppIdInstanceName AppInfoManager::appInstanceInfoWithWindowId(quint32 wid) { auto ret = iterateThroughAppInstances([wid] (const appinfo::AppInstance &instance)->bool { return instance.wids.contains(wid); }); return { ret.first, ret.second.instName }; } AppInfoManager::AppIdInstanceName AppInfoManager::appInstanceInfoWithPid(int pid) { auto ret = iterateThroughAppInstances([pid] (const appinfo::AppInstance &instance)->bool { return instance.pids.contains(pid); }); return { ret.first, ret.second.instName }; } AppInfoManager::AppIdInstanceName AppInfoManager::appInstanceInfoWithInstName(const QString &instName) { auto ret = iterateThroughAppInstances([instName] (const appinfo::AppInstance &instance)->bool { return instance.instName == instName; }); return { ret.first, ret.second.instName }; } appinfo::AppInstance AppInfoManager::appInstanceWithPid(int pid) { auto ret = iterateThroughAppInstances([pid] (const appinfo::AppInstance &instance)->bool { return instance.pids.contains(pid); }); return ret.second; } QString AppInfoManager::findAndroidNameByWid(const quint32 &wid) { if (wid <= 0) { qWarning() << current_func << "wid is not valid!"; return QString(); } if (!QDBusConnection::sessionBus().isConnected()) { qWarning() << current_func << "Cannot connect to the D-Bus session bus." << wid; return QString(); } QDBusInterface kmreIface("cn.kylinos.Kmre.Window", "/cn/kylinos/Kmre/Window", "cn.kylinos.Kmre.Window", QDBusConnection::sessionBus()); if (!kmreIface.isValid()) { qWarning() << current_func << "kmreIface is not valid!" << wid; return QString(); } QDBusReply reply = kmreIface.call("getAppNameFromWid", wid); if (!reply.isValid()) { qWarning() << current_func << "reply is not valid!" << wid; return QString(); } if (!reply.value().endsWith(".desktop")) { return reply.value().append(".desktop"); } return reply.value(); } QPair AppInfoManager::findDesktopCmdline(const int &pid) { if (pid <= 0) { qWarning() << current_func << "pid is not valid!"; return QPair(); } QString appCmdLine = cmdline(pid); if (appCmdLine.isEmpty()) { qWarning() << current_func << "appCmdLine is empty!" << pid; return QPair(); } if (appInfos.isEmpty()) { qWarning() << current_func << "appInfos is empty!" << pid; return QPair(); } auto it = appInfos.constBegin(); while (it != appInfos.constEnd()) { auto appList = it.value(); for (const auto &appInfo : appList.instances()) { QStringList execArgs = appInfo.args; QString exec; for (auto &subStr : execArgs) { // 网页版应用Exec中带"xdg-open",appCmdLine中不带需要去除 if (subStr != "xdg-open") { exec.append(subStr); exec.append(" "); } } if (exec.endsWith(" ")) { exec.remove(exec.size() - 1, 1); } // 移除字符串首尾的空格 exec = exec.trimmed(); if (appCmdLine.contains(exec)) { return {it.key(), appInfo.instName}; } // software center appCmdLine.remove("./"); if (exec.endsWith(appCmdLine)) { return {it.key(), appInfo.instName}; } } ++ it; } return QPair(); } QString AppInfoManager::findGlobalDesktopFile(const QString &cmdline) { if (cmdline.isEmpty()) { qWarning() << current_func << "cmdline is empty!"; return QString(); } auto desktopsExec = m_desktopsExec; auto desktopExecIt = std::find_if(desktopsExec.constBegin(), desktopsExec.constEnd(), [cmdline](const QPair &exec) { QString strExec; for (const auto &str : exec.second) { strExec.push_back(str); strExec.push_back(" "); } if (strExec.endsWith(" ")) { strExec.remove(strExec.size()-1, 1); } // eg. peony-qt-desktop vs peony // return cmdline.contains(strExec); return cmdline == strExec; }); if (desktopExecIt != desktopsExec.constEnd()) { return desktopExecIt.key(); } return QString(); } QString AppInfoManager::desktopFileFromCmdline(QString cmdline) { if (cmdline.isEmpty()) { qWarning() << current_func << "cmdline is empty!"; return QString(); } if (cmdline.contains("./")) { cmdline.remove("./"); } QString packageName; if (!QDir::isAbsolutePath(cmdline)) { QString path = QProcessEnvironment::systemEnvironment().value("PATH"); QStringList pathLists; pathLists << path.split(':'); QString filePath; QString appCmdline = cmdline; for (int i=0; i AppInfoManager::findChildrenPids(const int &pid) { if (pid <= 0) { qWarning() << current_func << "pid is invalid!"; return QList (); } QList pids; QList childrenPids; std::string appChildrenPid = "pgrep -P " + QString::number(pid).toStdString(); FILE *fp = popen(appChildrenPid.c_str(), "r"); char nameBuff[200] = ""; for (int i = 0; fgets(nameBuff, 200, fp) != NULL; ++i) { std::string childrenInfo = nameBuff; std::string appChildrenInfo = std::string(childrenInfo.begin(), childrenInfo.begin() + childrenInfo.find('\n')); if (appChildrenInfo.empty()) { continue; } childrenPids << QString::fromStdString(appChildrenInfo).toInt(); pids << QString::fromStdString(appChildrenInfo).toInt(); } pclose(fp); if (pids.empty()) { qDebug() << current_func << "child pid is " << childrenPids; return childrenPids; } for (const auto &pid : qAsConst(pids)) { findChildrenPids(pid); } return QList (); } void AppInfoManager::updateAppInfomations(const QString &fullDesktopName, const quint32 &wid, const int &pid) { if (fullDesktopName.isEmpty() || wid <= 0 || pid <= 0) { qWarning() << current_func << "fullDesktopName wid or pid is inValid!" << fullDesktopName << wid << pid; return; } QString appId = appIdByDesktopFile(fullDesktopName); if (!appInfos.contains(appId)) { qWarning() << "appInfos is not contains " << appId; return; } QString desktopName = fullDesktopName.trimmed().split("/").last(); auto appIdInstance = appInstanceInfoWithPid(pid); if (appIdInstance.first.isEmpty()) { qWarning() << current_func << "appId is empty!" << fullDesktopName; return; } QString appInstName = appIdInstance.second; appInfos[appId].appendAppWid(appInstName, wid); appInfos[appId].setInstanceRunningStatus(appInstName, appinfo::Running); if (isKmreApp(fullDesktopName)) { appInfos[appId].setInstanceAppType(appInstName, appinfo::Kmre); Q_EMIT appLaunched(desktopName, pid, uint(wid)); qDebug() << current_func << "appLaunched signal " << desktopName << wid << pid; return; } appInfos[appId].setInstanceAppType(appInstName, appinfo::Normal); Q_EMIT appLaunched(desktopName, pid, wid); qDebug() << current_func << "appLaunched signal " << desktopName << wid << pid; } void AppInfoManager::addAppInfomations(const QString &fullDesktopName, const QString &cmdline, const quint32 &wid, const int &pid) { if (fullDesktopName.isEmpty() || cmdline.isEmpty() || wid <= 0 || pid <= 0) { qWarning() << current_func << "fullDesktopName cmdline wid or pid is inValid!" << fullDesktopName << cmdline << wid << pid; return; } QString appId = appIdByDesktopFile(fullDesktopName); QList childrenPids = findChildrenPids(pid); childrenPids.append(pid); QString instName; QString desktopName = fullDesktopName.trimmed().split("/").last(); if (isKmreApp(fullDesktopName)) { instName = newAppInstance(fullDesktopName, QStringList() << cmdline, QList() << childrenPids, appinfo::Kmre); } else { instName = newAppInstance(fullDesktopName, QStringList() << cmdline, QList() << childrenPids, appinfo::Normal); } appInfos[appId].appendAppWid(instName, wid); appInfos[appId].setInstanceRunningStatus(instName, appinfo::Running); Q_EMIT appLaunched(desktopName, pid, wid); qDebug() << current_func << "appLaunched signal " << desktopName << wid << pid; } bool AppInfoManager::isKmreApp(const QString &fullDesktopName) { if (fullDesktopName.isEmpty()) { qWarning() << current_func << "fullDesktopName is empty!"; return false; } else { QScopedPointer desktop(new QSettings(fullDesktopName, QSettings::IniFormat)); desktop->setIniCodec(QTextCodec::codecForName("UTF-8")); QString categories = desktop->value("Desktop Entry/Categories").toString(); categories.remove(QRegExp("%.")); categories.remove(QRegExp("^\"")); categories.remove(QRegExp(" *$")); return categories.contains("Android") || categories.contains("Apk"); } return false; } void AppInfoManager::updateInstanceInfos(const AppIdInstanceName &appIdInstance, const quint32 &wid, const int &pid) { if (wid <= 0 || pid <= 0) { qWarning() << current_func << "wid or pid is inValid!" << wid << pid; return; } QString appId = appIdInstance.first; QString instName = appIdInstance.second; if (!appId.isEmpty() && !instName.isEmpty()) { QString desktopName = appInfos[appId].desktopFile().trimmed().split("/").last(); appInfos[appId].appendAppWid(instName, wid); appInfos[appId].setInstanceRunningStatus(instName, appinfo::Running); if (m_tmpMprisDbus.contains(pid)) { appInfos[appId].setMprisDbus(instName, m_tmpMprisDbus.value(pid)); m_tmpMprisDbus.remove(pid); } appInfos[appId].appendPid(instName, pid); Q_EMIT appLaunched(desktopName, pid, wid); qDebug() << current_func << "appLaunched signal " << desktopName << wid << pid; } } QString AppInfoManager::findAndroidNameByPid(const quint32 &pid) { if (pid <= 0) { qWarning() << current_func << "pid is inValid!"; return QString(); } QString appCmdline = cmdline(pid); QString startStr = "-p"; QString endStr = "-f"; int startIndex = appCmdline.indexOf(startStr); int endIndex = appCmdline.indexOf(endStr); if (startIndex == -1 || endIndex == -1 || startIndex+2 > appCmdline.size() || endIndex-startIndex-2 > appCmdline.size()) { qWarning() << current_func << "appCmdline index is inValid!" << pid << appCmdline; return QString(); } QString androidName = appCmdline.mid(startIndex + 2, endIndex - startIndex - 2).trimmed().split("/").last(); if (!androidName.endsWith(".desktop")) { androidName.push_back(".desktop"); } return androidName; } QPair AppInfoManager::iterateThroughAppInstances( const std::function &callback) { for (auto &appInfo : qAsConst(appInfos)) { const auto &instances = appInfo.instances(); for (auto &instance : qAsConst(instances)) { if (callback(instance)) { return { appInfo.appId(), instance }; } } } return QPair(); } void AppInfoManager::initDesktopExecsInfoWatcher() { QStringList desktopDirs = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation); qDebug() << desktopDirs; auto appExecFunction = [desktopDirs, this]() { for (const auto &desktopDir : desktopDirs) { updateDesktopExecsInfo(desktopDir); } }; QtConcurrent::run(appExecFunction); m_desktopFileWatcher->addPaths(desktopDirs); } void AppInfoManager::initConnections() { connect(m_desktopFileWatcher, &QFileSystemWatcher::directoryChanged, this, &AppInfoManager::updateDesktopExecsInfo); } kylin-process-manager-4.0.0.0/process-manager/src/core/timerwheel.h0000644000175000017500000000311014536025312024430 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef TIMERWHEEL_H #define TIMERWHEEL_H #include #include "policy.h" class QTimer; namespace timerwheel { struct AppData { QString appId; QString instName; Policy::AppStatus dstStatus; }; struct Timer { quint64 timerId; int rotation; int tickSlot; std::function callback; AppData appData; }; class TimerWheel : public QObject { public: explicit TimerWheel(QObject *parent = nullptr); quint64 addTimer(int timeout, AppData appData, const std::function &callback); bool deleteTimer(quint64 timerId); private Q_SLOTS: void tick(); private: QTimer *m_timer; int m_currentSlot; quint64 m_currentTimerCount; QMap> m_slotTimers; const int kSi = 1; // 1s const int kSlotNumber = 60; }; } // namsapce timerwheol; #endif // TIMERWHEEL_H kylin-process-manager-4.0.0.0/process-manager/src/core/appinfo.cpp0000644000175000017500000002024614536025312024263 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "appinfo.h" #include #include #include #include #include "common.h" namespace appinfo { AppInfo::AppInfo(QObject *parent) : QObject(parent) { } AppInfo::AppInfo(const QString &id, const QString &desktopFile, AppType appType, QObject *parent) : QObject(parent) , m_appId(id) , m_desktopFile(desktopFile) , m_appType(appType) { } AppInfo::AppInfo(const appinfo::AppInfo &other) : AppInfo(other.parent()) { this->m_instances = other.m_instances; this->m_appId = other.m_appId; this->m_desktopFile = other.m_desktopFile; this->m_appType = other.m_appType; } bool AppInfo::operator==(const appinfo::AppInfo &other) { return this->m_instances == other.m_instances && this->m_appId == other.m_appId && this->m_desktopFile == other.m_desktopFile && this->m_appType == other.m_appType; } AppInfo AppInfo::operator=(const AppInfo &other) { this->m_instances = other.m_instances; this->m_appId = other.m_appId; this->m_desktopFile = other.m_desktopFile; this->m_appType = other.m_appType; return *this; } QString AppInfo::appId() const { return m_appId; } QString AppInfo::desktopFile() const { return m_desktopFile; } AppType AppInfo::appType() const { return m_appType; } QString AppInfo::cgroupName(const QString &instName) const { for (auto const &instance : qAsConst(m_instances)) { if (instance.instName == instName) { return instance.cgroupName; } } return QString(); } QStringList AppInfo::cgroupNames(Policy::AppStatus status) const { QStringList cgroupNames; for (auto const &instance : qAsConst(m_instances)) { if (instance.appStatus == status) { cgroupNames.push_back(instance.cgroupName); } } return cgroupNames; } void AppInfo::setInstanceStatus(const QString &name, Policy::AppStatus status) { for (auto &instance : m_instances) { if (instance.instName == name || instance.cgroupName == name) { instance.appStatus = status; return; } } } int AppInfo::instanceStatus(const QString &name) { for (auto &instance : m_instances) { if (instance.instName == name || instance.cgroupName == name) { return instance.appStatus; } } return -1; } void AppInfo::setInstanceRunningStatus(const QString &name, RunningStatus status) { for (auto &instance : m_instances) { if (instance.instName == name || instance.cgroupName == name) { instance.appRunningStaus = status; return; } } } void AppInfo::setInstanceAppType(const QString &name, AppType type) { for (auto &instance : m_instances) { if (instance.instName == name || instance.cgroupName == name) { instance.appType = type; return; } } } QString AppInfo::mprisDbus(const QString &instName) const { for (auto const &instance : qAsConst(m_instances)) { if (instance.instName == instName) { return instance.mprisDbus; } } return QString(); } void AppInfo::setMprisDbus(const QString &instName, const QString &mprisDBusName) { for (auto &instance : m_instances) { if (instance.instName == instName) { instance.mprisDbus = mprisDBusName; return; } } } quint64 AppInfo::instanceTimerId(const QString &instName) const { for (auto &instance : m_instances) { if (instance.instName == instName) { return instance.timerId ; } } return 0; } void AppInfo::setInstanceTimerId(const QString &instName, quint64 timerId) { for (auto &instance : m_instances) { if (instance.instName == instName) { instance.timerId = timerId; return; } } } bool AppInfo::appInstanceIsHidden(const QString &instName) const { if (instName.isEmpty()) { return false; } for (auto const &inst : m_instances) { if (inst.instName != instName) { continue; } for (auto const &wid : inst.wids) { if (Policy::isWaylandPlatform()) { auto windowInfo = kdk::WindowManager::getwindowInfo(wid); return windowInfo.isMinimized(); } KWindowInfo windowInfo(wid, NET::WMState | NET::XAWMState); if (!windowInfo.hasState(NET::Hidden)) { return false; } } return true; } return true; } QList AppInfo::instances() const { return m_instances; } void AppInfo::appendAppInstance(const AppInstance &inst) { m_instances.append(inst); } void AppInfo::removeAppInstance(const QString &inst) { if (inst.isEmpty()) { qWarning() << current_func << "id or inst is inValid!" << inst; return; } for (int i=0; i AppInfo::appInstanceWids(const QString &inst) const { if (inst.isEmpty()) { return QList(); } for (auto const &instance : m_instances) { if (instance.instName == inst) { return instance.wids; } } return QList(); } void AppInfo::appendPid(const QString &inst, const int &pid) { if (inst.isEmpty() || pid <= 0) { qWarning() << current_func << "inst or wid is inValid!" << inst << pid; return; } for (auto &instance : m_instances) { if (instance.instName == inst) { if (instance.pids.contains(pid)) { continue; } instance.pids.push_back(pid); qDebug() << current_func << inst << instance.pids; return; } } } void AppInfo::removePid(const QString &inst, const int &pid) { if (inst.isEmpty() || pid <= 0) { qWarning() << current_func << "inst or pid is inValid!" << inst << pid; return; } for (auto &instance : m_instances) { if (instance.instName == inst) { instance.pids.removeOne(pid); return; } } } QList AppInfo::pids(const QString &inst) const { if (inst.isEmpty()) { qWarning() << current_func << "inst is inValid!" << inst; return QList(); } for (auto const &instance : m_instances) { if (instance.instName == inst) { return instance.pids; } } return QList(); } void AppInfo::removeAppWid(const QString &inst, const quint32 &wid) { if (inst.isEmpty() || wid <= 0) { qWarning() << current_func << "inst or wid is inValid!" << inst << wid; return; } for (auto &instance : m_instances) { if (instance.instName == inst) { instance.wids.removeOne(wid); return; } } } }// namespace appinfo kylin-process-manager-4.0.0.0/process-manager/src/core/appcgroup.cpp0000644000175000017500000000724214536025312024630 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "appcgroup.h" #include #include #include AppCGroup::AppCGroup(QObject *parent) : QObject(parent) , m_processManagerInterface(new com::kylin::ProcessManager("com.kylin.ProcessManager", "/com/kylin/ProcessManager", QDBusConnection::systemBus())) { initCGroup(); } QString AppCGroup::createProcessCGroup(const QString &appId, const QList &pids) { auto reply = m_processManagerInterface->CreateProcessCGroup(appId, pids); qDebug() << "reply is finished. " << reply.isFinished(); while (!reply.isFinished()) { qApp->processEvents(); usleep(10); } if (reply.isError()) { qWarning() << "createProcessCGroup error, " << reply.error(); return QString(); } if (reply.value().value(kDbusResult).toString().isEmpty()) { qWarning() << "createProcessCGroup error, " << reply.value().value(kDbusErrMsg).toString(); return QString(); } return reply.value().value(kDbusResult).toString(); } bool AppCGroup::deleteProcessCGroup(const QString &cgroupName) { auto reply = m_processManagerInterface->DeleteProcessCGroup(cgroupName); return true; } bool AppCGroup::setProcessCGroupResourceLimit(const QString &cgroupName, const QString &attrName, int value) { auto reply = m_processManagerInterface->SetProcessCGroupResourceLimit(cgroupName, attrName, value); return true; } bool AppCGroup::reclaimProcesses(const QList &pids) { auto reply = m_processManagerInterface->ReclaimProcesses(pids); return true; } bool AppCGroup::reclaimProcesses(const QString &cgroupName) { auto reply = m_processManagerInterface->ReclaimProcesses(cgroupName); return true; } QString AppCGroup::cgroupNameWithPid(int pid) { auto reply = m_processManagerInterface->CGroupNameWithPid(pid); while (!reply.isFinished()) { qApp->processEvents(); usleep(1); } if (reply.isError()) { qWarning() << __FUNCTION__ << "error, " << reply.error(); return QString(); } return reply.value().value(kDbusResult).toString(); } void AppCGroup::initCGroup() { QProcess p; p.start("id", QStringList() << "-u"); p.waitForFinished(); auto userId = p.readAll().toUInt(); auto rootPath = QString("user.slice/user-%1.slice/kylin-app-manager").arg(userId); auto reply = m_processManagerInterface->InitCGroup(rootPath); while (!reply.isFinished()) { qApp->processEvents(); usleep(1); } if (reply.isError()) { qWarning() << __FUNCTION__ << "error, " << reply.error(); return; } if (!reply.value().value(kDbusResult).toBool()) { qWarning() << "Init CGroup failed, " << reply.value().value(kDbusErrMsg).toString() << "the root path is " << rootPath; } qDebug() << reply.value().value(kDbusResult) << rootPath; } kylin-process-manager-4.0.0.0/process-manager/src/core/basecontroller.cpp0000644000175000017500000000153414536025312025644 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "basecontroller.h" BaseController::BaseController() { } int BaseController::stat() { } int BaseController::max() { } int BaseController::pressure() { } kylin-process-manager-4.0.0.0/process-manager/src/core/eventwatcher.cpp0000644000175000017500000001506214536025312025326 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "eventwatcher.h" #include #include "policy.h" bool EventWatcher::m_isTablet = false; EventWatcher::EventWatcher(QObject *parent) : QObject(parent) , m_statusManagerInterface(new com::kylin::statusmanager::interface("com.kylin.statusmanager.interface", "/", QDBusConnection::sessionBus(), this)) , m_kwinInterface(new org::ukui::KWin("org.ukui.KWin", "/KWin", QDBusConnection::sessionBus(), this)) , m_processManager(new com::kylin::ProcessManager("com.kylin.ProcessManager", "/com/kylin/ProcessManager", QDBusConnection::systemBus())) , m_appLauncherDaemon(new AppLauncherDaemon(this)) { if (!m_statusManagerInterface->isValid()) { m_isTablet = false; qWarning() << "com.kylin.statusmanager.interface is not valid."; } else { m_isTablet = m_statusManagerInterface->get_current_tabletmode(); } initConnections(); #ifdef QT_DEBUG // m_isTablet = true; #endif } bool EventWatcher::isTabletMode() { return m_isTablet; } void EventWatcher::onTabletModeChanged(bool tabletMode) { qDebug() << "onTabletModeChanged " << tabletMode; if (tabletMode == m_isTablet) { return; } m_isTablet = tabletMode; Q_EMIT tabletModeChanged(tabletMode); } void EventWatcher::onActiveWindowChanged(quint32 wid) { doWindowActiveChanged(wid); } void EventWatcher::onWindowAdded(quint32 wid) { KWindowInfo windowInfo(wid, NET::WMPid); int pid = windowInfo.pid(); if (pid <= 0) { qWarning() << __FUNCTION__ << "pid is inValid!"; return; } Q_EMIT windowAdded(wid, pid); } void EventWatcher::onWindowRemoved(quint32 wid) { Q_EMIT windowRemoved(wid); } void EventWatcher::onActiveWaylandWindowChanged(kdk::WindowId wid) { doWindowActiveChanged(wid.toUInt()); } void EventWatcher::onWaylandWindowAdded(kdk::WindowId wid) { int pid = kdk::WindowManager::getPid(wid); if (pid <= 0) { qWarning() << __FUNCTION__ << "pid is inValid!"; return; } Q_EMIT windowAdded(wid.toUInt(), pid); } void EventWatcher::onWaylandWindowRemoved(kdk::WindowId wid) { Q_EMIT windowRemoved(wid.toUInt()); } void EventWatcher::onDbusNameAcquired(QString name, QString newOwner, QString oldOwner) { Q_UNUSED(newOwner) Q_UNUSED(oldOwner) if (!name.contains(mprisPrefix)) { return; } QDBusInterface mediaInterface("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", QDBusConnection::sessionBus()); QDBusPendingReply pidReply = mediaInterface.call(QStringLiteral("GetConnectionUnixProcessID"), name); quint32 pid = pidReply.value(); if (pid > 0) { Q_EMIT mprisDbusAdded(pid, name); } } void EventWatcher::initConnections() { connect(m_appLauncherDaemon, &AppLauncherDaemon::aboutToQuitAppManager, this, &EventWatcher::onAboutToQuitApp); connect(m_appLauncherDaemon, &AppLauncherDaemon::childProcessFinished, this, &EventWatcher::onChildPidFinished); connect(m_statusManagerInterface, &com::kylin::statusmanager::interface::mode_change_signal, this, &EventWatcher::onTabletModeChanged); connect(m_processManager, &com::kylin::ProcessManager::ResourceThresholdWarning, this, &EventWatcher::onResourceThresholdWarning); QDBusConnection::sessionBus().connect("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "NameOwnerChanged", this, SLOT(onDbusNameAcquired(QString,QString,QString))); bool m_isWayland = Policy::isWaylandPlatform(); if (m_isWayland) { connect(kdk::WindowManager::self(), &kdk::WindowManager::activeWindowChanged, this, &EventWatcher::onActiveWaylandWindowChanged); connect(kdk::WindowManager::self(), &kdk::WindowManager::windowAdded, this, &EventWatcher::onWaylandWindowAdded); connect(kdk::WindowManager::self(), &kdk::WindowManager::windowRemoved, this, &EventWatcher::onWaylandWindowRemoved); } else { connect(KWindowSystem::self(), &KWindowSystem::activeWindowChanged, this, &EventWatcher::onActiveWindowChanged); connect(KWindowSystem::self(), &KWindowSystem::windowAdded, this, &EventWatcher::onWindowAdded); connect(KWindowSystem::self(), &KWindowSystem::windowRemoved, this, &EventWatcher::onWindowRemoved); } } void EventWatcher::doWindowActiveChanged(quint32 wid) { static quint32 preWid = 0; static int prePid = 0; int pid = 0; if (Policy::isWaylandPlatform()) { pid = kdk::WindowManager::getPid(wid); } else { KWindowInfo windowInfo(wid, NET::WMPid); pid = windowInfo.pid(); } if (pid <= 0) { qWarning() << __FUNCTION__ << "pid is inValid!"; return; } Q_EMIT activeWindowChanged(wid, pid, preWid, prePid); preWid = wid; prePid = pid; } void EventWatcher::onResourceThresholdWarning(const QString &resource, int level) { qDebug() << "onResourceThresholdWarning " << resource << level; QMetaEnum meta = QMetaEnum::fromType(); Policy::Feature emResource = (Policy::Feature)meta.keyToValue(resource.toLocal8Bit().data()); Q_EMIT resourceThresholdWarning(emResource, Policy::ResourceUrgency(level - 1)); } void EventWatcher::onAboutToQuitApp() { Q_EMIT aboutToQuitApp(); } void EventWatcher::onChildPidFinished(const int &pid) { Q_EMIT ChildPidFinished(pid); } kylin-process-manager-4.0.0.0/process-manager/src/core/eventwatcher.h0000644000175000017500000000532614536025312024775 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef EVENTWATCHER_H #define EVENTWATCHER_H /* * 负责外部事件信号的监听,经过处理后转发给内部模块 * wayland环境下监听kysdk的窗管信号 */ #include #include #include #include "dbusinterfaces/statusmanagerinterface.h" #include "dbusinterfaces/kwininterface.h" #include "policy.h" #include "processmanagerinterface.h" #include "dbusservices/applauncherdaemon.h" class EventWatcher : public QObject { Q_OBJECT public: explicit EventWatcher(QObject *parent = nullptr); static bool isTabletMode(); Q_SIGNALS: void tabletModeChanged(bool isTablet); void activeWindowChanged(quint32 currentWid, int currentPid, quint32 preWid, int prePid); void windowAdded(quint32 wid, int pid); void windowRemoved(quint32 wid); void mprisDbusAdded(int pid, const QString &serviceName); void resourceThresholdWarning(Policy::Feature resource, Policy::ResourceUrgency level); void aboutToQuitApp(); void ChildPidFinished(const int &pid); private Q_SLOTS: void onTabletModeChanged(bool tabletMode); void onActiveWindowChanged(quint32 wid); void onWindowAdded(quint32 wid); void onWindowRemoved(quint32 wid); void onActiveWaylandWindowChanged(kdk::WindowId wid); void onWaylandWindowAdded(kdk::WindowId wid); void onWaylandWindowRemoved(kdk::WindowId wid); void onDbusNameAcquired(QString name, QString newOwner, QString oldOwner); void onResourceThresholdWarning(const QString &resource, int level); void onAboutToQuitApp(); void onChildPidFinished(const int &pid); private: /** * @brief initConnections */ void initConnections(); void doWindowActiveChanged(quint32 wid); private: static bool m_isTablet; const QString mprisPrefix = "org.mpris.MediaPlayer2."; com::kylin::statusmanager::interface *m_statusManagerInterface; org::ukui::KWin *m_kwinInterface; com::kylin::ProcessManager *m_processManager; AppLauncherDaemon *m_appLauncherDaemon; }; #endif // EVENTWATCHER_H kylin-process-manager-4.0.0.0/process-manager/src/core/appstatusmanager.cpp0000644000175000017500000003440114536025312026204 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "appstatusmanager.h" #include #include "appinfomanager.h" #include "eventwatcher.h" #include AppStatusManager::AppStatusManager(AppInfoManager *parent) : QObject(parent) , m_timerWheel(new timerwheel::TimerWheel(this)) , m_appInfoManager(parent) , m_appCGroup(new AppCGroup(this)) , m_policy(new Policy(this)) { initConnections(); } QString AppStatusManager::newAppInstance(const QString &appId, const QList &pids) { return m_appCGroup->createProcessCGroup(appId, pids); } bool AppStatusManager::deleteAppCGroup(const QString &cgroupName) { bool ret = m_appCGroup->deleteProcessCGroup(cgroupName); if (ret) { m_cgroupInfo.remove(cgroupName); auto it = m_appInfoManager->appInfos.constBegin(); while (it != m_appInfoManager->appInfos.constEnd()) { auto instances = it.value().instances(); for (auto const &instance : qAsConst(instances)) { if (instance.cgroupName == cgroupName) { m_timerWheel->deleteTimer(instance.timerId); m_appInfoManager->appInfos[it.key()].setInstanceTimerId(cgroupName, 0); break; } } ++ it; } } return ret; } void AppStatusManager::appFinished(const QString &appId, const QString &instName) { QString cgroupName = instName; m_timerWheel->deleteTimer(m_appInfoManager->appInfos[appId].instanceTimerId(instName)); if (deleteAppCGroup(cgroupName)) { m_appInfoManager->appInfos[appId].removeAppInstance(instName); } } void AppStatusManager::activeAppChanged(const QString ¤tAppId, const QString ¤tInstName, const QString &preAppId, const QString &preInstName, bool preAppMinimized) { if (m_appInfoManager->appInfos.contains(currentAppId)) { auto ¤tAppInfo = m_appInfoManager->appInfos[currentAppId]; m_timerWheel->deleteTimer(currentAppInfo.instanceTimerId(currentInstName)); auto currentAppStatus = currentAppInfo.instanceStatus(currentInstName); if (currentAppStatus == m_policy->suspendStatus() || currentAppStatus == m_policy->frozenStatus()) { setProcessResourceValue(currentAppId, currentAppInfo.desktopFile(), currentInstName, CGROUP_FREEZE, 0); } if (m_cgroupInfo.contains(currentInstName)) { auto values = m_cgroupInfo.value(currentInstName); auto it = values.constBegin(); while (it != values.constEnd()) { int defaultValue = m_policy->defaultCGroupVale(it.key()); if (it.value() != defaultValue) { setProcessResourceValue(currentAppId, currentAppInfo.desktopFile(), currentInstName, it.key(), defaultValue); m_cgroupInfo[currentInstName][it.key()] = defaultValue; } ++ it; } } currentAppInfo.setInstanceStatus(currentInstName, m_policy->appStatus(true, false)); } if (m_appInfoManager->appInfos.contains(preAppId)) { bool isTabletMode = EventWatcher::isTabletMode(); auto &preAppInfo = m_appInfoManager->appInfos[preAppId]; auto preAppStatus = m_policy->appStatus(false, preAppMinimized); preAppInfo.setInstanceStatus(preInstName, preAppStatus); timerwheel::AppData appData { preAppId, preInstName, m_policy->nextAppStatus(preAppStatus) }; auto timerId = m_timerWheel->addTimer(m_policy->statusSwitchInterval(), appData, std::bind(&AppStatusManager::appStatusTimeOutCallback, this, std::placeholders::_1)); preAppInfo.setInstanceTimerId(preInstName, timerId); if (!isTabletMode || preAppInfo.mprisDbus(preInstName).isEmpty()) { return; } callMprisDbus(preAppInfo.mprisDbus(preInstName)); } } void AppStatusManager::appWidsIsEmpty(const QString &appId, const QString &instance) { QString cgroupName = instance; m_timerWheel->deleteTimer(m_appInfoManager->appInfos[appId].instanceTimerId(instance)); // 删除失败时说明只是窗口退出了,进程并没有退出 if (deleteAppCGroup(cgroupName)) { m_appInfoManager->appInfos[appId].removeAppInstance(instance); } } void AppStatusManager::appStatusTimeOutCallback(timerwheel::AppData appData) { qDebug() << current_func << appData.appId << appData.dstStatus << appData.instName; if (!m_appInfoManager->appInfos.contains(appData.appId)) { qWarning() << current_func << "Does not contain the app info, app id " << appData.appId; return; } auto &appInfo = m_appInfoManager->appInfos[appData.appId]; bool isTabletMode = EventWatcher::isTabletMode(); appInfo.setInstanceStatus(appData.instName, appData.dstStatus); if (isTabletMode && appData.dstStatus == m_policy->frozenStatus()) { setProcessResourceValue(appData.appId, appInfo.desktopFile(), appData.instName, CGROUP_FREEZE, 1); } if (appData.dstStatus != m_policy->lastStatus()) { timerwheel::AppData nextAppData { appData.appId, appData.instName, m_policy->nextAppStatus(appData.dstStatus) }; auto timerId = m_timerWheel->addTimer(m_policy->statusSwitchInterval(), nextAppData, std::bind(&AppStatusManager::appStatusTimeOutCallback, this, std::placeholders::_1)); appInfo.setInstanceTimerId(appData.instName, timerId); } } void AppStatusManager::tabletModeChanged(bool isTablet) { int freeze = isTablet ? 1 : 0; for (auto &appInfo : m_appInfoManager->appInfos) { auto instances = appInfo.instances(); for (auto const &instance : qAsConst(instances)) { if (instance.appStatus == m_policy->frozenStatus()) { setProcessResourceValue(appInfo.appId(), appInfo.desktopFile(), instance.cgroupName, CGROUP_FREEZE, freeze); } } } } QString AppStatusManager::cgroupWithPid(int pid) { return m_appCGroup->cgroupNameWithPid(pid); } bool AppStatusManager::thawApp(const QString &appId, const QString &cgroupName) { if (!m_appInfoManager->appInfos.contains(appId)) { qWarning() << current_func << "appInfos dose not contains " << appId << cgroupName; return false; } QString desktopFile = m_appInfoManager->appInfos.value(appId).desktopFile(); QStringList strList = desktopFile.split("/"); QString desktop = strList.isEmpty() ? desktopFile : strList.constLast(); if (m_whiteListManager.isExists(desktop, "AppManager")) { return true; } return setProcessResourceValue(appId, desktopFile, cgroupName, CGROUP_FREEZE, 0); } void AppStatusManager::aboutToQuitApp() { qWarning() << "kylin-app-manager about to quit."; bool isTabletMode = EventWatcher::isTabletMode(); auto it = m_appInfoManager->appInfos.constBegin(); while (it != m_appInfoManager->appInfos.constEnd()) { for (auto &instance : it.value().instances()) { if (instance.appStatus == Policy::CachedAppAdj && isTabletMode) { setProcessResourceValue(it->appId(), it->desktopFile(), instance.cgroupName, CGROUP_FREEZE, 0); } } ++ it; } } void AppStatusManager::initConnections() { connect(&m_whiteListManager, &WhiteListManager::addedToWhitelist, this, &AppStatusManager::updateWhitelistAppStatus); } bool AppStatusManager::ThawApps() { qWarning() << "shut down and unfreeze all apps"; auto it = m_appInfoManager->appInfos.constBegin(); while (it != m_appInfoManager->appInfos.constEnd()) { for (auto &instance : it.value().instances()) { if (instance.appStatus == Policy::CachedAppAdj || instance.appStatus == Policy::SuspendedAppAdj) { setProcessResourceValue(it->appId(), it->desktopFile(), instance.cgroupName, CGROUP_FREEZE, 0); } } ++ it; } return true; } void AppStatusManager::updateWhitelistAppStatus(QString desktopName) { if (desktopName.isEmpty()) { qWarning() << current_func << "desktopName is empty!"; return; } QString fullDesktopFile = m_appInfoManager->desktopFullFilename(desktopName); QString appId = m_appInfoManager->appIdByDesktopFile(fullDesktopFile); if (!m_appInfoManager->appInfos.contains(appId)) { qWarning() << current_func << "appInfos is not contains " << desktopName; return; } auto const &appInfo = m_appInfoManager->appInfos.value(appId); auto instances = appInfo.instances(); for (auto const &instance : qAsConst(instances)) { setProcessResourceValue(appId, appInfo.desktopFile(), instance.cgroupName, CGROUP_FREEZE, m_policy->defaultCGroupVale(CGROUP_FREEZE)); setProcessResourceValue(appId, appInfo.desktopFile(), instance.cgroupName, CPU_WEIGHT, m_policy->defaultCGroupVale(CPU_WEIGHT)); setProcessResourceValue(appId, appInfo.desktopFile(), instance.cgroupName, IO_WEIGHT, m_policy->defaultCGroupVale(IO_WEIGHT)); } } void AppStatusManager::resourceThresholdWarning(Policy::Feature resource, Policy::ResourceUrgency level) { auto actions = m_policy->actions(resource, level); qDebug() << "resourceThresholdWarning1" << resource << level << actions; if (actions.isEmpty()) { qWarning() << "Cat not find any actions when the system resource warning."; return; } for (auto &action : qAsConst(actions)) { if (!action.contains("adjs")) { qWarning() << current_func << "Get action error when resource threshold warning." << actions; continue; } Policy::AppStatus appStatus = static_cast(action.value("adjs")); for (auto &appInfo : m_appInfoManager->appInfos) { auto cgroups = appInfo.cgroupNames(appStatus); if (cgroups.isEmpty()) { continue; } auto cgroupValues = action; for (auto &cgroup : qAsConst(cgroups)) { auto it = cgroupValues.constBegin(); while (it != cgroupValues.constEnd()) { if (it.key() == "adjs") { ++ it; continue; } qDebug() << cgroup << it.key() << it.value(); bool ret = setProcessResourceValue(appInfo.appId(), appInfo.desktopFile(), cgroup, it.key(), it.value()); if (ret) { m_cgroupInfo[cgroup].insert(it.key(), it.value()); } if (resource == Policy::Memory) { appInfo.setInstanceStatus(cgroup, m_policy->suspendStatus()); m_appCGroup->reclaimProcesses(cgroup); } ++ it; } } } } } void AppStatusManager::callMprisDbus(const QString &dbusService) { qDebug() << current_func << dbusService; if (!QDBusConnection::sessionBus().isConnected()) { qWarning() << current_func << "Cannot connect to the D-Bus session bus."; return; } QDBusInterface mprisDbus(dbusService, "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player", QDBusConnection::sessionBus()); if (!mprisDbus.isValid()) { qWarning() << current_func << "mprisDbus is not valid!" << dbusService; return; } auto tryPauseReply = mprisDbus.asyncCall("Pause"); while (!tryPauseReply.isFinished()) { qApp->processEvents(); usleep(10); } if (!tryPauseReply.isError()) { return; } auto reply = mprisDbus.asyncCall("PlayState"); while (!reply.isFinished()) { qApp->processEvents(); usleep(10); } if (reply.isError()) { qWarning() << "Call mpris dbus " << dbusService << " error, " << reply.error(); return; } if (reply.reply().arguments().isEmpty()) { qWarning() << "Mpris dbus return value is empty, " << dbusService; return; } qDebug() << reply.reply().arguments().first().toInt(); if (reply.reply().arguments().first().toInt()) { mprisDbus.asyncCall("PlayPause"); } } bool AppStatusManager::setProcessResourceValue(const QString &appId, const QString &desktopFile, const QString &cgroupName, const QString &attrName, int value) { qDebug() << current_func << desktopFile << cgroupName; if (m_appInfoManager->isInhibited(appId, cgroupName) && attrName == CGROUP_FREEZE && value == 1) { return false; } QStringList strList = desktopFile.split("/"); QString desktop = strList.isEmpty() ? desktopFile : strList.constLast(); if (m_whiteListManager.isExists(desktop, "AppManager")) { return false; } m_appCGroup->setProcessCGroupResourceLimit(cgroupName, attrName, value); return true; } kylin-process-manager-4.0.0.0/process-manager/src/core/appinfomanager.h0000644000175000017500000002043314536025312025261 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef APPINFOMANAGER_H #define APPINFOMANAGER_H #include #include #include "appinfo.h" class AppStatusManager; class AppInfoManager : public QObject { friend class AppStatusManager; Q_OBJECT public: typedef QPair AppIdInstanceName; typedef QMap> DesktopFileExecInfo; explicit AppInfoManager(QObject *parent = nullptr); /** * @brief isKmreApp 根据desktop文件判断当前应用是不是kmre的应用 * @param fullDesktopName * @return */ static bool isKmreApp(const QString &fullDesktopName); /** * @brief newAppInstance 创建应用实例的cgroup分组 * @param desktopFile 全路径下的desktop文件 * @param forkPid launcher中进行打开应用的进程pid * @return cgroupName */ QString newAppInstance(const QString &desktopFile, const QStringList &args, const QList &pids, appinfo::AppType type); QString desktopFile(int pid); QString desktopFile(quint32 wid); bool thawApp(int pid); bool thawApp(quint32 wid); /** * @brief wids 获取desktopFile应用的wids列表 * @param desktopFile 全路径下的desktop文件 * @return 所有该应用对应的wid列表 */ QList wids(const QString &desktopFile); QList wids(const QString &desktopFile, const QString &instName); QList pids(const QString &desktopFile, const QString &instName); QString cmdline(int pid); DesktopFileExecInfo desktopFilesExec() const; /** * @brief runningStatus * @param desktopFile * @param args * @return */ std::tuple runningStatus(const QString &desktopFile, const QStringList &args) const; QString inhibit(const QString &desktopFile, uint pid, const QString &reason, uint flags); void unInhibit(const QString &cookie); bool isInhibited(const QString &appId, const QString &instName); /** * @brief ThawApps session关机时调用,解冻所有应用 * @return 解冻成功返回true */ bool ThawApps(); public Q_SLOTS: void onActiveWindowChanged(quint32 currentWid, int currentPid, quint32 preWid, int prePid); void onWindowAdded(quint32 wid, int pid); void onWindowRemoved(quint32 wid); void onMprisDbusAdded(int pid, const QString &dbusService); void updateDesktopExecsInfo(const QString &path); void onResourceThresholdWarning(Policy::Feature resource, Policy::ResourceUrgency level); void onTabletModeChanged(bool tabletMode); void onChildPidFinished(const int &pid); void onAboutToQuitApp(); Q_SIGNALS: void appFinished(const QString &desktopNmae, qlonglong pid, uint wid); void appLaunched(const QString &desktopNmae, qlonglong pid, uint wid); private: /** * @brief initDesktopExecsInfoWatcher 初始化应用desktopFileExec字段信息 */ void initDesktopExecsInfoWatcher(); /** * @brief initConnections */ void initConnections(); /** * @brief appIdByDesktopFile 通过全路径下的desktop文件获得应用MD5值 * @param desktopFile 全路径下的desktop文件 * @return appId(MD5值) */ QString appIdByDesktopFile(const QString &desktopFile) const; QString desktopFileName(const QString &desktopFile); /** * @brief desktopFileExec 获取desktop文件中Exec字段 * @param desktopFileName desktop名称 * @return Exec字段 */ QPair desktopFileExec(const QString &desktopFileName); /** * @brief desktopFullFilename 获得带全路径的desktop名称 * @param desktopFileName 应用的desktop名称 * @return 带全路径的desktop名称 */ QString desktopFullFilename(const QString &desktopFileName) const; /** * @brief appInstanceInfoWithWindowId 根据指定信息查找appId和instName * @param wid、pid、instName * @return QPair */ AppIdInstanceName appInstanceInfoWithWindowId(quint32 wid); AppIdInstanceName appInstanceInfoWithPid(int pid); AppIdInstanceName appInstanceInfoWithInstName(const QString &instName); appinfo::AppInstance appInstanceWithPid(int pid); /** * @brief findAndroidNameByWid 调用kmre接口通过wid找到对应移动android包名 * @param wid 窗口wid * @return wid对应的包名则返回 */ QString findAndroidNameByWid(const quint32 &wid); /** * @brief findDesktopCmdline 根据应用pid找到对应的应用id和desktop文件 // 根据cmdline进行匹配 * @param pid 应用pid * @return 返回存储应用的desktop和id */ QPair findDesktopCmdline(int const &pid); /** * @brief findGlobalDesktopFile 非应用管理拉起的应用,根据应用的cmdline值匹配到标准路径下对应的desktop文件和id * @param cmdline 应用的cmdline * @return 应用对应的desktop和id */ QString findGlobalDesktopFile(QString const &cmdline); /** * @brief desktopFileFromPackage 通过cmdline获得package里面的desktopFile信息 * @param cmdline 进程启动信息 * @return desktopFile */ QString desktopFileFromCmdline(QString cmdline); /** * @brief pkgNameFromCmdline 通过cmdline获取包名 * @param cmdline 应用启动进程信息 * @return 所查询的包信息列表 */ QStringList pkgNameFromCmdline(const QString &cmdline); /** * @brief isDesktopFileNoDisplay 判断desktop文件中的NoDisplay字段 * @param desktopFileName desktop名称 * @return 字段为true返回true */ bool isDesktopFileNoDisplay(const QString &desktopFileName); /** * @brief findChildrenPids 获取pid进程下所有的子进程 * @return 所有的子进程 */ QList findChildrenPids(const int &pid); /** * @brief updateAppInfomations 更新appinfos中已保存的appId的信息 * @param fullDesktopName * @param wid * @param pid */ void updateAppInfomations(const QString &fullDesktopName, const quint32 &wid, const int &pid); /** * @brief addAppInfomations 非应用管理打开的应用添加一下信息 * @param fullDesktopName * @param wid * @param pid */ void addAppInfomations(const QString &fullDesktopName, const QString &cmdline, const quint32 &wid, const int &pid); /** * @brief setInstanceInfos 设置实例信息 * @param appId * @param instName */ void updateInstanceInfos(const AppIdInstanceName &appIdInstance, const quint32 &wid, const int &pid); QString findAndroidNameByPid(const quint32 &pid); // QPair iterateThroughAppInstances( const std::function &callback); private: typedef QMap AppInformation; struct Inhibitor { QString appId; QString cookie; int pid; inhibitor::Inhibitor type; }; AppInformation appInfos; QMap m_tmpMprisDbus; AppStatusManager *m_appStatusManager; QMap> m_desktopsExec; QMutex m_mutex; QFileSystemWatcher *m_desktopFileWatcher; QList m_inhibitors; const QString kKmreAppMainWindow = "/usr/bin/kylin-kmre-window"; const QStringList kNoCgroupApps = { }; }; #endif // APPINFOMANAGER_H kylin-process-manager-4.0.0.0/process-manager/src/core/appcgroup.h0000644000175000017500000000305214536025312024270 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef APPCGROUP_H #define APPCGROUP_H #include #include "processmanagerinterface.h" class AppCGroup : public QObject { Q_OBJECT public: explicit AppCGroup(QObject *parent = nullptr); QString createProcessCGroup(const QString &appId, const QList &pids); bool deleteProcessCGroup(const QString &cgroupName); bool setProcessCGroupResourceLimit(const QString &cgroupName, const QString &attrName, int value); bool reclaimProcesses(const QList &pids); bool reclaimProcesses(const QString &cgroupName); QString cgroupNameWithPid(int pid); private: void initCGroup(); private: com::kylin::ProcessManager *m_processManagerInterface; const char *kDbusResult = "result"; const char *kDbusErrMsg = "errorMessage"; }; #endif // APPCGROUP_H kylin-process-manager-4.0.0.0/process-manager/src/core/policy.h0000644000175000017500000000722014536025312023570 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ /* * 读取/监听配置文件,管理应用管理的管控策略,单例类 */ #ifndef POLICY_H #define POLICY_H #include #include #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) #include #endif #define CGROUP_FREEZE "cgroup.freeze" #define CPU_WEIGHT "cpu.weight" #define CPU_MAX "cpu.max" #define IO_WEIGHT "io.weight" namespace inhibitor { enum Inhibitor { FrozenInhibitor = 1, }; } class QFileSystemWatcher; class Policy : public QObject { Q_OBJECT public: explicit Policy(QObject *parent = nullptr); enum Feature { CPU, Memory, IO, Frozen, MultimediaControl, }; Q_ENUM(Feature) enum AppStatus { ActiveAppAdj = 0, ForegroundAppAdj = 100, BackendAppAdj = 200, CachedAppAdj = 300, SuspendedAppAdj = 400, }; Q_ENUM(AppStatus) enum ResourceUrgency { Low, Medium, High, }; Q_ENUM(ResourceUrgency) static bool isWaylandPlatform(); /** * @brief features 获取当前支持的特性 * @return 返回做支持的Feature(特性) */ QList features(); /** * @brief featureEnabled 判断是否支持Feature类型 * @param tablet 是否为平板模式 * @param feat 判断是否支持指定的Feature类型 * @return 支持该controller返回true,否则返回false */ bool featureEnabled(bool tablet, Feature feat); /** * @brief statusSwitchInterval 获取进程状态切换时间间隔 * @return 时间间隔 */ int statusSwitchInterval() const; AppStatus appStatus(bool appAvtived, bool appMinimized = false); /** * @brief nextAppStatus 进程切换至下一次的状态 * @param tablet 是否为平板模式 * @param currentStatus 当前的进程状态 * @return 下次切换的进程状态 */ AppStatus nextAppStatus(AppStatus currentStatus); /** * @brief frozenStatus 获取需要对进程进行冻结的状态 * @return */ AppStatus frozenStatus() const; /** * @brief lastStatus 返回非睡眠状态的最后一个状态 * @return */ AppStatus lastStatus() const; /** * @brief suspendStatus 返回休眠状态 * @return */ AppStatus suspendStatus() const; // QList> actions(Feature feat, ResourceUrgency urgency); int defaultCGroupVale(const QString &name); private Q_SLOTS: /** * @brief readConf 读取配置文件 */ void readConf(); private: typedef QMap StatNameValue; typedef QMap CGroupUrgencyStatValue; // >> QMap m_actions; int m_statusSwitchInterval; // s QFileSystemWatcher *m_confFileWatcher; const char *kConfFilename = "/etc/kylin-app-manager/kylin-app-manager.json"; }; #endif // POLICY_H kylin-process-manager-4.0.0.0/process-manager/src/core/basecontroller.h0000644000175000017500000000207214536025312025307 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef BASECONTROLLER_H #define BASECONTROLLER_H class BaseController { public: explicit BaseController(); int stat(); int max(); int pressure(); struct CpuController { int cpuWeight(); void weight_nice(); }; struct IoController { int IoWeight(); }; struct MemoryController { }; }; #endif // BASECONTROLLER_H kylin-process-manager-4.0.0.0/process-manager/src/core/timerwheel.cpp0000644000175000017500000000520014536025312024765 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "timerwheel.h" #include namespace timerwheel { TimerWheel::TimerWheel(QObject *parent) : QObject(parent) , m_timer(new QTimer(this)) , m_currentSlot(0) , m_currentTimerCount(0) { connect(m_timer, &QTimer::timeout, this, &TimerWheel::tick); } quint64 TimerWheel::addTimer(int timeout, AppData appData, const std::function &callback) { int ticks = 0; if (timeout <= kSi) { ticks = 1; } else { ticks = timeout / kSi; } int rotation = timeout / kSlotNumber; int tickSlot = (m_currentSlot + (ticks % kSlotNumber)) % kSlotNumber; Timer *timer = new Timer; timer->appData = appData; timer->callback = callback; timer->rotation = rotation; timer->tickSlot = tickSlot; m_slotTimers[tickSlot].push_back(timer); if (!m_timer->isActive()) { m_timer->start(kSi * 1000); } if (m_currentTimerCount == UINT64_MAX) { m_currentTimerCount = 1; } else { ++m_currentTimerCount; } timer->timerId = m_currentTimerCount; return m_currentTimerCount; } void TimerWheel::tick() { if (!m_slotTimers.contains(m_currentSlot)) { m_currentSlot = ++m_currentSlot % kSlotNumber; return; } auto &timers = m_slotTimers[m_currentSlot]; for (auto &timer : timers) { if (timer->rotation > 0) { -- timer->rotation; } else { timer->callback(timer->appData); timers.removeOne(timer); } } m_currentSlot = ++m_currentSlot % kSlotNumber; } bool TimerWheel::deleteTimer(quint64 timerId) { auto it = m_slotTimers.begin(); while (it != m_slotTimers.end()) { for (auto &timer : *it) { if (timer->timerId == timerId) { it->removeOne(timer); return true; } } ++ it; } return false; } } // namspace timerwheel kylin-process-manager-4.0.0.0/process-manager/src/core/appinfo.h0000644000175000017500000001011314536025312023720 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef APPINFO_H #define APPINFO_H #include #include "policy.h" #include #include #include namespace appinfo { enum AppType { Normal, Kmre, }; enum RunningStatus { None, Launching, Running, Finished, Error, }; struct AppInstance { QString instName; QString cgroupName; QStringList args; QList pids; quint64 timerId = 0; Policy::AppStatus appStatus; RunningStatus appRunningStaus; AppType appType; int64_t launchTimestamp; QList wids; QString mprisDbus; bool operator==(const AppInstance &other) { return instName == other.instName && cgroupName == other.cgroupName && args == other.args && pids == other.pids && timerId == other.timerId && appStatus == other.appStatus && appRunningStaus == other.appRunningStaus && appType == other.appType && launchTimestamp == other.launchTimestamp && wids == other.wids && mprisDbus == other.mprisDbus; } }; class AppInfo : public QObject { Q_OBJECT public: explicit AppInfo(QObject *parent = nullptr); explicit AppInfo(const QString &id, const QString &desktopFile, AppType appType, QObject *parent = nullptr); AppInfo(const appinfo::AppInfo &other); bool operator==(const appinfo::AppInfo &other); AppInfo operator= (const appinfo::AppInfo &other); /** * @brief id * @return */ QString appId() const; /** * @brief desktopFile * @return */ QString desktopFile() const; AppType appType() const; /** * @brief instances * @return */ QList instances() const; void appendAppInstance(const AppInstance &inst); QString cgroupName(const QString &instName) const; QStringList cgroupNames(Policy::AppStatus status) const; QString mprisDbus(const QString &instName) const; void setMprisDbus(const QString &instName, const QString &mprisDBusName); quint64 instanceTimerId(const QString &instName) const; void setInstanceTimerId(const QString &instName, quint64 timerId); bool appInstanceIsHidden(const QString &instName) const; void setInstanceStatus(const QString &name, Policy::AppStatus status); int instanceStatus(const QString &name); void setInstanceRunningStatus(const QString &name, RunningStatus status); void setInstanceAppType(const QString &name, AppType type); /** * @brief removeAppInstance * @param id * @param inst */ void removeAppInstance(const QString &inst); /** * @brief appendAppWid * @param id * @param inst * @param wid */ void appendAppWid(const QString &inst, const quint32 &wid); QList appInstanceWids(const QString &inst) const; /** * @brief removeAppWid * @param id * @param inst */ void removeAppWid(const QString &inst, const quint32 &wid); void appendPid(const QString &inst, const int &pid); void removePid(const QString &inst, const int &pid); QList pids(const QString &inst) const; private: QList m_instances; QString m_appId; QString m_desktopFile; AppType m_appType; }; } // namespace appinfo #endif // APPINFO_H kylin-process-manager-4.0.0.0/process-manager/src/core/appstatusmanager.h0000644000175000017500000000574514536025312025662 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef APPSTATUSMANAGER_H #define APPSTATUSMANAGER_H #include #include #include "timerwheel.h" #include "appinfomanager.h" #include "appcgroup.h" #include "dbusservices/whitelistmanager.h" #include "common.h" class Policy; class AppInfoManager; class AppStatusManager : public QObject { Q_OBJECT public: explicit AppStatusManager(AppInfoManager *parent); /** * @brief newAppInstance * @param appId * @param forkPid * @param instlndex * @return */ QString newAppInstance(const QString &appId, const QList &pids); /** * @brief deleteAppCGroup * @param cgroupName * @return */ bool deleteAppCGroup(const QString &cgroupName); void appFinished(const QString &appId, const QString &instName); /** * @brief activeAppChanged * @param currentAppId * @param preAppId */ void activeAppChanged(const QString ¤tAppId, const QString ¤tInstName, const QString &preAppId, const QString &preInstName, bool preAppMinimized); void appWidsIsEmpty(const QString &appId, const QString &instance); void appStatusTimeOutCallback(timerwheel::AppData appData); void resourceThresholdWarning(Policy::Feature resource, Policy::ResourceUrgency level); void tabletModeChanged(bool isTablet); QString cgroupWithPid(int pid); bool thawApp(const QString &appId, const QString &cgroupName); void aboutToQuitApp(); void initConnections(); bool ThawApps(); private Q_SLOTS: void updateWhitelistAppStatus(QString desktopName); private: void callMprisDbus(const QString &dbusService); bool setProcessResourceValue(const QString &appId, const QString &desktopFile, const QString &cgroupName, const QString &attrName, int value); private: timerwheel::TimerWheel *m_timerWheel; QMap> m_cgroupInfo; AppInfoManager *m_appInfoManager; AppCGroup *m_appCGroup; Policy *m_policy; WhiteListManager &m_whiteListManager = common::Singleton::GetInstance(); }; #endif // APPSTATUSMANAGER_H kylin-process-manager-4.0.0.0/process-manager/src/main.cpp0000644000175000017500000000211414536025312022615 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include #include #include #include #include "src/appmanager.h" #ifndef QT_DEBUG #include "ukui-log4qt.h" #endif int main(int argc, char *argv[]) { QApplication a(argc, argv); qDBusRegisterMetaType>(); #ifndef QT_DEBUG initUkuiLog4qt("kylin-app-manager"); #endif AppManager appManager; return a.exec(); } kylin-process-manager-4.0.0.0/process-manager/src/appmanager.h0000644000175000017500000000526614536025312023464 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef APPMANAGER_H #define APPMANAGER_H #include #include #include "common.h" #include "core/appinfomanager.h" #include "dbusservices/whitelistmanager.h" class AppManagerService; class AppLauncher; class WhiteListManager; class MemoryMonitor; class EventWatcher; class AppStatusManager; class AppManager : public QObject { Q_OBJECT public: explicit AppManager(QObject *parent = nullptr); Q_INVOKABLE bool LaunchApp(const QString &desktopFile); Q_INVOKABLE bool LaunchAppWithArguments(const QString &desktopFile,const QStringList &args); Q_INVOKABLE bool Open(const QString &fileName); Q_INVOKABLE bool LaunchDefaultAppWithUrl(const QString &url); Q_INVOKABLE void SetPowerSavingModeEnable(bool isPowerSaving); Q_INVOKABLE bool AddToWhiteList(const QString &desktopFile, const QString &option); Q_INVOKABLE bool RemoveFromWhiteList(const QString &desktopFile, const QString &option); Q_INVOKABLE bool ActiveProcessByWid(const uint &wid); Q_INVOKABLE bool ActiveProcessByPid(const int &pid); Q_INVOKABLE QStringList WhiteListsOfApp(const QString &desktopFile); Q_INVOKABLE QStringList AppWhiteList(const QString &option); Q_INVOKABLE QVector RecommendAppLists(const QString &fileName); Q_INVOKABLE QString AppDesktopFileNameByPid(qint64 pid); Q_INVOKABLE QString AppDesktopFileNameByWid(qint64 wid); Q_INVOKABLE QString Inhibit(const QString &desktopFile, uint pid, const QString &reason, uint flags); Q_INVOKABLE void UnInhibit(const QString &cookie); Q_INVOKABLE bool ThawApps(); private: void initConnections(); private: AppManagerService *m_service; AppLauncher *m_appLauncher; EventWatcher *m_eventWatcher; WhiteListManager &m_whiteListManager = common::Singleton::GetInstance(); AppInfoManager &m_appInfoManager = common::Singleton::GetInstance(); }; #endif // APPMANAGER_H kylin-process-manager-4.0.0.0/process-manager/src/dbusinterfaces/0000755000175000017500000000000014536025312024170 5ustar debiandebiankylin-process-manager-4.0.0.0/process-manager/src/dbusinterfaces/com.kylin.CGroup.xml0000644000175000017500000000120214536025312030006 0ustar debiandebian kylin-process-manager-4.0.0.0/process-manager/src/dbusinterfaces/statusmanagerinterface.cpp0000644000175000017500000000253014536025312031433 0ustar debiandebian/* * Copyright (C) 2020 The Qt Company Ltd. * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "statusmanagerinterface.h" /* * Implementation of interface class StatusManagerInterface */ StatusManagerInterface::StatusManagerInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent) : QDBusAbstractInterface(service, path, staticInterfaceName(), connection, parent) { QDBusConnection::sessionBus().connect(this->service(), this->path(), staticInterfaceName(), "mode_change_signal", this, SLOT(__modeChanged__(bool))); } StatusManagerInterface::~StatusManagerInterface() { } kylin-process-manager-4.0.0.0/process-manager/src/dbusinterfaces/cgroupinterface.h0000644000175000017500000000426014536025312027523 0ustar debiandebian/* * Copyright (C) 2020 The Qt Company Ltd. * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef CGROUPINTERFACE_H #define CGROUPINTERFACE_H #include #include #include #include #include #include #include #include /* * Proxy class for interface com.kylin.CGroup.AppManager */ class CGroupInterface: public QDBusAbstractInterface { Q_OBJECT public: static inline const char *staticInterfaceName() { return "com.kylin.CGroup.AppManager"; } public: CGroupInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent = nullptr); ~CGroupInterface(); public Q_SLOTS: // METHODS inline QDBusPendingReply<> CreateCGroup() { QList argumentList; return asyncCallWithArgumentList(QStringLiteral("CreateCGroup"), argumentList); } inline QDBusPendingReply<> DeleteCGroup() { QList argumentList; return asyncCallWithArgumentList(QStringLiteral("DeleteCGroup"), argumentList); } inline QDBusPendingReply<> MoveProcess(int pid, int group) { QList argumentList; argumentList << QVariant::fromValue(pid) << QVariant::fromValue(group); return asyncCallWithArgumentList(QStringLiteral("MoveProcess"), argumentList); } Q_SIGNALS: // SIGNALS }; namespace com { namespace kylin { namespace CGroup { typedef ::CGroupInterface AppManager; } } } #endif kylin-process-manager-4.0.0.0/process-manager/src/dbusinterfaces/statusmanagerinterface.h0000644000175000017500000000370514536025312031105 0ustar debiandebian/* * Copyright (C) 2020 The Qt Company Ltd. * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef STATUSMANAGERINTERFACE_H #define STATUSMANAGERINTERFACE_H #include #include #include #include #include #include #include #include /* * Proxy class for interface com.kylin.statusmanager.interface */ class StatusManagerInterface: public QDBusAbstractInterface { Q_OBJECT Q_SLOT void __modeChanged__(bool tablet_mode) { Q_EMIT mode_change_signal(tablet_mode); } public: static inline const char *staticInterfaceName() { return "com.kylin.statusmanager.interface"; } public: StatusManagerInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent = nullptr); ~StatusManagerInterface(); public Q_SLOTS: // METHODS inline QDBusPendingReply get_current_tabletmode() { QList argumentList; return asyncCallWithArgumentList(QStringLiteral("get_current_tabletmode"), argumentList); } Q_SIGNALS: // SIGNALS void mode_change_signal(bool tablet_mode); }; namespace com { namespace kylin { namespace statusmanager { typedef ::StatusManagerInterface interface; } } } #endif kylin-process-manager-4.0.0.0/process-manager/src/dbusinterfaces/cgroupinterface.cpp0000644000175000017500000000207714536025312030062 0ustar debiandebian/* * Copyright (C) 2020 The Qt Company Ltd. * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "cgroupinterface.h" /* * Implementation of interface class CGroupInterface */ CGroupInterface::CGroupInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent) : QDBusAbstractInterface(service, path, staticInterfaceName(), connection, parent) { } CGroupInterface::~CGroupInterface() { } kylin-process-manager-4.0.0.0/process-manager/src/dbusinterfaces/kwininterface.h0000644000175000017500000000514114536025312027173 0ustar debiandebian/* * Copyright (C) 2020 The Qt Company Ltd. * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef KWININTERFACE_H #define KWININTERFACE_H #include #include #include #include #include #include #include #include /* * Proxy class for interface org.ukui.KWin */ class KWinInterface: public QDBusAbstractInterface { Q_OBJECT Q_SLOT void __activeWindowChanged__() { Q_EMIT activeWindowChanged(); } public: static inline const char *staticInterfaceName() { #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) return "org.ukui.KWin"; #else return "org.kde.KWin"; #endif } public: KWinInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent = nullptr); ~KWinInterface(); public Q_SLOTS: // METHODS inline QDBusPendingReply getActiveWindowId() { QList argumentList; return asyncCallWithArgumentList(QStringLiteral("getActiveWindowId"), argumentList); } inline QDBusPendingReply getWindowPid(uint wid) { QList argumentList; argumentList << QVariant::fromValue(wid); return asyncCallWithArgumentList(QStringLiteral("getWindowPid"), argumentList); } inline QDBusPendingReply getWidFromPid(uint pid) { QList argumentList; argumentList << QVariant::fromValue(pid); return asyncCallWithArgumentList(QStringLiteral("getWidFromPid"), argumentList); } inline QDBusPendingReply activeWindowByPid(uint pid) { QList argumentList; argumentList << QVariant::fromValue(pid); return asyncCallWithArgumentList(QStringLiteral("activeWindowByPid"), argumentList); } Q_SIGNALS: // SIGNALS void activeWindowChanged(); }; namespace org { namespace ukui { typedef ::KWinInterface KWin; } } #endif kylin-process-manager-4.0.0.0/process-manager/src/dbusinterfaces/kwininterface.cpp0000644000175000017500000000244714536025312027534 0ustar debiandebian/* * Copyright (C) 2020 The Qt Company Ltd. * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "kwininterface.h" /* * Implementation of interface class KWinInterface */ KWinInterface::KWinInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent) : QDBusAbstractInterface(service, path, staticInterfaceName(), connection, parent) { QDBusConnection::sessionBus().connect(this->service(), this->path(), staticInterfaceName(), "activeWindowChanged", this, SLOT(__activeWindowChanged__())); } KWinInterface::~KWinInterface() { } kylin-process-manager-4.0.0.0/process-manager/src/dbusinterfaces/org.ukui.KWin.xml0000644000175000017500000000066014536025312027326 0ustar debiandebian ././@LongLink0000644000000000000000000000014700000000000011605 Lustar rootrootkylin-process-manager-4.0.0.0/process-manager/src/dbusinterfaces/com.kylin.statusmanager.interface.xmlkylin-process-manager-4.0.0.0/process-manager/src/dbusinterfaces/com.kylin.statusmanager.interface.x0000644000175000017500000000100714536025312033076 0ustar debiandebian kylin-process-manager-4.0.0.0/process-manager/src/childrenprocesswatcher.cpp0000644000175000017500000000773014536025312026447 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "childrenprocesswatcher.h" #include extern "C" { #include #include #include #include #include #include #include } ChildrenProcessWatcher::ChildrenProcessWatcher(QObject *parent) : QObject(parent) { } void ChildrenProcessWatcher::start() { int fd = inotify_init1(IN_NONBLOCK); if (fd == -1) { qWarning() << "inotify init failed"; return; } // Mark directories for events // - file was opened // - file was closed int pid = getpid(); QString childrenProcessPath = QString("/proc/%1/task/%2/children").arg(pid).arg(pid); int wd = inotify_add_watch(fd, childrenProcessPath.toStdString().c_str(), IN_OPEN | IN_CLOSE); if (wd == -1) { qWarning() << "inotify add watch failed"; return; } nfds_t nfds; struct pollfd fds[2]; // Prepare for polling. nfds = 2; // Console input fds[0].fd = STDIN_FILENO; fds[0].events = POLLIN; // Inotify input fds[1].fd = fd; fds[1].events = POLLIN; int pollNum; char buf; for (;;) { pollNum = poll(fds, nfds, -1); if (pollNum == -1) { if (errno == EINTR) { continue; } qWarning() << "poll errno: " << errno; break; } if (pollNum > 0) { if (fds[0].revents & POLLIN) { // Console input is available. Empty stdin and quit. while (read(STDIN_FILENO, &buf, 1) > 0 && buf != '\n') { continue; } break; } if (fds[1].revents & POLLIN) { // Inotify events are available. handleEvents(fd); } } } } void ChildrenProcessWatcher::handleEvents(int fd) { // Some systems cannot read integer variables if they are not // properly aligned. On other systems, incorrect alignment may // decrease performance. Hence, the buffer used for reading from // the inotify file descriptor should have the same alignment as // struct inotify_event. char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event)))); const struct inotify_event *event; ssize_t len; // Loop while events can be read from inotify file descriptor. for (;;) { // Read some events. len = read(fd, buf, sizeof(buf)); if (len == -1 && errno != EAGAIN) { qWarning() << "read buffer error: " << errno; return; } // If the nonblocking read() found no events to read, then // it returns -1 with errno set to EAGAIN. In that case, // we exit the loop. if (len <= 0) { break; } // Loop over all events in the buffer. for (char *ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) { event = (const struct inotify_event *) ptr; // Print event type. if (event->mask & IN_CLOSE_NOWRITE) { close(fd); qDebug() << "IN_CLOSE_NOWRITE: "; } if (event->mask & IN_OPEN) qDebug("IN_OPEN: "); qDebug() << event->mask; } } } kylin-process-manager-4.0.0.0/process-manager/src/appmanager.cpp0000644000175000017500000001156414536025312024015 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "appmanager.h" #include #include #include "dbusservices/appmanagerservice.h" #include "dbusservices/applauncher.h" #include "dbusservices/whitelistmanager.h" #include "dbusservices/applauncher.h" #include "core/eventwatcher.h" AppManager::AppManager(QObject *parent) : QObject(parent) , m_service(new AppManagerService(this)) , m_appLauncher(new AppLauncher(this)) , m_eventWatcher(new EventWatcher(this)) { QDBusConnection connection = QDBusConnection::sessionBus(); if (!connection.registerService("com.kylin.AppManager") || !connection.registerObject("/com/kylin/AppManager", this)) { qWarning() << "register dbus service failed" << connection.lastError(); } initConnections(); } bool AppManager::LaunchApp(const QString &desktopFile) { return m_appLauncher->LaunchApp(desktopFile); } bool AppManager::LaunchAppWithArguments(const QString &desktopFile,const QStringList &args) { return m_appLauncher->LaunchAppWithArguments(desktopFile, args); } bool AppManager::Open(const QString &fileName) { return m_appLauncher->Open(fileName); } bool AppManager::LaunchDefaultAppWithUrl(const QString &url) { return m_appLauncher->LaunchDefaultAppWithUrl(url); } void AppManager::SetPowerSavingModeEnable(bool enable) { // m_appLauncher->setPowerSavingModeEnable(enable); } bool AppManager::AddToWhiteList(const QString &desktopFile, const QString &option) { return m_whiteListManager.addToWhiteList(desktopFile, option); } bool AppManager::RemoveFromWhiteList(const QString &desktopFile, const QString &option) { return m_whiteListManager.removeFromWhiteList(desktopFile, option); } bool AppManager::ActiveProcessByWid(const uint &wid) { return m_appLauncher->ActiveProcessByWid(wid); } bool AppManager::ActiveProcessByPid(const int &pid) { return m_appLauncher->ActiveProcessByPid(pid); } QStringList AppManager::WhiteListsOfApp(const QString &desktopFile) { return m_whiteListManager.whiteListsOfApp(desktopFile); } QStringList AppManager::AppWhiteList(const QString &option) { return m_whiteListManager.appWhiteList(option); } QVector AppManager::RecommendAppLists(const QString &fileName) { return m_appLauncher->RecommendAppLists(fileName); } QString AppManager::AppDesktopFileNameByPid(qint64 pid) { return m_appLauncher->AppDesktopFileNameByPid(pid); } QString AppManager::AppDesktopFileNameByWid(qint64 wid) { return m_appLauncher->AppDesktopFileNameByWid(wid); } QString AppManager::Inhibit(const QString &desktopFile, uint pid, const QString &reason, uint flags) { return m_appInfoManager.inhibit(desktopFile, pid, reason, flags); } void AppManager::UnInhibit(const QString &cookie) { m_appInfoManager.unInhibit(cookie); } bool AppManager::ThawApps() { return m_appInfoManager.ThawApps(); } void AppManager::initConnections() { connect(&m_appInfoManager, &AppInfoManager::appLaunched, m_service, &AppManagerService::appLaunched); connect(&m_appInfoManager, &AppInfoManager::appFinished, m_service, &AppManagerService::appFinished); connect(m_eventWatcher, &EventWatcher::activeWindowChanged, &m_appInfoManager, &AppInfoManager::onActiveWindowChanged); connect(m_eventWatcher, &EventWatcher::windowAdded, &m_appInfoManager, &AppInfoManager::onWindowAdded); connect(m_eventWatcher, &EventWatcher::windowRemoved, &m_appInfoManager, &AppInfoManager::onWindowRemoved); connect(m_eventWatcher, &EventWatcher::mprisDbusAdded, &m_appInfoManager, &AppInfoManager::onMprisDbusAdded); connect(m_eventWatcher, &EventWatcher::tabletModeChanged, &m_appInfoManager, &AppInfoManager::onTabletModeChanged); connect(m_eventWatcher, &EventWatcher::resourceThresholdWarning, &m_appInfoManager, &AppInfoManager::onResourceThresholdWarning); connect(m_eventWatcher, &EventWatcher::aboutToQuitApp, &m_appInfoManager, &AppInfoManager::onAboutToQuitApp); connect(m_eventWatcher, &EventWatcher::ChildPidFinished, &m_appInfoManager, &AppInfoManager::onChildPidFinished); } kylin-process-manager-4.0.0.0/process-manager/src/utils/0000755000175000017500000000000014536025312022327 5ustar debiandebiankylin-process-manager-4.0.0.0/process-manager/src/utils/processinfo.h0000644000175000017500000000715314536025312025040 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef PROCESSINFO_H #define PROCESSINFO_H #include #include /** * @brief The ProcessInfo class 保存应用进程信息的类 * @Info 保存的信息包含(pid childrenPid status wids args tasks exe cmdline) * @author duchangfeng */ class ProcessInfo : public QObject { Q_OBJECT public: enum AppCGroup { ForegroundProcess, InvisibleProcess, CachedProcess, }; enum AppStatus { None, Launching, Running, Finished, Error, }; enum AppType { Normal, Kmre, }; explicit ProcessInfo(const ProcessInfo& proc); explicit ProcessInfo(); bool operator==(const ProcessInfo &other); ProcessInfo& operator=(const ProcessInfo &other); qint64 pid() const; QList childrenPids(); AppCGroup cgroup() const; QList wids() const; QStringList getArgs() const; QList getTasks(); void setCGroup(const AppCGroup &appCGroup); void setArgs(const QStringList &appArgs); void setPid(const qint64 &appPid); void pushWid(const uint &wid); void removeWid(const uint &wid); void setWids(const QList &appWids); void setAppStatus(AppStatus status); void setLaunchPid(int pid); void setAppType(AppType type) { m_appType = type; } AppType appType() const { return m_appType; } void setDesktopFileName(const QString &desktopFileName) { m_desktopFileName = desktopFileName; } QString desktopFileName() const { return m_desktopFileName; } int launchPid() const; bool needingPolkitWindow() const { return m_needingPolkitWindow; } void setNeedingPolkitWindow(bool needing) { m_needingPolkitWindow = needing; } void setLockCgroup(bool locking); bool lockingCgroup() const; /** * @brief getCmdlineFromPid * @param pid 根据当前应用的pid获取Cmdline * @return */ static QString getCmdlineFromPid(int pid); AppStatus appStatus() const; qint64 launchTimestamp() const { return m_launchTimestamp; } static QString cmdline(int pid); static qint64 parentId(int pid); private: void setChildrenPid(const qint64 &appPid); /** * @brief findChildrenPids 找到所有的子pid,包括孙pid * @param pid * @return */ void findChildrenPids(qint64 pid); void setTasks(const qint64 &appPid); qint64 m_pid; QString m_procPath; QList m_childrenPid; AppCGroup m_cgroup; QList m_wids; QStringList m_args; QList m_tasks; QString m_exe; QString m_cmdline; QString m_directoryPath; QString m_filePath; AppStatus m_appStatus; AppType m_appType; QString m_desktopFileName; bool m_needingPolkitWindow; bool m_lockingCgroup; int m_launchPid; qint64 m_launchTimestamp; }; QDebug &operator<<(QDebug &, const ProcessInfo &); #endif // PROCESSINFO_H kylin-process-manager-4.0.0.0/process-manager/src/utils/processinfo.cpp0000644000175000017500000002102114536025312025361 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "processinfo.h" #include #include #include #include #include #include #include #include #include bool ProcessInfo::operator==(const ProcessInfo &other) { return other.m_pid == this->m_pid && other.m_childrenPid == this->m_childrenPid && other.m_cgroup == this->m_cgroup && other.m_wids == this->m_wids && other.m_args == this->m_args && other.m_tasks == this->m_tasks && other.m_exe == this->m_exe && other.m_cmdline == this->m_cmdline && other.m_appType == this->m_appType && other.m_desktopFileName == this->m_desktopFileName && this->m_lockingCgroup == other.m_lockingCgroup && this->m_launchPid == other.m_launchPid && this->m_launchTimestamp == other.m_launchTimestamp; } ProcessInfo &ProcessInfo::operator=(const ProcessInfo &other) { if (this != &other) { this->m_pid = other.m_pid; this->m_childrenPid = other.m_childrenPid; this->m_cgroup = other.m_cgroup; this->m_wids = other.m_wids; this->m_args = other.m_args; this->m_tasks = other.m_tasks; this->m_exe = other.m_exe; this->m_cmdline = other.m_cmdline; this->m_appType = other.m_appType; this->m_desktopFileName = other.m_desktopFileName; this->m_lockingCgroup = other.m_lockingCgroup; this->m_launchPid = other.m_launchPid; this->m_launchTimestamp = other.m_launchTimestamp; } return *this; } ProcessInfo::ProcessInfo(const ProcessInfo &proc) { this->m_pid = proc.m_pid; this->m_childrenPid = proc.m_childrenPid; this->m_cgroup = proc.m_cgroup; this->m_wids = proc.m_wids; this->m_args = proc.m_args; this->m_tasks = proc.m_tasks; this->m_exe = proc.m_exe; this->m_cmdline = proc.m_cmdline; this->m_appStatus = proc.m_appStatus; this->m_appType = proc.m_appType; this->m_desktopFileName = proc.m_desktopFileName; this->m_lockingCgroup = proc.m_lockingCgroup; this->m_launchPid = proc.m_launchPid; this->m_launchTimestamp = proc.m_launchTimestamp; } ProcessInfo::ProcessInfo() : m_pid(-1) , m_cgroup(ProcessInfo::ForegroundProcess) , m_appStatus(ProcessInfo::Launching) , m_appType(ProcessInfo::Normal) , m_needingPolkitWindow(false) , m_lockingCgroup(false) , m_launchPid(-1) , m_launchTimestamp(0) { } qint64 ProcessInfo::pid() const { return m_pid; } void ProcessInfo::setPid(const qint64 &appPid) { m_pid = appPid; setChildrenPid(m_pid); } ProcessInfo::AppCGroup ProcessInfo::cgroup() const { return m_cgroup; } void ProcessInfo::setCGroup(const AppCGroup &appStatus) { m_cgroup = appStatus; } QList ProcessInfo::wids() const { return m_wids; } void ProcessInfo::setWids(const QList &appWids) { m_wids = appWids; } void ProcessInfo::setAppStatus(AppStatus status) { m_appStatus = status; if (status == ProcessInfo::Launching) { m_launchTimestamp = QDateTime::currentSecsSinceEpoch(); } } void ProcessInfo::setLaunchPid(int pid) { m_launchPid = pid; } int ProcessInfo::launchPid() const { return m_launchPid; } void ProcessInfo::setLockCgroup(bool locking) { m_lockingCgroup = locking; #if 0 if (locking) { QTimer::singleShot(15000, [this] { m_lockingCgroup = false; }); } #endif } bool ProcessInfo::lockingCgroup() const { return m_lockingCgroup; } ProcessInfo::AppStatus ProcessInfo::appStatus() const { return m_appStatus; } QString ProcessInfo::cmdline(int pid) { QString cmdLineFile = QString("/proc/%1/cmdline").arg(pid); QFile file(cmdLineFile); if (!file.open(QIODevice::ReadOnly)) { qWarning() << "Get cmdline failed, " << pid << file.errorString(); return QString(); } QByteArray cmd = file.readAll(); QString cmdLine; int startIndex = 0; for (int i=0; i ProcessInfo::getTasks() { setTasks(m_pid); return m_tasks; } void ProcessInfo::setTasks(const qint64 &appPid) { m_tasks.clear(); qDebug() << __FUNCTION__ << appPid; QList appAllPids; appAllPids << appPid << m_childrenPid; for (auto pid : appAllPids) { QString tasksFilePath = "/proc/" + QString::number(pid) + "/task"; QDir dir(tasksFilePath); // 取到所有的文件和文件名,但是去掉.和..的文件夹 dir.setFilter(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot); if (!dir.exists()) { qWarning() << tasksFilePath << "is not exists!"; continue; } // 判断目录中的信息是否为空 QFileInfoList fileLists = dir.entryInfoList(); if (fileLists.count() <= 0) { qWarning() << tasksFilePath << "info is entry!"; continue; } // 将线程Id存放进m_tasks列表中 for (int i=0; i ProcessInfo::childrenPids() { setChildrenPid(m_pid); return m_childrenPid; } void ProcessInfo::setChildrenPid(const qint64 &appPid) { m_childrenPid.clear(); findChildrenPids(appPid); qDebug() << "child child pids" << m_childrenPid; } void ProcessInfo::findChildrenPids(qint64 pid) { QList pids; std::string appChildrenPid = "pgrep -P " + QString::number(pid).toStdString(); FILE *fp = popen(appChildrenPid.c_str(), "r"); char nameBuff[200] = ""; for (int i = 0; fgets(nameBuff, 200, fp) != NULL; ++i) { std::string childrenInfo = nameBuff; std::string appChildrenInfo = std::string(childrenInfo.begin(), childrenInfo.begin() + childrenInfo.find('\n')); if (appChildrenInfo.empty()) { continue; } m_childrenPid << QString::fromStdString(appChildrenInfo).toInt(); pids << QString::fromStdString(appChildrenInfo).toInt(); } pclose(fp); if (pids.empty()) { return; } for (const auto &pid : pids) { findChildrenPids(pid); } } QDebug &operator<<(QDebug &dbg, const ProcessInfo &info) { QDebugStateSaver saver(dbg); dbg.nospace() << "pid:" << info.pid() << " wids:" << info.wids() << " appType:" << info.appType() << " args:" << info.getArgs() << " cgroup:" << info.cgroup() << " status:" << info.appStatus() << " desktop:" << info.desktopFileName() << " launchingTimestamp: " << info.launchTimestamp() << " lockingCgroup: " << info.lockingCgroup(); return dbg; } kylin-process-manager-4.0.0.0/process-manager/src/utils/misc.cpp0000644000175000017500000000415014536025312023766 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "misc.h" #include #include #include #include namespace utils { Misc::Misc(QObject *parent) : QObject(parent) { } bool utils::Misc::ensurePathExists(const QString &path) { QDir dir(path); if (dir.exists(path)) { return true; } if (!dir.mkdir(path)) { qWarning() << "mkdir failed: " << path; return false; } return true; } bool Misc::ensureFileExists(const QString &fileName) { QFile file(fileName); if (file.exists()) { return true; } if (!file.open(QIODevice::ReadWrite | QIODevice::Append)) { qWarning() << "open file failed: " << fileName; return false; } return true; } QJsonObject Misc::readJsonFromFile(const QString &fileName) { QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { qWarning() << "open json file failed: " << fileName; return QJsonObject(); } QJsonDocument jdc(QJsonDocument::fromJson(file.readAll())); file.close(); return jdc.object(); } bool Misc::writeJsonToFile(const QJsonObject &obj, const QString &fileName) { QFile jsonFile(fileName); if (!jsonFile.open(QIODevice::WriteOnly)) { qWarning() << "open json file failed: " << fileName; return false; } QJsonDocument jdoc(obj); jsonFile.write(jdoc.toJson()); jsonFile.flush(); jsonFile.close(); return true; } } // namespace utils kylin-process-manager-4.0.0.0/process-manager/src/utils/misc.h0000644000175000017500000000224314536025312023434 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef MISC_H #define MISC_H #include #include namespace utils { class Misc : public QObject { Q_OBJECT public: explicit Misc(QObject *parent = nullptr); static bool ensurePathExists(const QString &path); static bool ensureFileExists(const QString &fileName); static QJsonObject readJsonFromFile(const QString &fileName); static bool writeJsonToFile(const QJsonObject &obj, const QString &fileName); }; } // namespace utils #endif // MISC_H kylin-process-manager-4.0.0.0/process-manager/src/dbusservices/0000755000175000017500000000000014536025312023670 5ustar debiandebiankylin-process-manager-4.0.0.0/process-manager/src/dbusservices/applauncherdaemon.h0000644000175000017500000000317314536025312027533 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef APPLAUNCHERDAEMON_H #define APPLAUNCHERDAEMON_H #include class QSocketNotifier; class QDBusInterface; /// /// \brief The AppLauncherDaemon class /// class AppLauncherDaemon : public QObject { Q_OBJECT public: explicit AppLauncherDaemon(QObject *parent = nullptr); // Unix signal handlers. static void chldSignalHandler(int unused); static void quitSignalHandler(int unused); static void setupUnixSignalHandlers(); public Q_SLOTS: // Qt signal handlers. void handleSigChld(); void handleQuitSig(); private Q_SLOTS: void prepareForShutdown(bool shutDown); Q_SIGNALS: void childProcessFinished(quint32 pid); void aboutToQuitAppManager(); private: static int s_sigChldFd[2]; static int s_sigQuitFd[2]; QSocketNotifier *m_snChld; QSocketNotifier *m_snQuit; QDBusInterface *m_sessionmanagerInterface; quint32 m_logoutInhibitCookie; }; #endif // APPLAUNCHERDAEMON_H kylin-process-manager-4.0.0.0/process-manager/src/dbusservices/appmanagerservice.h0000644000175000017500000001437314536025312027545 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef APPMANAGERSERVICE_H #define APPMANAGERSERVICE_H #include #include QT_BEGIN_NAMESPACE class QByteArray; template class QList; template class QMap; class QString; class QStringList; class QVariant; QT_END_NAMESPACE /* * Adaptor class for interface com.kylin.AppManager */ class AppManagerService: public QDBusAbstractAdaptor { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "com.kylin.AppManager") Q_CLASSINFO("D-Bus Introspection", "" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" "") public: AppManagerService(QObject *parent); virtual ~AppManagerService(); public: // PROPERTIES public Q_SLOTS: // METHODS bool ActiveProcessByPid(int pid); bool ActiveProcessByWid(uint wid); bool AddToWhiteList(const QString &desktopFile, const QString &option); QString AppDesktopFileNameByPid(qlonglong pid); QString AppDesktopFileNameByWid(qlonglong wid); QStringList AppWhiteList(const QString &option); QString Inhibit(const QString &desktopFile, uint pid, const QString &reason, uint flags); bool LaunchApp(const QString &desktopFile); bool LaunchAppWithArguments(const QString &desktopFile, const QStringList &args); bool LaunchDefaultAppWithUrl(const QString &url); bool Open(const QString &fileName); QVector RecommendAppLists(const QString &fileName); bool RemoveFromWhiteList(const QString &desktopFile, const QString &option); void SetPowerSavingModeEnable(bool enable); bool ThawApps(); void UnInhibit(const QString &cookie); QStringList WhiteListsOfApp(const QString &desktopFile); Q_SIGNALS: // SIGNALS void appFinished(const QString &desktopNmae, qlonglong pid, uint wid); void appLaunched(const QString &desktopNmae, qlonglong pid, uint wid); }; #endif kylin-process-manager-4.0.0.0/process-manager/src/dbusservices/whitelistmanager.h0000644000175000017500000000613014536025312027410 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef WHITELISTMANAGER_H #define WHITELISTMANAGER_H #include #include #include class WhiteListManager : public QObject { Q_OBJECT public: explicit WhiteListManager(QObject *parent = nullptr); bool addToWhiteList(const QString &desktopFile, const QString &option); bool removeFromWhiteList(const QString &desktopFile, const QString &option); bool isExists(const QString &desktopFile, const QString &option); // 根据应用名称返回白名单列表 QStringList whiteListsOfApp(const QString &desktopFile); // 根据白名单名称返回应用列表 QStringList appWhiteList(const QString &option); Q_SIGNALS: void addedToWhitelist(QString desktopName); private: void initJsonFile(); void initConnections(); void updateWhitelistJsonInfo(const QString &path); private: const QString m_jsonFile; QJsonObject m_jsonObj; QJsonObject m_jsonObjTest; QStringList m_desktopList; QFileSystemWatcher *m_whitelistFileWatcher; const QStringList appManagerList = { "eye-protection-center.desktop", "mdm-acpanel.desktop", "sogouImeService.desktop", "ukui-search-menu.desktop", "mate-terminal.desktop", "kylin-weather.desktop", "ukui-clock.desktop", "peony-computer.desktop", "peony.desktop", "peony-home.desktop", "peony-trash.desktop", "kylin-connectivity.desktop", "ukui-control-center.desktop", "startqax.desktop", "onboard.desktop", "ukui-sidebar.desktop", "ukui-panel.desktop", "firefox.desktop", "google-chrome.desktop", "browser360-cn.desktop", "qaxbrowser-safe.desktop" }; const QStringList appUninstallList = { "kylin-screenshot.desktop", "ukui-notebook.desktop", "ukui-clock.desktop", "kylin-calculator.desktop", "kylin-recorder.desktop", "kylin-software-center.desktop", "kylin-camera.desktop", "biometric-manager.desktop", "yhkylin-backup-tools.desktop", "box-manager.desktop", "ukui-system-monitor.desktop", "ksc-defender.desktop", "logview.desktop", "kylin-service-support.desktop", "kylin-user-guide.desktop", "ukui-control-center.desktop", "peony.desktop", "engrampa.desktop" }; }; #endif // WHITELISTMANAGER_H kylin-process-manager-4.0.0.0/process-manager/src/dbusservices/appmanagerservice.cpp0000644000175000017500000001415014536025312030071 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "appmanagerservice.h" #include #include #include #include #include #include #include /* * Implementation of adaptor class AppManagerService */ AppManagerService::AppManagerService(QObject *parent) : QDBusAbstractAdaptor(parent) { // constructor setAutoRelaySignals(true); } AppManagerService::~AppManagerService() { // destructor } bool AppManagerService::ActiveProcessByPid(int pid) { // handle method call com.kylin.AppManager.ActiveProcessByPid bool succeed; QMetaObject::invokeMethod(parent(), "ActiveProcessByPid", Q_RETURN_ARG(bool, succeed), Q_ARG(int, pid)); return succeed; } bool AppManagerService::ActiveProcessByWid(uint wid) { // handle method call com.kylin.AppManager.ActiveProcessByWid bool succeed; QMetaObject::invokeMethod(parent(), "ActiveProcessByWid", Q_RETURN_ARG(bool, succeed), Q_ARG(uint, wid)); return succeed; } bool AppManagerService::AddToWhiteList(const QString &desktopFile, const QString &option) { // handle method call com.kylin.AppManager.AddToWhiteList bool succeed; QMetaObject::invokeMethod(parent(), "AddToWhiteList", Q_RETURN_ARG(bool, succeed), Q_ARG(QString, desktopFile), Q_ARG(QString, option)); return succeed; } QString AppManagerService::AppDesktopFileNameByPid(qlonglong pid) { // handle method call com.kylin.AppManager.AppDesktopFileNameByPid QString desktopFileName; QMetaObject::invokeMethod(parent(), "AppDesktopFileNameByPid", Q_RETURN_ARG(QString, desktopFileName), Q_ARG(qlonglong, pid)); return desktopFileName; } QString AppManagerService::AppDesktopFileNameByWid(qlonglong wid) { // handle method call com.kylin.AppManager.AppDesktopFileNameByWid QString desktopFileName; QMetaObject::invokeMethod(parent(), "AppDesktopFileNameByWid", Q_RETURN_ARG(QString, desktopFileName), Q_ARG(qlonglong, wid)); return desktopFileName; } QStringList AppManagerService::AppWhiteList(const QString &option) { // handle method call com.kylin.AppManager.AppWhiteList QStringList appLists; QMetaObject::invokeMethod(parent(), "AppWhiteList", Q_RETURN_ARG(QStringList, appLists), Q_ARG(QString, option)); return appLists; } QString AppManagerService::Inhibit(const QString &desktopFile, uint pid, const QString &reason, uint flags) { // handle method call com.kylin.AppManager.Inhibit QString cookie; QMetaObject::invokeMethod(parent(), "Inhibit", Q_RETURN_ARG(QString, cookie), Q_ARG(QString, desktopFile), Q_ARG(uint, pid), Q_ARG(QString, reason), Q_ARG(uint, flags)); return cookie; } bool AppManagerService::LaunchApp(const QString &desktopFile) { // handle method call com.kylin.AppManager.LaunchApp bool succeed; QMetaObject::invokeMethod(parent(), "LaunchApp", Q_RETURN_ARG(bool, succeed), Q_ARG(QString, desktopFile)); return succeed; } bool AppManagerService::LaunchAppWithArguments(const QString &desktopFile, const QStringList &args) { // handle method call com.kylin.AppManager.LaunchAppWithArguments bool succeed; QMetaObject::invokeMethod(parent(), "LaunchAppWithArguments", Q_RETURN_ARG(bool, succeed), Q_ARG(QString, desktopFile), Q_ARG(QStringList, args)); return succeed; } bool AppManagerService::LaunchDefaultAppWithUrl(const QString &url) { // handle method call com.kylin.AppManager.LaunchDefaultAppWithUrl bool succeed; QMetaObject::invokeMethod(parent(), "LaunchDefaultAppWithUrl", Q_RETURN_ARG(bool, succeed), Q_ARG(QString, url)); return succeed; } bool AppManagerService::Open(const QString &fileName) { // handle method call com.kylin.AppManager.Open bool succeed; QMetaObject::invokeMethod(parent(), "Open", Q_RETURN_ARG(bool, succeed), Q_ARG(QString, fileName)); return succeed; } QVector AppManagerService::RecommendAppLists(const QString &fileName) { // handle method call com.kylin.AppManager.RecommendAppLists QVector appLists; QMetaObject::invokeMethod(parent(), "RecommendAppLists", Q_RETURN_ARG(QVector , appLists), Q_ARG(QString, fileName)); return appLists; } bool AppManagerService::RemoveFromWhiteList(const QString &desktopFile, const QString &option) { // handle method call com.kylin.AppManager.RemoveFromWhiteList bool succeed; QMetaObject::invokeMethod(parent(), "RemoveFromWhiteList", Q_RETURN_ARG(bool, succeed), Q_ARG(QString, desktopFile), Q_ARG(QString, option)); return succeed; } void AppManagerService::SetPowerSavingModeEnable(bool enable) { // handle method call com.kylin.AppManager.SetPowerSavingModeEnable QMetaObject::invokeMethod(parent(), "SetPowerSavingModeEnable", Q_ARG(bool, enable)); } bool AppManagerService::ThawApps() { // handle method call com.kylin.AppManager.ThawApps bool succeed; QMetaObject::invokeMethod(parent(), "ThawApps", Q_RETURN_ARG(bool, succeed)); return succeed; } void AppManagerService::UnInhibit(const QString &cookie) { // handle method call com.kylin.AppManager.UnInhibit QMetaObject::invokeMethod(parent(), "UnInhibit", Q_ARG(QString, cookie)); } QStringList AppManagerService::WhiteListsOfApp(const QString &desktopFile) { // handle method call com.kylin.AppManager.WhiteListsOfApp QStringList whiteLists; QMetaObject::invokeMethod(parent(), "WhiteListsOfApp", Q_RETURN_ARG(QStringList, whiteLists), Q_ARG(QString, desktopFile)); return whiteLists; } kylin-process-manager-4.0.0.0/process-manager/src/dbusservices/applauncher.h0000644000175000017500000001455014536025312026350 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #ifndef APPLAUNCHER_H #define APPLAUNCHER_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "utils/processinfo.h" #include "dbusinterfaces/cgroupinterface.h" #include "dbusinterfaces/kwininterface.h" #include "dbusservices/whitelistmanager.h" #include "common.h" #include "core/appinfomanager.h" extern "C" { #include } class QProcess; class QTimer; class QFileSystemWatcher; class ChildrenProcessWatcher; class AppLauncherDaemon; class WhiteListManager; class ProcessInfo; class AppLauncher : public QObject { Q_OBJECT public: typedef QList ProcessList ; typedef QMap AppsInfo; explicit AppLauncher(QObject *parent = nullptr); bool LaunchApp(const QString &desktopFile); bool LaunchAppWithArguments(const QString &desktopFile,const QStringList &args); bool Open(const QString &fileName); bool LaunchDefaultAppWithUrl(const QString &url); bool ActiveProcessByWid(const uint &wid); bool ActiveProcessByPid(const int &pid); QString AppDesktopFileNameByPid(qint64 pid); QString AppDesktopFileNameByWid(qint64 wid); /** * @brief RecommendAppLists 获取可打开文件的推荐应用列表 * @param fileName 文件名称 * @return 返回默认打开应用和推荐打开应用的列表容器 */ QVector RecommendAppLists(const QString &fileName); Q_SIGNALS: void aboutToQuitMainApp(); private: /** * @brief desktopFileExec 获取desktop文件中Exec字段 * @param desktopFileName desktop名称 * @return Exec字段 */ QPair desktopFileExec(const QString &desktopFileName); /// /// \brief hasWindow 根据desktop文件判断当前文件是否会显示窗口 /// \param desktopFile /// \return /// bool hasWindow(const QString &desktopFile); /** * @brief desktopFullFilename 获得带全路径的desktop名称 * @param desktopFileName 应用的desktop名称 * @return 带全路径的desktop名称 */ static QString desktopFullFilename(const QString &desktopFileName); /** * @brief doLaunchApp 启动应用 * @param desktopFile 应用的desktop名称 * @param args 启动参数 * @return 启动成功返回true */ bool doLaunchApp(const QString &desktopFile, const QStringList &args = QStringList()); /** * @brief launch 打开应用 * @param execPath 应用名称 * @param args 启动参数 * @return 启动成功返回true */ bool launch(const QString &desktopFile, const QString &execPath, const QStringList &args = QStringList()); /** * @brief launchDesktopApp 优先使用GIO的接口打开应用,如果打开失败尝试调用 \memberof launch * @param desktopFile 绝对路径desktop文件 * @param args 指定的参数 * @param execPath 应用的启动二进制文件,用于 \memberof launch函数 * @param execArgs 启动参数,用于 \memberof launch函数 * @return 启动成功返回true */ bool launchDesktopApp(const QString &desktopFile, const QStringList &args, const QString &execPath, const QStringList &execArgs); /** * @brief activeAppWindow * @param wid * @param pids * @return */ bool activeAppWindow(quint32 wid, QList pids); bool activePolkitWindow(); static void childProcessCallback(GDesktopAppInfo *appInfo, GPid pid, gpointer userData); /** * @brief getLaunchAppLists 利用gio的接口获取文件的默认打开应用和推荐打开应用列表 * @param fileName 文件名称 * @return 返回可打开文件的应用列表 */ QVector getLaunchAppLists(const QString &fileName); private: org::ukui::KWin *m_kwinInterface; bool m_isWayland; // QMap> m_desktopExec; QMutex m_mutex; int m_polkitWindowPid; uint m_polkitWindowId; AppInfoManager &m_appInfoManager = common::Singleton::GetInstance(); const quint64 interval = 10;//30 * 60; const QString kKmreAppMainWindow = "/usr/bin/kylin-kmre-window"; const QStringList polkitWindowPidCmdline = { "/usr/lib/x86_64-linux-gnu/ukui-polkit/polkit-ukui-authentication-agent-1", "/usr/lib/aarch64-linux-gnu/ukui-polkit/polkit-ukui-authentication-agent-1" }; const QStringList kNeedingPolkitWindowApps = { "gparted.desktop" }; const QStringList kFreeApps = { "ukui-search-menu.desktop", "onboard.desktop" }; struct { int maxname; int fd[2]; int launcher[2]; /* socket pair for launcher communication */ int deadpipe[2]; /* pipe used to detect dead children */ int initpipe[2]; int wrapper; /* socket for wrapper communication */ int accepted_fd; /* socket accepted and that must be closed in the child process */ char result; int exit_status; pid_t fork; pid_t launcher_pid; pid_t kded_pid; int n; char **argv = nullptr; int (*func)(int, char *[]); int (*launcher_func)(int); bool debug_wait; QByteArray errorMsg; bool launcher_ok; bool suicide; } d; }; #endif // APPLAUNCHER_H kylin-process-manager-4.0.0.0/process-manager/src/dbusservices/whitelistmanager.cpp0000644000175000017500000001201514536025312027742 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "whitelistmanager.h" #include #include #include #include #include #include #include #include "utils/misc.h" WhiteListManager::WhiteListManager(QObject *parent) : QObject(parent) , m_jsonFile(QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/kylin-app-manager/whitelists") , m_whitelistFileWatcher(new QFileSystemWatcher(this)) { initConnections(); initJsonFile(); } bool WhiteListManager::addToWhiteList(const QString &desktopFile, const QString &option) { if (!m_jsonObj.contains(option)) { qWarning() << "unkwon option: " << option << m_jsonObj.keys(); return false; } QJsonArray arr = m_jsonObj[option].toArray(); if (arr.contains(desktopFile)) { return true; } arr.append(desktopFile); m_jsonObj[option] = arr; if (!utils::Misc::writeJsonToFile(m_jsonObj, m_jsonFile)) { qWarning() << __FUNCTION__ << "writeJsonToFile has failed!"; return false; } Q_EMIT addedToWhitelist(desktopFile); return true; } bool WhiteListManager::removeFromWhiteList(const QString &desktopFile, const QString &option) { qDebug() << __FUNCTION__ << m_jsonObj[option]; if (!m_jsonObj.contains(option)) { qWarning() << "unkwon option: " << option << m_jsonObj.keys(); return false; } QJsonArray arr = m_jsonObj[option].toArray(); if (!arr.contains(desktopFile)) { return true; } for (int i=0; iaddPath(m_jsonFile); connect(m_whitelistFileWatcher, &QFileSystemWatcher::fileChanged, this, &WhiteListManager::updateWhitelistJsonInfo); } void WhiteListManager::updateWhitelistJsonInfo(const QString &path) { QFile dir(path); if (!dir.exists()) { qWarning() << __FUNCTION__ << path << " is not exists"; return; } m_jsonObj = utils::Misc::readJsonFromFile(m_jsonFile); } kylin-process-manager-4.0.0.0/process-manager/src/dbusservices/com.kylin.AppManager.xml0000644000175000017500000000767514536025312030346 0ustar debiandebian kylin-process-manager-4.0.0.0/process-manager/src/dbusservices/applauncher.cpp0000644000175000017500000005511014536025312026700 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "applauncher.h" #include #include #include #include #include #include #include #include "core/eventwatcher.h" extern "C" { #include #include #include #include } #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) #define KWIN_SERVICE "org.ukui.KWin" #else #define KWIN_SERVICE "org.kde.KWin" #endif AppLauncher::AppLauncher(QObject *parent) : QObject(parent) , m_kwinInterface(new org::ukui::KWin(KWIN_SERVICE, "/KWin", QDBusConnection::sessionBus(), this)) , m_isWayland(false) { if (QGuiApplication::platformName().startsWith("wayland", Qt::CaseInsensitive)) { m_isWayland = true; } } bool AppLauncher::LaunchApp(const QString &desktopFile) { return doLaunchApp(desktopFile); } bool AppLauncher::ActiveProcessByWid(const uint &wid) { return m_appInfoManager.thawApp(wid); } bool AppLauncher::ActiveProcessByPid(const int &pid) { qDebug() << current_func << pid; return m_appInfoManager.thawApp(pid); } bool AppLauncher::LaunchAppWithArguments(const QString &desktopFile, const QStringList &args) { return doLaunchApp(desktopFile, args); } bool AppLauncher::Open(const QString &fileName) { if (fileName.isEmpty()) { qWarning() << __FUNCTION__ << "fileName is NULL!"; return false; } QUrl url(fileName); QFile file(fileName); if (!url.scheme().isEmpty()) { // 传入的参数为url类型 return LaunchDefaultAppWithUrl(fileName); } if (!file.exists()) { qWarning() << __FUNCTION__ << fileName << "is invalid!"; return false; } QVector launchApplists = getLaunchAppLists(fileName); if (launchApplists.isEmpty()) { qWarning() << __FUNCTION__ << fileName << "is not have apps to launch!"; return false; } if (!launchApplists.first().isEmpty()) { // 选用默认应用打开文件 for (int i=0; i AppLauncher::RecommendAppLists(const QString &fileName) { if (fileName.isEmpty()) { qWarning() << __FUNCTION__ << fileName << "is NULL!"; return QVector(); } QUrl url(fileName); QFile file(fileName); if (url.scheme().isEmpty()) { if (file.exists() && url.isRelative()) { // fileName为本地文件类型 return getLaunchAppLists(fileName); } } else { QString localFileName = url.toLocalFile(); if (QFile(localFileName).exists()) { // fileName为url类型 return getLaunchAppLists(localFileName); } } qWarning() << fileName << "is not exists!"; return QVector(); } QVector AppLauncher::getLaunchAppLists(const QString &fileName) { QVector appLists; QStringList defaultLists; QStringList recommendLists; // 获取文件的mime-type QMimeDatabase fileDate; QMimeType fileMimeType = fileDate.mimeTypeForFile(fileName); // 获取默认打开应用 GAppInfo *appInfo = g_app_info_get_default_for_type(fileMimeType.name().toUtf8().constData(), false); if (appInfo == nullptr) { qWarning() << "appInfo is NULL, localFilePath not have default launch app!"; appLists.push_back(QStringList()); } else { const char *defaultAppName = g_app_info_get_id(appInfo); defaultLists.append(QString::fromUtf8(defaultAppName)); appLists.push_back(defaultLists); } // 获取推荐打开应用列表 GList *app_infos = g_app_info_get_recommended_for_type(fileMimeType.name().toUtf8().constData()); GList *l = app_infos; if (app_infos == nullptr) { qWarning() << "app_infos is NULL, localFilePath not have recommend launch apps!"; appLists.push_back(QStringList()); } else { while (l != nullptr) { auto app_info = static_cast (l->data); if (app_info == nullptr) { qWarning() << "app_info is nullptr!"; recommendLists.append(""); l = l->next; continue; } else { const char *appDesktopName = g_app_info_get_id(app_info); if (defaultLists.contains(QString::fromUtf8(appDesktopName))) { l = l->next; continue; } recommendLists.append(QString::fromUtf8(appDesktopName)); l = l->next; } g_object_unref(app_info); } appLists.push_back(recommendLists); } g_list_free(l); g_list_free(app_infos); return appLists; } QPair AppLauncher::desktopFileExec(const QString &desktopFileName) { if (desktopFileName.isEmpty()) { qWarning() << __FUNCTION__ << "desktopFileName is NULL!!!"; return QPair(); } else { QString fullDesktopName = desktopFullFilename(desktopFileName); KDesktopFile desktop(fullDesktopName); QString exec = desktop.entryMap("Desktop Entry").value("Exec"); if (exec.isEmpty()) { qWarning() << __FUNCTION__ << "Exec is empty!"; return {QString(), QStringList()}; } exec.remove(QRegExp("%.")); exec.remove(QRegExp("^\"")); exec.remove(QRegExp(" *$")); exec.remove(QRegExp("\"")); QStringList execArgs = exec.split(" "); if (execArgs.count() > 0) { exec = execArgs.first(); } return { exec, execArgs }; } } bool AppLauncher::hasWindow(const QString &desktopFile) { QString fullDesktopName = desktopFullFilename(desktopFile); QScopedPointer desktop(new QSettings(fullDesktopName, QSettings::IniFormat)); desktop->setIniCodec(QTextCodec::codecForName("UTF-8")); QString keywords = desktop->value("Desktop Entry/Keywords").toString(); keywords.remove(QRegExp("%.")); keywords.remove(QRegExp("^\"")); keywords.remove(QRegExp(" *$")); if (keywords.contains("screenshot") || keywords.contains("capture") || keywords.contains("shutter")) { return false; } return true; } QString AppLauncher::desktopFullFilename(const QString &desktopFileName) { QFile desktopFile(desktopFileName); if (desktopFile.exists()) { return desktopFileName; } QStringList desktopfilePaths = QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation); for (auto &path : qAsConst(desktopfilePaths)) { if (desktopFileName.contains(path)) { return desktopFileName; } else if (QFile::exists(path + "/" + desktopFileName)) { return path + "/" + desktopFileName; } } return QString(); } bool AppLauncher::doLaunchApp(const QString &desktopFile, const QStringList &args) { qDebug() << current_func << desktopFile; QString fullDesktopName = desktopFullFilename(desktopFile); if (fullDesktopName.isEmpty()) { qWarning() << "Launch app failed, the dekstop file is " << desktopFile; return false; } if (!KDesktopFile::isDesktopFile(fullDesktopName)) { return false; } qDebug() << "fullDesktopName" << fullDesktopName; #if 1 //crashed: corrupted double-linked list. mybe a bug QScopedPointer file(new KDesktopFile(fullDesktopName)); if (!file->hasApplicationType() && !file->tryExec()) { return false; } #endif // not full path file name QString desktopFilename = fullDesktopName.split("/").last(); bool launchResult = false; d.fork = 0; QStringList startUpArgs = args; QPair execArgs; // m_desktopExec保存全路径下的desktop名称 if (m_appInfoManager.desktopFilesExec().contains(fullDesktopName)) { execArgs = m_appInfoManager.desktopFilesExec().value(fullDesktopName); } else { execArgs = desktopFileExec(desktopFile); } if (args.isEmpty()) { startUpArgs = execArgs.second; } // 文件打开方式选择kmre应用打开时,只能启动应用,不能打开文件 if (m_appInfoManager.isKmreApp(desktopFile)) { startUpArgs = execArgs.second; for (int i=0; i 1) { if (execArgs.second[0] == ("sh") && execArgs.second[1] == ("-c")) { QStringList execList; QStringList execStr; execList << "sh" << "-c"; for (int i=2; i " << "app is running " << wids << pids; if (wids.isEmpty() || pids.isEmpty() || !activeAppWindow(wids.last(), pids)) { launchResult = launchDesktopApp(fullDesktopName, args, exec, startUpArgs); } else { launchResult = true; } break; } default: break; } } return launchResult; } bool AppLauncher::activeAppWindow(quint32 wid, QList pids) { if (Policy::isWaylandPlatform()) { // kysdk的activateWindow接口没有返回值 kdk::WindowManager::activateWindow(wid); return true; } for (auto &pid : qAsConst(pids)) { bool ret = m_kwinInterface->activeWindowByPid(pid); qDebug() << "avtive window ret " <activeWindowByPid(m_polkitWindowPid); } } bool AppLauncher::launch(const QString &desktopFile, const QString &execPath, const QStringList &args) { int dummyFd[2]; if (pipe(d.fd) < 0 || pipe(dummyFd) < 0) { qWarning() << "pipe() failed," << strerror(errno); d.fork = 0; return false; } { if (d.argv == nullptr) { d.argv = new char*[args.count() + 1]; for (int i=0; idata(); } d.argv[args.count()] = nullptr; } } d.fork = fork(); switch (d.fork) { case -1: { qWarning() << execPath << "fork failed, " << strerror(errno); close(d.fd[0]); close(d.fd[1]); d.fork = 0; return false; } // child process case 0: { close(d.fd[0]); close(dummyFd[1]); char dummy; read(dummyFd[0], &dummy, 1); close(dummyFd[0]); d.result = 2; // Try execing write(d.fd[1], &d.result, 1); // We set the close on exec flag. // Closing of d.fd[1] indicates that the execvp succeeded! fcntl(d.fd[1], F_SETFD, FD_CLOEXEC); QByteArray executable; executable.append(execPath); if (!executable.isEmpty()) { execvp(executable.constData(), d.argv); delete [] d.argv; d.argv = nullptr; } qWarning() << "Launch error" << strerror(errno); d.result = 1; // Error write(d.fd[1], &d.result, 1); close(d.fd[1]); exit(255); break; } default: { appinfo::AppType type = m_appInfoManager.isKmreApp(desktopFile) ? appinfo::Kmre : appinfo::Normal; m_appInfoManager.newAppInstance(desktopFile, args, QList() << d.fork, type); close(d.fd[1]); close(dummyFd[0]); char dummy = 1; write(dummyFd[1], &dummy, 1); close(dummyFd[1]); bool exec = false; for (;;) { d.n = read(d.fd[0], &d.result, 1); if (d.n == 1) { if (d.result == 2) { exec = true; continue; } if (d.result == 3) { int l = 0; d.n = read(d.fd[0], &l, sizeof(int)); if (d.n == sizeof(int)) { QByteArray tmp; tmp.resize(l + 1); d.n = read(d.fd[0], tmp.data(), l); tmp[l] = 0; if (d.n == l) { d.errorMsg = tmp; } } } qWarning() << "process finished" << errno << "str error " << strerror(errno); // Finished break; } if (d.n == -1) { if (errno == ECHILD) { // a child died. continue; } if (errno == EINTR || errno == EAGAIN) { // interrupted or more to read continue; } } if (d.n == 0) { if (exec) { d.result = 0; } else { perror("Pipe closed unexpectedly"); d.result = 1; // Error } qWarning() << "process finished1" << errno << "str error " << strerror(errno);; break; } qWarning() << "Error reading from pipe"; d.result = 1; // Error } close(d.fd[0]); break; } } delete [] d.argv; d.argv = nullptr; return true; } bool AppLauncher::launchDesktopApp(const QString &desktopFile, const QStringList &args, const QString &execPath, const QStringList &execArgs) { qDebug() << current_func << desktopFile; GDesktopAppInfo *appInfo = g_desktop_app_info_new_from_filename(desktopFile.toUtf8().constData()); if (!appInfo) { qWarning() << __FUNCTION__ << "appInfo is null"; return false; } GList *l = nullptr; if (!args.isEmpty()) { QString filePath = execArgs.join(" ").contains("sh -c") ? execArgs.join(" ") : args.join(" "); QUrl url(filePath); QFile file(filePath); if (url.isValid() && url.scheme().isEmpty()) { if (file.exists() && url.isRelative()) { QString fileUriPath = "file://" + filePath; char *uri = g_strdup(fileUriPath.toUtf8().constData()); l = g_list_prepend(l, uri); } else { qDebug() << filePath << "is not localFile, maybe is command line!"; return launch(desktopFile, execPath, execArgs); } } else { qDebug() << filePath << " is url type !"; for (auto uri : qAsConst(args)) { l = g_list_prepend(l, g_strdup(uri.toUtf8().constData())); } } } gboolean ret = g_desktop_app_info_launch_uris_as_manager(appInfo, l, nullptr, G_SPAWN_DEFAULT, nullptr, nullptr, childProcessCallback, (gpointer)&execArgs, nullptr); g_list_free(l); if (ret == FALSE) { return launch(desktopFile, execPath, execArgs); } return true; } void AppLauncher::childProcessCallback(GDesktopAppInfo *appInfo, GPid pid, gpointer userData) { QString desktopFile = g_desktop_app_info_get_filename(appInfo); if (desktopFile.isEmpty() || !desktopFile.endsWith(".desktop")) { return; } QStringList strList = desktopFile.split("/"); QString desktopFileName = strList.isEmpty() ? desktopFile : strList.constLast(); qDebug() << "gio launched pid: " << pid; QStringList *args = (QStringList *)userData; appinfo::AppType type = AppInfoManager::isKmreApp(desktopFile) ? appinfo::Kmre : appinfo::Normal; common::Singleton::GetInstance().newAppInstance(desktopFile, *args, QList() << pid, type); } kylin-process-manager-4.0.0.0/process-manager/src/dbusservices/applauncherdaemon.cpp0000644000175000017500000001375514536025312030075 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "applauncherdaemon.h" #include #include #include #include #include #include extern "C" { #include #include #include #include #include #include } int AppLauncherDaemon::s_sigChldFd[2]; int AppLauncherDaemon::s_sigQuitFd[2]; AppLauncherDaemon::AppLauncherDaemon(QObject *parent) : QObject(parent) , m_logoutInhibitCookie(0) { // when app starts with '&' in shell, AppManager takes over it. if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0) { qWarning() << "Failed to make us a subreaper: " << strerror(errno); } if (::socketpair(AF_UNIX, SOCK_STREAM, 0, s_sigChldFd)) { qFatal("Couldn't create CHLD socketpair"); } if (::socketpair(AF_UNIX, SOCK_STREAM, 0, s_sigQuitFd)) { qFatal("Couldn't create Quit socketpair"); } m_snChld = new QSocketNotifier(s_sigChldFd[1], QSocketNotifier::Read, this); m_snQuit = new QSocketNotifier(s_sigQuitFd[1], QSocketNotifier::Read, this); connect(m_snChld, &QSocketNotifier::activated, this, &AppLauncherDaemon::handleSigChld); connect(m_snQuit, &QSocketNotifier::activated, this, &AppLauncherDaemon::handleQuitSig); connect(qApp, &QCoreApplication::aboutToQuit, this, &AppLauncherDaemon::aboutToQuitAppManager); setupUnixSignalHandlers(); bool shutDownConnection = QDBusConnection::systemBus().connect("org.freedesktop.login1", "/org/freedesktop/login1/Manager", "org.freedesktop.login1.Manager", "PrepareForShutdown", this, SLOT(prepareForShutdown(bool))); if (!shutDownConnection) { qWarning() << "connect to PrepareForShutdown failed!"; } /// FIXME #if 0 m_sessionmanagerInterface = new QDBusInterface("org.gnome.SessionManager", "/org/gnome/SessionManager", "org.gnome.SessionManager", QDBusConnection::sessionBus()); auto reply = m_sessionmanagerInterface->asyncCallWithArgumentList("Inhibit", QVariantList() << "kylin-app-manager" << quint32(0) << "Delete CGroups" << quint32(1)); reply.waitForFinished(); if (reply.isError()) { qDebug() << "Add Inhibit Error: " << reply.error(); } else { m_logoutInhibitCookie = reply.reply().arguments().first().toUInt(); qDebug() << "m_logoutInhibitCookie " << m_logoutInhibitCookie; } #endif } void AppLauncherDaemon::chldSignalHandler(int unused) { Q_UNUSED(unused) char a = 1; ::write(s_sigChldFd[0], &a, sizeof(a)); } void AppLauncherDaemon::quitSignalHandler(int unused) { Q_UNUSED(unused) #ifdef QT_DEBUG qDebug() << "handle linux signal: " << unused; #endif char a = 1; ::write(s_sigQuitFd[0], &a, sizeof(a)); } void AppLauncherDaemon::setupUnixSignalHandlers() { struct sigaction chld, term, inter; chld.sa_handler = AppLauncherDaemon::chldSignalHandler; sigemptyset(&chld.sa_mask); chld.sa_flags = 0; chld.sa_flags |= SA_RESTART; if (sigaction(SIGCHLD, &chld, 0)) { qWarning() << "handle SIGCHLD failed: " << errno; } term.sa_handler = AppLauncherDaemon::quitSignalHandler; sigemptyset(&term.sa_mask); term.sa_flags = 0; term.sa_flags |= SA_RESTART; if (sigaction(SIGTERM, &term, 0)) { qWarning() << "handle SIGTERM failed: " << errno; } inter.sa_handler = AppLauncherDaemon::quitSignalHandler; sigemptyset(&inter.sa_mask); inter.sa_flags = 0; inter.sa_flags |= SA_RESTART; if (sigaction(SIGINT, &inter, 0)) { qWarning() << "handle SIGINT failed: " << errno; } } void AppLauncherDaemon::handleSigChld() { m_snChld->setEnabled(false); char tmp; ::read(s_sigChldFd[1], &tmp, sizeof(tmp)); int pid = waitpid(-1, NULL, WNOHANG); // No child processes. Case of starting with QProcess // When the app launches with QProcess, waitpid() would return -1 // and set errno 10 what means "No child process". // Startup with "&" with QProcess in shell, // waitpid() would return the child process id. if (pid > 0) { Q_EMIT childProcessFinished(pid); } m_snChld->setEnabled(true); } void AppLauncherDaemon::handleQuitSig() { m_snChld->setEnabled(false); char tmp; ::read(s_sigQuitFd[1], &tmp, sizeof(tmp)); qDebug() << "handle quit sig"; Q_EMIT aboutToQuitAppManager(); m_snChld->setEnabled(true); qApp->quit(); } void AppLauncherDaemon::prepareForShutdown(bool shutDown) { if (shutDown) { qDebug() << "About to shutdown"; Q_EMIT aboutToQuitAppManager(); #if 0 m_sessionmanagerInterface->asyncCallWithArgumentList("Uninhibit", QVariantList() << m_logoutInhibitCookie); #endif } } kylin-process-manager-4.0.0.0/process-manager/src/memorymonitor.cpp0000644000175000017500000000403314536025312024613 0ustar debiandebian/* * Copyright 2023 KylinSoft Co., Ltd. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ #include "memorymonitor.h" #include #include MemoryMonitor::MemoryMonitor(QObject *parent) : QObject(parent) { m_timer = new QTimer(this); connect(m_timer,SIGNAL(timeout()),this,SLOT(getSystemInfo())); startTime(30000); } void MemoryMonitor::getSystemInfo() { QProcess process; process.start("cat /proc/meminfo"); bool processret = process.waitForFinished(); if (!processret) { return; } while (!process.atEnd()) { QString str = process.readLine(); if (str.startsWith("MemTotal")) { str.replace("\n",""); str.replace(QRegExp("( ){1,}")," "); auto lst = str.split(" "); if (lst.size() > 2) { m_memtotal = lst[1].toLong(); } } if (str.startsWith("MemAvailable")) { str.replace("\n",""); str.replace(QRegExp("( ){1,}")," "); auto lst = str.split(" "); if(lst.size() > 2) m_memavailable = lst[1].toLong(); } } if ((m_memtotal != 0) && (m_memavailable != 0)) { m_available = m_memavailable / m_memtotal; if(m_available<0.1) { qDebug() << "Available memory less than %10" << endl; Q_EMIT lowMemoryWaining(); } } } void MemoryMonitor::startTime(int time) { m_timer->start(time); }