/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* 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    */
/* SECURITY EXPLORATIONS 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.                                                                  */

import java.lang.*;
import java.io.*;
import tv.osmosys.system.System;

public class Sys {
 public static final int BUFSIZE = 0x8000;

 private static NativeCode nc;

 private static int buf;
 private static int klogctl;

 static {
  nc=NativeCode.getInstance();
  buf=nc.malloc(BUFSIZE);

  klogctl=nc.get_libc_sym_addr("klogctl");
 }

 public static byte[] dmesg_buf() {
  int res=nc.invoke(klogctl,3,buf,BUFSIZE,0,0,0,0,0);

  if (res>0) return Utils.buf2barray(buf,res);
   else return null;
 }

 public static void print_dmesg() {
  try {
   byte dmesg[]=dmesg_buf();

   if (dmesg!=null) {
    ByteArrayInputStream in=new ByteArrayInputStream(dmesg);
    BufferedReader b=new BufferedReader(new InputStreamReader(in,"utf-8"));

    String line=b.readLine();

    while(line!=null) {
     Output.text(line);
    
     line=b.readLine();
    }

    b.close();
    in.close();
   }
  } catch(Throwable t) {}
 }

 public static void reboot() {
  System.reboot("reset",System.REBOOTTYPE_USER);
 }

}
