X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=lustre%2Futils%2Fltrack_stats.c;h=e8710c6d6f91f9ef3a78c98d282914784ff54eac;hb=20d7508afa3b6f3ac703b4d43e5b1ad0ba782f19;hp=3aee89f70b9c29272fb9ef8ae58e651458fc63fc;hpb=53d3367a988039b58d8a26ef747c0b317567a637;p=fs%2Flustre-release.git diff --git a/lustre/utils/ltrack_stats.c b/lustre/utils/ltrack_stats.c index 3aee89f..e8710c6 100644 --- a/lustre/utils/ltrack_stats.c +++ b/lustre/utils/ltrack_stats.c @@ -1,6 +1,4 @@ -/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*- - * vim:expandtab:shiftwidth=8:tabstop=8: - * +/* * GPL HEADER START * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. @@ -26,8 +24,10 @@ * GPL HEADER END */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved + * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. * Use is subject to license terms. + * + * Copyright (c) 2014, Intel Corporation. */ /* * This file is part of Lustre, http://www.lustre.org/ @@ -47,7 +47,7 @@ #include #include #include - +#include #define TRACK_BY_GID 0 #define TRACK_BY_PPID 1 @@ -68,14 +68,10 @@ #define LEN_CLIENT 1024 /* size of output of llstat command we read at a time */ -#define MAX 1024 - -/* max strlen of outfile we get on command line */ -#define LEN_OUT 1024 +#define LLSTAT_READ_SIZE 1024 /* Length of command given on command line */ #define COMM_LEN 4096 -pid_t llstat[1024]; /* print usage */ void print_usage() @@ -202,12 +198,12 @@ void check_llstat() pid_t fork_llstat_command(char* llstat_file,char* stats_path) { - char truncate_command[100]; - char llstat_command[LEN_LLSTAT]; - pid_t pid_llstat_command; - FILE *fp_popen, *fp_out; - char buffer[MAX]; - int ret; + char truncate_command[100]; + char llstat_command[LEN_LLSTAT]; + pid_t pid_llstat_command; + FILE *fp_popen, *fp_out; + char buffer[LLSTAT_READ_SIZE]; + int ret; /* Truncating llstat output file as it will be opened in while * loop to append output */ @@ -238,37 +234,37 @@ pid_t fork_llstat_command(char* llstat_file,char* stats_path) "\"%s\"n", llstat_command); exit(1); } - while (fgets(buffer, 1024, fp_popen) != NULL) { - /* Following code should be in while loop as llstat - * will keep on sending output each second and will - * not exit on itself. It will be killed when we finsh - * with our command so we must make the output file - * consistent after writing each 1024 bytes chunk */ - - /* opening file where llstat will write its output */ - fp_out = fopen(llstat_file, "a"); - if (!fp_out) { - fprintf(stderr, "Error: Couldn't open llstat" - "outfile file: %s\n", - llstat_file); - exit(1); - } - /* fgets reads the popen output and fprintf writes it to - * output file */ - - if (fputs(buffer, fp_out) == EOF) { - fprintf(stderr, "Error: Couldn't write output" - "of llstat to out file\n"); - exit(1); - } - - /* closing file opened for storing llstat's output */ - if (fclose(fp_out)) { - fprintf(stderr, "Error: Couldn't close llstat" - "outfile: %s\n", llstat_file); - exit(1); - } - } + while (fgets(buffer, LLSTAT_READ_SIZE, fp_popen) != NULL) { + /* Following code should be in while loop as llstat + * will keep on sending output each second and will + * not exit on itself. It will be killed when we finsh + * with our command so we must make the output file + * consistent after writing each 1024 bytes chunk */ + + /* opening file where llstat will write its output */ + fp_out = fopen(llstat_file, "a"); + if (!fp_out) { + fprintf(stderr, "Error: Couldn't open llstat" + "outfile file: %s\n", + llstat_file); + exit(1); + } + /* fgets reads the popen output and fprintf writes it to + * output file */ + + if (fputs(buffer, fp_out) == EOF) { + fprintf(stderr, "Error: Couldn't write output" + "of llstat to out file\n"); + exit(1); + } + + /* closing file opened for storing llstat's output */ + if (fclose(fp_out)) { + fprintf(stderr, "Error: Couldn't close llstat" + "outfile: %s\n", llstat_file); + exit(1); + } + } /* closing popen for llstat */ if (pclose(fp_popen) < 0) { fprintf(stderr, "Error: Couldn't pclos" @@ -395,7 +391,7 @@ void fork_command(int type, unsigned short id, char* command, char* llstat_file) int with_llstat = 1; int status; char stats_path[1024]; - char stats_path_temp[1024]; + char stats_path_temp[1024 + 6]; /* 6=strlen("/stats") */ if (strlen(llstat_file) == 0) with_llstat = 0; @@ -417,7 +413,7 @@ void fork_command(int type, unsigned short id, char* command, char* llstat_file) if (with_llstat) { /* comment #25 of BUG 10968 */ - sleep(2); + sleep(2); /* sending kill to all llstat commands created for each * lustre-client respectively */ @@ -455,12 +451,13 @@ int main(int argc, char **argv) while ((c = getopt(argc, argv, "l:g:c:i:a:h")) != 1) switch (c) { case 'l': - strcpy(llstat_file, optarg); - if (strlen(llstat_file) > LEN_OUT) { + if (strlen(optarg) > sizeof(llstat_file)-1) { fprintf(stderr, "length of outfile file" " is too long\n"); exit(1); - } + } + strncpy(llstat_file, optarg, + sizeof(llstat_file)); break; /* When any value is written to vfs_track_gid, then VFS @@ -469,7 +466,9 @@ int main(int argc, char **argv) * write_track_xid writes given in vfs_track_gid * here. */ case 'g': - strcpy(gid_string, optarg); + if (strlen(optarg) > sizeof(gid_string)-1) + return -E2BIG; + strncpy(gid_string, optarg, sizeof(gid_string)); get_command_from_argv(optind, argc, argv, "", command); gid = atoi(gid_string);